node.js 本地安装的 gulp 不在命令行中运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33677545/
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 19:31:43  来源:igfitidea点击:

Locally installed gulp not running in command line?

node.jscommand-linegulppackage.json

提问by user2378417

I am new to nodejs and gulp stuff. I working on a nodejs project in which I have to run jslint on all the files. I am using gulp for this purpose.

我是 nodejs 和 gulp 的新手。我在一个 nodejs 项目中工作,我必须在其中对所有文件运行 jslint。为此,我正在使用 gulp。

My problem is that In order to run gulp on cli I don't want to install gulp globally and also does not want to update my path variable, So I have installed gulp and other node modules in my project locally using the package.json file

我的问题是为了在 cli 上运行 gulp,我不想全局安装 gulp,也不想更新我的路径变量,所以我使用 package.json 文件在我的项目中本地安装了 gulp 和其他节点模块

cd myproject

npm install

光盘我的项目

安装

Since I don't want to install gulp globally and want to run the local gulp I have added script in my package.json file like this as given in thisquestion

由于我不想全局安装 gulp 并想运行本地 gulp,因此我在我的 package.json 文件中添加了脚本,如问题所示

{
  "name": "",
  "version": "1.0.0",
  "main": "index.js",
  "private": true,
  "dependencies": {
    "async": "1.5.0"
  },
  "devDependencies": {
    "gulp": "^3.9.0",
    "gulp-jslint": "^0.2.2"
  },
  "scripts": {
      "gulp": "./node_modules/.bin/gulp"   // is this correct?
    }
}

Add added a gulpfile.js inside my myproject folder

添加在我的 myproject 文件夹中添加了一个 gulpfile.js

var gulp = require('gulp'); 

// include plug-ins
var jslint = require('gulp-jslint');

// JS hint task
gulp.task('lint', function() {
  gulp.src('./common/srp/*.js')
    .pipe(jslint())
    .pipe(jslint.reporter('default'));
});

gulp.task("default", ["lint"]);

But now on my command line inside myproject folder, when I run gulp and gulp lint I get an error

但是现在在 myproject 文件夹中的命令行上,当我运行 gulp 和 gulp lint 时出现错误

user1-VirtualBox:~/myproject$ gulp lint

/usr/local/node-v0.10.26-linux-x64/bin/gulp No such file or directory

user1-VirtualBox:~/myproject$ gulp lint

/usr/local/node-v0.10.26-linux-x64/bin/gulp 没有那个文件或目录

Its looking for gulp in the global node module.

它在全局节点模块中寻找 gulp。

Is there a way to make gulp run on cli without installing globally and updating PATH variable.

有没有办法让 gulp 在 cli 上运行而无需全局安装和更新 PATH 变量。

Any help will be appreciated Thanks

任何帮助将不胜感激谢谢

回答by snorberhuis

You can find any executable installed by npm in node_modules/.bin. So you can run gulp locally using:

您可以在node_modules/.bin. 因此,您可以使用以下命令在本地运行 gulp:

./node_modules/.bin/gulp

You can find more information at no command 'gulp' found - after installation

您可以在没有找到“gulp”命令的情况下找到更多信息- 安装后

回答by Rahul Jha

With your code you should be able to run command npm run gulp

使用您的代码,您应该能够运行命令 npm run gulp

Please try

请尝试

One way to define script is

定义脚本的一种方法是

"scripts": {
      "gulp": "gulp"
    }

If in case you are not able to run gulp command in your project, run

如果您无法在项目中运行 gulp 命令,请运行

npm link gulp

It will link your global install gulp with your local project. Then try

它会将您的全局安装 gulp 与您的本地项目链接起来。然后试试

 gulp -v

If it is showing you the version then you are done. Now you can run any gulp command as you want.

如果它向您显示版本,那么您就完成了。现在您可以根据需要运行任何 gulp 命令。

回答by thewildandy

Scripts defined in package.jsonare accessed through NPM, i.e. npm run-script gulp. I imagine you're trying to run plain old gulp, which shouldfail since you didn't install it globally.

中定义的脚本package.json通过 NPM 访问,即npm run-script gulp. 我想您正在尝试运行普通 old gulp,这应该会失败,因为您没有全局安装它。

The scripts section won't automatically create an alias, which I think is your mistake here. You could define one yourself or create a simple bash script if you don't want to type it every time.

脚本部分不会自动创建别名,我认为这是您的错误。如果您不想每次都输入它,您可以自己定义一个或创建一个简单的 bash 脚本。

