node.js - grunt 安装时遇到问题

标签 node.js gruntjs

我按照 grunt site 上的文档进行操作。执行 sudo npm install -g grunt-cli 后,我得到了这个输出

    npm http GET https://registry.npmjs.org/grunt-cli
    npm http 304 https://registry.npmjs.org/grunt-cli
    npm http GET https://registry.npmjs.org/findup-sync
    npm http GET https://registry.npmjs.org/nopt
    npm http GET https://registry.npmjs.org/resolve
    npm http 304 https://registry.npmjs.org/resolve
    npm http 304 https://registry.npmjs.org/findup-sync
    npm http 304 https://registry.npmjs.org/nopt
    npm http GET https://registry.npmjs.org/abbrev
    npm http GET https://registry.npmjs.org/glob
    npm http GET https://registry.npmjs.org/lodash
    npm http 304 https://registry.npmjs.org/abbrev
    npm http 304 https://registry.npmjs.org/glob
    npm http 304 https://registry.npmjs.org/lodash
    npm http GET https://registry.npmjs.org/graceful-fs
    npm http GET https://registry.npmjs.org/inherits
    npm http GET https://registry.npmjs.org/minimatch
    npm http 304 https://registry.npmjs.org/minimatch
    npm http 304 https://registry.npmjs.org/inherits
    npm http 304 https://registry.npmjs.org/graceful-fs
    npm http GET https://registry.npmjs.org/lru-cache
    npm http GET https://registry.npmjs.org/sigmund
    npm http 304 https://registry.npmjs.org/sigmund
    npm http 304 https://registry.npmjs.org/lru-cache
    npm http GET https://registry.npmjs.org/sigmund/-/sigmund-1.0.0.tgz
    npm http 200 https://registry.npmjs.org/sigmund/-/sigmund-1.0.0.tgz
    /usr/local/bin/grunt -> /usr/local/lib/node_modules/grunt-cli/bin/grunt
    grunt-cli@0.1.11 /usr/local/lib/node_modules/grunt-cli
    ├── resolve@0.3.1
    ├── nopt@1.0.10 (abbrev@1.0.4)
    └── findup-sync@0.1.2 (lodash@1.0.1, glob@3.1.21)

此后,当我执行grunt时,我收到此消息

    grunt-cli: The grunt command line interface. (v0.1.11)

    Fatal error: Unable to find local grunt.

    If you're seeing this message, either a Gruntfile wasn't found or grunt
    hasn't been installed locally to your project. For more information about
    installing and configuring grunt, please see the Getting Started guide:

    http://gruntjs.com/getting-started

此后我也关注了this link解决我的问题,但我仍然遇到上面相同的错误。

更新

Gruntfile.js

    module.exports = function () {
        grunt.initConfig({
            min: {
                dist: {
                    src: 'file-one.js',
                    dest: 'file-one.min.js'
                }
            }
        });
    }

package.json 文件

    {
      "name": "grunt",
      "version": "0.1.0",
      "devDependencies": {
        "grunt": "~0.1.11",
        "grunt-contrib-jshint": "~0.6.3",
        "grunt-contrib-nodeunit": "~0.2.0",
        "grunt-contrib-uglify": "~0.2.2"
      }
    }

最佳答案

您已将 grunt-cli 安装为全局包,但还需要在本地安装 grunt。 grunt-cli 包安装一个全局命令行工具,但该命令行工具只是尝试加载本地安装的 grunt 模块。这样做是为了允许人们为不同的项目拥有多个版本的 grunt,您需要将 grunt 本身作为本地包安装在您正在处理的项目中。

从您链接到的页面:

Note that installing grunt-cli does not install the Grunt task runner! The job of the Grunt CLI is simple: run the version of Grunt which has been installed next to a Gruntfile. This allows multiple versions of Grunt to be installed on the same machine simultaneously.

因此,您需要将 grunt 作为 package.json 文件中的本地依赖项,就像任何其他模块一样,您不会在其中列出 grunt-cli .

关于node.js - grunt 安装时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20586164/

相关文章:

node.js - 我怎样才能安装 grunt-cli 而不会出错?

javascript - AngularJS 通过组合 javascript 文件减少 HTTP 请求

javascript - 如何使用 'require' 导入命名空间?

javascript - 有没有办法告诉 grunt node_modules 在哪里?

javascript - 为什么有人会关闭 Nagle 的算法?

html - 仅 Express 加载主页

node.js - 使用 Mongoose、Express 删除用户

javascript - Uglify.JS 缩小文件名而不是文件内容

javascript - 使用 Node.js + 异步模块从同步回调创建同步循环

java - Nodejs和java套接字实现错误: connect ECONNREFUSED 127. 0.0.1:8081