Tell npm to look for node_modules inside a differe

2019-06-10 00:58发布

问题:

npm looks for node_modules in the current directory, and all its parent directories, then looks in the global location. My current proposed dir structure is:

  • global
    • package.json
    • node_modules/
  • project-1
    • gulpfile.coffee
  • project-2
    • gulpfile.coffee

But this isn't where npm normally looks for this. Is there an environment variable I can set to deal with this, or a way I can tell it to look in a specific directory? Thanks!

回答1:

Try the following to set NODE_PATH in gulp.

.pipe(preprocess({context: { NODE_PATH: '$NODE_PATH:/path/to/other/dir'}}))

A full example would look like the following.

var preprocess = require('gulp-preprocess');

gulp.task('html', function() {
  gulp.src('./app/*.html')
    .pipe(preprocess({context: { NODE_PATH: '$NODE_PATH:/path/to/other/dir'}})) //To set environment variables in-line 
    .pipe(gulp.dest('./dist/'))
});

The above 2 examples add to the node path if it is set. If you want to just set it do the following.

.pipe(preprocess({context: { NODE_PATH: '/path/to/other/dir'}}))


回答2:

There is a NODE_PATH environment variable you can set.