node.js 在本地安装 Node Grunt

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15777859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:05:37  来源:igfitidea点击:

Installing Node Grunt Locally

node.jsbuild-automationportable-executablegruntjs

提问by Dominic P

I'm in the process of reworking my build system, and I've read that node.js with grunt is a good way to go. I've never used either, and I'm having a bit of trouble.

我正在重新设计我的构建系统,并且我已经阅读了带有 grunt 的 node.js 是一个很好的方法。我从来没有用过,我遇到了一些麻烦。

The problem is that I want to set up a portable build environment that I can include in the version control for my project (maybe this isn't possible). Getting node.js and npm working has been no trouble. But, every instruction I see for installing grunt says to use the -gflag with npm which installs it globally. Since I want a totally portable environment, I have attempted to leave this off, but I can't get grunt to work.

问题是我想设置一个可移植的构建环境,我可以将其包含在我的项目的版本控制中(也许这是不可能的)。让 node.js 和 npm 正常工作一直没有问题。但是,我看到的关于安装 grunt 的每一条指令都说要在-gnpm 中使用该标志,以便在全球范围内安装它。由于我想要一个完全可移植的环境,因此我尝试将其关闭,但我无法咕噜咕噜地工作。

Am I missing something, or is what I'm attempting to do not feasible?

我是否遗漏了什么,或者我试图做的事情不可行?

回答by Ruben Vermeersch

Have a look at http://gruntjs.com/getting-started

看看http://gruntjs.com/getting-started

Grunt has recently been split into a project-local dependency (grunt) and a command-line launcher (grunt-cli). It's the latter that should be installed globally.

Grunt 最近被拆分为项目本地依赖项 ( grunt) 和命令行启动器 ( grunt-cli)。后者应该全局安装。

As an extra hint on ensuring that you can take your builds everywhere: make sure you save all dependencies in package.json, by using the --saveand --save-devparameters when using npm install. More info: https://npmjs.org/doc/install.html

作为确保您可以在任何地方进行构建的额外提示:确保将所有依赖项保存在 中package.json,在使用时使用--save--save-dev参数npm install。更多信息:https: //npmjs.org/doc/install.html

回答by tomajar

You can use local grunt without global (-g) installation of a grunt-cli by calling:

您可以通过调用以下命令使用本地 grunt,而无需全局 (-g) 安装 grunt-cli:

node node_modules/grunt-cli/bin/grunt --version

Of course first you need install it in you project locally and have a grunt version greater than 0.3; for example:

当然首先你需要将它安装在你本地的项目中并且有一个大于 0.3 的 grunt 版本;例如:

npm install grunt-cli
npm install [email protected]

Or add them both to your packages.json and call

或者将它们都添加到你的 packages.json 并调用

npm install  

This should also help when you just can't install any package globally as I've described in https://stackoverflow.com/a/39046355/2201879

当您无法像我在https://stackoverflow.com/a/39046355/2201879 中描述的那样全局安装任何软件包时,这也应该有所帮助

回答by splintor

See https://www.npmjs.com/package/grunt-cli#installing-grunt-cli-locally:

请参阅https://www.npmjs.com/package/grunt-cli#installing-grunt-cli-locally

Installing grunt-cli locally

在本地安装 grunt-cli

If you prefer the idiomatic Node.js method to get started with a project (npm install && npm test) then install grunt-cli locally with npm install grunt-cli --save-dev. Then add a script to your package.json to run the associated grunt command: "scripts": { "test": "grunt test" }. Now npm test will use the locally installed ./node_modules/.bin/gruntexecutable to run your Grunt commands.

如果您更喜欢使用惯用的 Node.js 方法来开始项目 ( npm install && npm test),请使用 npm install grunt-cli --save-dev 在本地安装 grunt-cli。然后添加脚本到您的package.json运行相关的咕噜命令:"scripts": { "test": "grunt test" }。现在 npm test 将使用本地安装的./node_modules/.bin/grunt可执行文件来运行您的 Grunt 命令。

To read more about npm scripts, please visit the npm docs:
https://docs.npmjs.com/misc/scripts.

要阅读有关 npm 脚本的更多信息,请访问 npm 文档:
https://docs.npmjs.com/misc/scripts 。

回答by Sarabjit Singh

Here is the command line code that will install the latest version of Grunt in your project folder, adding it to your devDependencies:

这是将在您的项目文件夹中安装最新版本 Grunt 的命令行代码,并将其添加到您的 devDependencies 中:

npm install grunt --save-dev

The same can be done for gruntplugins and other node modules. As seen in the following example installing the JSHint task module:

对 gruntplugins 和其他节点模块也可以这样做。如以下安装 JSHint 任务模块的示例所示:

npm install grunt-contrib-jshint --save-dev

Checkout the current available gruntplugins to be installed and used on your project at the plugins page.

在插件页面查看要在您的项目中安装和使用的当前可用 gruntplugins。

Be sure to commit the updated package.json file with your project when you're done!

完成后,请务必将更新的 package.json 文件与您的项目一起提交!