【问题标题】:Grunt install node_modules locallyGrunt 在本地安装 node_modules
【发布时间】:2016-09-28 23:29:14
【问题描述】:

我有一个 Gruntfile 来安装一些 npm 并制作其他功能。

问题是:npm的下载工作正常,但是在全局下载node_modules

/User/my_user/node_modules

我希望 gruntfile 在我的项目中本地下载 nom 而无需指定路径。

这是我的 grunt 文件的一部分:

module.exports = function(grunt) {
    grunt.initConfig({
        shell: {
            install: {
                options: {
                    stdout: true,
                    stderr: true
                },
                command: [
                    "npm install grunt-contrib-sass",
                    "npm install node-sass",
                    "npm install grunt-contrib-less",
                    "npm install less",
                    "npm install grunt-contrib-watch",
                    "npm install grunt-contrib-clean",
                    "npm install grunt-contrib-copy",
                    "npm install grunt-csso",
                    "npm install grunt-deployments"
                ].join("&&")
            },
            install_test: {
                options: {
                    stdout: true,
                    stderr: true
                },
                command: [
                    "sudo npm install -g phantomjs",
                    "npm install -g casperjs",
                    "mkdir app/Test/Frontend",
                    "sudo chmod -R 777 app/Test/Frontend"
                ].join("&&")
            },
        }
    });
    grunt.loadNpmTasks("grunt-contrib-less");
    grunt.loadNpmTasks("grunt-contrib-watch");
    grunt.loadNpmTasks("grunt-contrib-clean");
    grunt.loadNpmTasks("grunt-contrib-copy");
    grunt.loadNpmTasks("grunt-shell");
    grunt.loadNpmTasks("grunt-csso");
    grunt.loadNpmTasks("grunt-rsync");

    grunt.registerTask("install", [
        "shell:cake_tmp",
        "shell:install",
        "shell:install_test"
    ]);
};

在我这样做之后:

sudo npm install grunt
sudo npm install grunt-shell
grunt install

返回我没有找到模块的错误,因为它不是本地的而是全局的..

我该如何解决?

谢谢

【问题讨论】:

  • 确保您的 shell 命令在您的 CWD 中运行。
  • 我不明白你为什么要把这些东西放在你的 Gruntfile 中。看起来它们应该在您的package.json 中,然后与npm install 一起安装。

标签: node.js gruntjs


【解决方案1】:

这是一种非常粗暴的工作方式。理想情况下,所有这些都应该放在根文件夹中的package.json 中。在进行必要的更改(如果有)并执行npm install 后将其放入您的package.json,它将在本地安装包,preinstall 挂钩将在执行安装之前安装全局包。

{
    "name": "appname",
    "version": "0.0.0",
    "dependencies": {},
    "devDependencies": {
        "grunt": "~0.4.1",
        "grunt-contrib-sass": "*",
        "node-sass": "*",
        "less": "*",
        "grunt-concurrent": "*",
        "grunt-contrib-clean": "*",
        "grunt-contrib-watch": "*",
        "grunt-contrib-less": "*",
        "grunt-contrib-copy": "*",
        "grunt-shell": "*",
        "grunt-csso": "*",
        "grunt-deployments": "*"
    },
    "engines": {
        "node": ">=0.8.0"
    },
    "scripts": {
        "test": "grunt test",
        "preinstall": "sudo npm install -g phantomjs && npm install -g casperjs"
    }
  }

npm 提供了称为 preinstall、install、postinstall 等的钩子,使用它们进行-g 安装。

关注LINK了解更多信息

您可以在Gruntfile.js 中作为shell 目标包含的其他任务

【讨论】:

  • npm global 目前无法从 package.json 安装,使用 preinstall 挂钩是一种解决方法。
猜你喜欢
  • 2013-03-24
  • 2017-12-20
  • 2017-05-31
  • 2016-07-24
  • 2018-01-29
  • 2011-11-16
  • 2019-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多