【问题标题】:Node.js/Grunt - Can't run Grunt's watch - why?Node.js/Grunt - 无法运行 Grunt 的手表 - 为什么?
【发布时间】:2013-10-15 23:55:45
【问题描述】:

我正在尝试使用 autoprefixer css 后处理器。我正在学习教程并安装了 npm。使用 npm,然后我使用 package.json 文件在我的项目根目录中安装了 grunt 和 autoprefixer:https://github.com/nDmitry/grunt-autoprefixer/blob/master/package.json

按照教程,然后我在我的项目根目录中创建了这个Gruntfile.js

module.exports = function (grunt) {
    grunt.initConfig({
        autoprefixer: {
            dist: {
                files: {
                    'build/style.css': 'style.css'
                }
            }
        },
        watch: {
            styles: {
                files: ['style.css'],
                tasks: ['autoprefixer']
            }
        }
    });
    grunt.loadNpmTasks('grunt-autoprefixer');
    grunt.loadNpmTasks('grunt-contrib-watch');
};

之后教程建议使用 Grunt Watch 使用

./node_modules/.bin/grunt watch

结果

-bash: ./node_modules/.bin/grunt: No such file or directory

我也尝试导航到我的项目中的 grunt 文件夹,然后它说

-bash: node_modules/grunt: is a directory

我的本​​地用户文件夹中也有一个 node_modules 文件夹,但解决该文件夹时 grunt 也只是告诉我它是一个文件夹。

请帮助我,为什么这不起作用?我愿意真正学习 grunt,但我什至无法使用入门指南开始...

【问题讨论】:

    标签: javascript node.js terminal gruntjs grunt-contrib-watch


    【解决方案1】:

    您是否安装了grunt-cli? (npm install grunt-cli -g) 当您在项目根目录中运行grunt 时会发生什么?您应该运行的命令只是 grunt watch,在您的项目根目录中。

    编辑:您的项目根目录还必须有一个 package.json 文件,您可以在其中定义开发依赖项;例如

    {
        "name":"yourprojectname",
        "version":"0.0.1",
        "devDependencies":{
            "grunt":"*",
            "grunt-contrib-watch":"*",
            "grunt-autoprefixer":"*"
        }
    }
    

    【讨论】:

    • 是的,grunt-cli 已安装。从我的项目根目录运行 grunt watch 会得到 Warning: Task "watch" not found. Use --force to continue.
    • 您的 Gruntfile 配置对我来说看起来不错。 grunt-contrib-watch 在您的 local node_modules 目录中吗?如果没有,请在项目根目录中运行npm install,然后再次运行grunt watch
    • 谢谢,您的回答帮助我解决了问题。这是混合了全局和本地安装的模块以及 package.json 中有错误内容的组合
    【解决方案2】:

    如果可执行文件名称中有空格,则需要将其放在引号中

    "./node_modules/.bin/grunt watch"

    否则 linux 将以 watch 作为标志运行“./node_modules/.bin/grunt”。

    如果还是不行的话,

    可能是一些问题,要么您的 ldconfig 未更新,文件未设置为可执行文件,要么您尝试执行命令的用户没有权限。

    首先尝试运行“ldconfig”(只需键入并运行它) 更多信息在这里 http://www.cyberciti.biz/tips/linux-shared-library-management.html

    chmod -x 文件以使其可执行。

    运气好吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 2017-01-25
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多