Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to omit devDependencies when copying node_modules?

Tags:

gulp

I'd like to copy only modules, which are important for application - those located inside dependencies in package.json. I'd like to omit those under devDependencies.

package.json

{            
    "dependencies": {
        "express": "^4.13.4",
        "log4js": "^0.6.33"            
    },
    "devDependencies": {
        "gulp": "^3.9.1",
        "gulp-rename": "^1.2.2",
        "gulp-typescript": "^2.12.1",
        "typings": "^0.7.9",
        "yargs": "^4.3.2"
    }
}

gulpfile.js

gulp.task('copy_packages', function() {
      gulp
         .src('node_modules/**/*.*')
         .pipe(gulp.dest('../release/node_modules'));
});

Is there any module or a smart way to distinguish which modules belongs to dependencies group and which to devDependencies?

like image 896
Fka Avatar asked Mar 25 '16 15:03

Fka


1 Answers

Had the same problem...felt weird this was not considered when gulp was created. My work around was using child_process to run npm install and specify a directory to put the node_modules directory with only the packages you need for your application.

e.g: gulp.task('createDeployNodeModulesFolder', function(cb) { spawn('"npm"', ['install', '--prefix', './dist/', 'package1', 'package2'], {stdio: 'inherit', shell: true}, function (err, stdout, stderr) { console.log(stdout); console.log(stderr); }) .on('close', cb); });

In your case you want only the production dependencies so you can likely use: npm install --prefix /deployDir/ --only=prod

There will be some warnings complaining about no package.json..etc., but those are just warnings. If you really want to get rid of them, I guess you can just add a task to copy or create a package.json into the deploy directory before running npm install.

like image 54
CHISHIUAN LU Avatar answered Oct 15 '22 17:10

CHISHIUAN LU