【问题标题】:Grunt Warning: Task "concat" not foundGrunt 警告:找不到任务“concat”
【发布时间】:2015-09-08 09:28:13
【问题描述】:

我正在尝试从一个简单的 Grunt 示例开始,但遇到了 grunt-contrib-concat 的问题。

这是我的 Gruntfile.js:

$ cat Gruntfile.js 
module.exports = function(grunt) {
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    concat: {
      options: {
        separator: ';'
      },
      dist: {
        src: ['src/**/*.js'],
        dest: 'dist/<%= pkg.name %>.js'
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-concat');

  grunt.registerTask('default', ['concat']);

还有我的 package.json:

$ cat package.json 
{
  "name": "Linker",
  "version": "0.0.0",
  "description": "A test project that is meant to be a dependency",
  "repository": {
    "type": "git",
    "url": "git://github.com/jonbri/Linker.git"
  },
  "main": "src/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
      "grunt-contrib-concat": "*"
  },
  "author": "",
  "license": "ISC"
}

运行 npm install 没有显示任何明显的错误:

$ npm install
$ ls node_modules/
grunt  grunt-contrib-concat

这是我的目录结构:

$ ls
Gruntfile.js  node_modules  package.json  README.md  src
$ ls src
index.js

当我运行 grunt concat 我得到这个:

$ grunt concat
Loading "concat.js" tasks...ERROR
>> Error: Cannot find module 'ansi-styles'
Warning: Task "concat" not found. Use --force to continue.

Aborted due to warnings.

我的设置:

Lubuntu 12.10,节点:v0.10.25,npm:1.4.21,grunt-cli:v0.1.13,grunt:v0.4.5

我错过了什么吗?

【问题讨论】:

    标签: javascript node.js gruntjs npm grunt-contrib-concat


    【解决方案1】:

    您应该将其运行为:grunt 不带 concat,因为任务是 default 之一。运行它不需要任何参数。所以启动的命令就变得简单了:

    grunt

    使用它来安装 grunt-contrib-concat:

    npm install grunt-contrib-concat --save-dev
    

    Package.json 应该是这样的:

    "devDependencies": {
      "grunt-contrib-concat": "^0.5.1"
    },  
    

    【讨论】:

    • 谢谢你,但是当我在没有参数的情况下运行时,我得到了完全相同的输出
    • 我运行没有问题,你可以尝试删除node_modules并重新安装grunt-contrib-concat吗?
    • 奇怪...我删除了 node_modules,又做了一次“npm install”,这一次成功了。不知道为什么会出错,但谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多