回答by iTake

Try:

尝试:

path_to_node path_to_gulp_js gulp_task

Example:

例子:

node\node.exe node_modules\gulp\bin\gulp.js build

回答by Daniel Orlan

Like @snorberhuis said. The only way for me to get gulp to work globally was to call gulp manually I am building in a Jenkins environment

就像@snorberhuis 所说的那样。让 gulp 在全球范围内工作的唯一方法是手动调用 gulp 我正在 Jenkins 环境中构建

Execute Windows Batch Command

执行 Windows 批处理命令

cd your-app
npm install gulp

Execute Windows Batch Command

执行 Windows 批处理命令

cd your-app\node_modules\.bin 
gulp

回答by Desislav Kamenov

Just another alternative that will work locally but will give you global like feeling. Add to your shell config i.e. ~/.bash_profilethe following export PATH=$PATH:./node_modules/.bin you have to sourcethat file, execute rehashor just open a new shell and then gulp(and any other script inside that folder) shall be available as a global command.

只是另一种可以在本地工作但会给你像全球一样的感觉的替代方案。添加到您的 shell 配置,即 您必须在该文件中添加~/.bash_profile以下内容 ,执行或仅打开一个新的 shell,然后(以及该文件夹中的任何其他脚本)将作为全局命令可用。export PATH=$PATH:./node_modules/.bin sourcerehashgulp

回答by Tobin Bradley

Check in your project node_modules/.binfolder and make sure gulp is in there. I had a case where it wasn't there that I never tracked down the reason for. If it isn't there, try re-installing gulp locally and see if it shows up. If that doesn't work and you get tired of the problem, the gulp-clipackage will fix it for sure, but that shouldn't be something you haveto do.

检查您的项目node_modules/.bin文件夹并确保 gulp 在那里。我有一个案例,我从来没有找到原因。如果它不存在,请尝试在本地重新安装 gulp 并查看它是否出现。如果这不起作用并且您厌倦了这个问题,那么该gulp-cli软件包肯定会修复它,但这不应该是您必须做的事情。

回答by xikandar hayyat

The way I did this after bashing my head every possible place is simply going to your Application and install npm dependencies like this:

我在每一个可能的地方抨击我的头之后这样做的方式只是去你的应用程序并安装这样的 npm 依赖项:

1- E:\webra-jenkins\Code\trunk\WebRa.Web>npm install

1- E:\webra-jenkins\Code\trunk\WebRa.Web>npm install

Once npm installed then go this directory

安装 npm 后,转到此目录

2- [%Application_path%]\node_modules\.bin

2- [%Application_path%]\node_modules\.bin

And execute the gulp and give your file/task, like this:

并执行 gulp 并提供您的文件/任务,如下所示:

3-[%Application_path%]\node_modules\.bin>gulp gulpfile --tasks

3-[%Application_path%]\node_modules\.bin>gulp gulpfile --tasks

In my case as I saw the following lines... I got the inner happiness

就我而言,当我看到以下几行时......我得到了内心的幸福

18:06:36] Working directory changed to [%Application_path%]
[18:06:37] Tasks for [%Application_path%]\gulpfile.js

Now you can run your tasks 1 by one.

现在您可以一一运行您的任务。

[%Application_path%]\node_modules\.bin>gulp pack-vendor-js

回答by Valeriu Palo?

The simplest solution I know of is to use npm bin:

我所知道的最简单的解决方案是使用npm bin

`npm bin`/gulp ...

This keeps you away from hard-coding any paths.

这使您远离硬编码任何路径。

回答by JakuSmacku

Globally install gulp in C:\Users\%USERNAME% using this command

使用此命令在 C:\Users\%USERNAME% 中全局安装 gulp

npm install –g gulp

You can install any other gulp methods you need to use.. Ex:

您可以安装您需要使用的任何其他 gulp 方法.. 例如:

npm install -g gulp-concat
npm install -g gulp-uglify
npm install -g gulp-replace

Then at the directory you wish to use GULP. Open the command prompt (Shift + RightClick) then install locally and you'll be able to execute gulp.

然后在您希望使用 GULP 的目录中。打开命令提示符(Shift + RightClick)然后在本地安装,你就可以执行 gulp。

npm install gulp

You can install any other gulp methods you need to use.. Ex:

您可以安装您需要使用的任何其他 gulp 方法.. 例如:

npm install gulp-concat
npm install gulp-uglify
npm install gulp-replace