简体   繁体   English

如何使用全局安装的gulp模块?

[英]How to use globally installed gulp modules?

I tried this tutorial to basically setup my gulp project and activate less processing and watching. 我尝试了本教程 ,基本上设置了我的gulp项目并激活了更少的处理和观察。 It works. 有用。 Now I'd like to install the modules globally, in order to acces them from everywhery on the machine and not to installa them for every project: 现在我想在全球范围内安装模块,以便从机器上的每个设备访问它们,而不是为每个项目安装它们:

npm install --global gulp-less
npm install --global gulp-watch
npm install --global gulp-autoprefixer
npm install --global gulp-plumber

Now I want to run gulp in my project folder, but gulp cannot find the globally installed modules: 现在我想在我的项目文件夹中运行gulp,但是gulp无法找到全局安装的模块:

$ cd /my/project
$ gulp
module.js:327
    throw err;
    ^

Error: Cannot find module 'gulp-less'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/root/temp/gulptest/gulpfile.js:9:12)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)

How to get it working and use global gulp modules? 如何让它工作并使用全局gulp模块?

The solution was npm-link . 解决方案是npm-link I had to link all my local packages to global counterparts: 我必须将所有本地软件包链接到全球同行:

npm link gulp
npm link gulp-less
npm link gulp-watch
npm link gulp-autoprefixer
npm link gulp-plumber
npm link path

Now it works. 现在它有效。

Some StackOverflow posts to this subject: 一些StackOverflow发布到这个主题:

1. Install gulp globally: 1.全局安装gulp:

If you have previously installed a version of gulp globally, please run npm rm --global gulp to make sure your old version doesn't collide with gulp-cli. 如果您以前在全球安装了npm rm --global gulp版本,请运行npm rm --global gulp以确保您的旧版本不会与gulp-cli发生冲突。

$ npm install --global gulp-cli

2. Install gulp in your project devDependencies: 2.在项目devDependencies中安装gulp:

$ npm install --save-dev gulp

Read more... https://github.com/gulpjs/gulp/blob/master/docs/getting-started.md 阅读更多... https://github.com/gulpjs/gulp/blob/master/docs/getting-started.md

For what it's worth, I solved this by placing this line at the top of my gulpfile.js 为了它的价值,我通过将这一行放在我的gulpfile.js的顶部来解决这个问题

module.paths = module.paths.concat(module.parent.paths);

My parent module knew my npm global directory, but while running gulpfile.js, a child module seems to be created which doesn't. 我的父模块知道我的npm全局目录,但是在运行gulpfile.js时,似乎创建了一个没有的子模块。 Instead, module paths is only relevant to the current directory from where the project is being run from and then each previous directory back to the root of my drive. 相反,模块路径仅与运行项目的当前目录相关,然后每个以前的目录都与驱动器的根目录相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM