如何让 Grunt Deploy 使用全局 NPM 模块而不是本地模块 [英] How to make Grunt Deploy use global NPM modules instead of local ones

查看:18
本文介绍了如何让 Grunt Deploy 使用全局 NPM 模块而不是本地模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我对 npm 和 grunt 非常陌生.我们有一个项目,我们使用 Grunt 来编译和生成输出文件.我正在尝试设置我们的构建服务器以使用 Grunt 来生成输出文件.我们使用带有 TFS 源代码控制的 Windows,并且由于 它的 260 个字符路径限制,我们无法将 grunt-bower-task 模块检查到源代码管理中(因为它单独使用 230 个字符在其安装路径中).

当我从我的项目目录运行 npm install 时,它工作正常并将以下必需的模块安装到我的项目目录中的 node_modules 文件夹中:

  • 咕哝
  • grunt-bower-task
  • grunt-contrib-compass
  • grunt-contrib-connect
  • grunt-contrib-jshint
  • grunt-contrib-requirejs
  • grunt-contrib-watch

然后当我从我的项目目录运行 grunt deploy 时,一切都按预期工作.

虽然我可以简单地将运行 npm install 作为构建过程的一部分,但我不想这样做,因为下载所有文件需要几分钟,而且我不想要我们的构建依赖于可用的外部网络服务.

我已经看到您可以在本地安装模块或全局,所以我希望能够在构建服务器上全局安装模块,这样在运行 grunt deploy<之前,它们不需要直接位于项目目录内的 node_modules 文件夹中/em>.我已经为上面列出的每个模块运行了 npm install -gnpm install -g [module],以及 npm install -g grunt-cli.

如果我执行 npm prefix -g 它会告诉我全局模块目录是 C:Users[My User]AppDataRoaming pm,当我查看了该目录的 node_modules 文件夹,我确实看到了所有模块.但是,当我运行 grunt deploy 时,它会抱怨:

<块引用>

致命错误:无法找到本地 grunt

如果我只包含 *node_modulesgrunt* 目录,那么我仍然会收到这些错误:

<块引用>

未找到本地 Npm 模块grunt-contrib-watch".安装了吗?

未找到本地 Npm 模块grunt-contrib-jshint".安装了吗?

...

我也尝试过使用 *grunt deploy --base "C:Users[My User]AppDataRoaming pm",但它抱怨说它找不到其他文件,例如 .jshintrc.

那么有没有一种方法可以让我运行 grunt deploy 并让它检查模块的 npm 全局前缀路径,而不是在项目目录中查找?

一个 hacky 解决方法是在构建过程中手动将模块复制到本地项目目录,但我想尽可能避免这种情况.

作为参考,这是我的 package.json 文件的样子:

<代码>{"name": "我的项目",版本":0.0.1",脚本":{"preinstall": "npm i -g grunt-cli bower"},开发依赖":{"咕噜声": "~0.4.1","grunt-contrib-compass": "~0.2.0","grunt-contrib-watch": "~0.4.4","grunt-contrib-jshint": "~0.6.0","grunt-contrib-requirejs": "~0.4.1","grunt-contrib-connect": "~0.3.0","grunt-bower-task": "~0.2.3"}}

谢谢.

解决方案

不要使用 npm 的全局选项,而应该使用 符号链接 在您的模块上,以获得类似的结果.

全局 npm 安装只是为了方便命令行实用程序,例如 jshintgrunt-cli.

First off, I'm very new to npm and grunt. We have a project that we are using Grunt to compile and produce the output files for. I am trying to setup our build server to use Grunt to produce the output files. We are using Windows with TFS source control, and due to it's 260 character path limit, we are not able to check the grunt-bower-task module into source control (as it alone uses 230 characters in its installed path).

When I run npm install from my project directory it works fine and installs the following required modules into the node_modules folder in my project directory:

  • grunt
  • grunt-bower-task
  • grunt-contrib-compass
  • grunt-contrib-connect
  • grunt-contrib-jshint
  • grunt-contrib-requirejs
  • grunt-contrib-watch

And then when I run grunt deploy from my project directory everything works as expected.

While I could simply make running npm install part of the build process, I prefer not to, as it takes a few minutes to download all of the files, and I don't want our builds to depend on an external web service being available.

I've seen that you can install modules either locally or globally, so I'm hoping to just be able to install the modules globally on the build server so that they do not need to be in the node_modules folder directly inside the project directory before running grunt deploy. I've ran npm install -g, as well as npm install -g [module] for each of the modules listed above, as well as npm install -g grunt-cli.

If I do npm prefix -g it shows me that the global module directory is C:Users[My User]AppDataRoaming pm, and when I look in that directory's node_modules folder I do see all of the modules. However, when I run grunt deploy it complains with:

Fatal error: Unable to find local grunt

If I include just the *node_modulesgrunt* directory, then I still get these errors:

Local Npm module "grunt-contrib-watch" not found. Is it installed?

Local Npm module "grunt-contrib-jshint" not found. Is it installed?

...

I've also tried using *grunt deploy --base "C:Users[My User]AppDataRoaming pm", but it complains that it then cannot find other files, such as .jshintrc.

So is there a way that I can run grunt deploy and have it check the npm global prefix path for the modules, rather than looking in the project directory?

A hacky work around is to copy the modules manually during the build process to the local project directory, but I would like to avoid this if possible.

For reference, this is what my package.json file looks like:

{
  "name": "MyProject",
  "version": "0.0.1",
  "scripts": {
    "preinstall": "npm i -g grunt-cli bower"
  },
  "devDependencies": {
    "grunt": "~0.4.1",
    "grunt-contrib-compass": "~0.2.0",
    "grunt-contrib-watch": "~0.4.4",
    "grunt-contrib-jshint": "~0.6.0",
    "grunt-contrib-requirejs": "~0.4.1",
    "grunt-contrib-connect": "~0.3.0",
    "grunt-bower-task": "~0.2.3"
  }
}

Thanks.

解决方案

Instead of using the global option for npm, you should be using symbolic links on your modules, to achieve similar results.

Global npm installs are intended only as a convenience for command-line utilities such as jshint, or grunt-cli.

这篇关于如何让 Grunt Deploy 使用全局 NPM 模块而不是本地模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