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

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

问题描述

首先,我对npm和grunt很陌生。我们有一个项目,我们正在使用Grunt来编译和生成输出文件。我正在尝试设置我们的构建服务器以使用Grunt生成输出文件。我们使用的Windows与TFS源代码管理,并由于它是260个字符的路径限制 ,我们无法将grunt-bower-task模块检入源代码管理(,因为它只能使用230个字符在我的项目目录中运行 npm install 时,它可以正常工作并安装以下所需的模块到我的项目目录中的node_modules文件夹:


  • grunt

  • grunt-bower-task li>
  • grunt-contrib-compass

  • grunt-contrib-connect

  • grunt-contrib-jshint

  • grunt-contrib-requirejs

  • grunt-contrib-watch



然后当我从我的项目目录运行 grunt deploy 时,所有工作都按预期运行。



虽然我可以简单地运行 npm install 部分e构建过程,我不愿意,因为它需要几分钟才能下载所有文件,而且我不希望我们的构建依赖于可用的外部Web服务。



我已经看到您可以安装模块无论是本地还是全球,所以我希望能够在生成服务器上全局安装模块,以便在运行 grunt之前不需要直接位于项目目录中的node_modules文件夹中部署。我为上面列出的每个模块以及 npm install 运行了 npm install -g ,以及 npm install -g [模块] g grunt-cli



如果我使用npm前缀-g ,它会告诉我全局模块目录是 C:\ Users [My User] \AppData\Roaming\\\
pm
,当我查看该目录的node_modules文件夹时,我看到所有模块。但是,当我运行 grunt deploy 时,它抱怨:


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

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


未找到本地Npm模块grunt-contrib-watch。是否安装?



未找到本地Npm模块grunt-contrib-jshint。是否安装了?



...


使用* grunt deploy --baseC:\ Users [My User] \AppData\Roaming\\\
pm,但它会抱怨它无法找到其他文件,如.jshintrc。



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

在构建过程中,手动将模块手动复制到本地项目目录,但我希望尽可能避免这种情况。



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

 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-连接:〜0.3.0,
grunt-bower-task:〜0.2.3
}
}

谢谢。

解决方案

对于 npm ,您应该在模块上使用符号链接,以达到类似的结果。



全局 npm 安装只是为了方便命令行实用程序,例如 jshint grunt-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]\AppData\Roaming\npm, 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_modules\grunt* 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]\AppData\Roaming\npm", 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天全站免登陆