I'm currently exploring different methods to package only the necessary node_modules dependencies for my project. After some research, I came across gulp-npm-dist and created a gulpfile.js.
var gulp = require('gulp');
var npmDist = require('gulp-npm-dist');
gulp.task('CopyNodeDependencies', function() {
gulp.src(npmDist(), {base:'./node_modules'})
.pipe(gulp.dest('./node_dependencies'));
});
This allows me to include only the modules required by my package.json:
{
"version": "1.0.0",
"name": "common",
"private": true,
"devDependencies": {
"gulp": "^3.9.1",
"gulp-less": "^3.1.0",
"gulp-npm-dist": "^0.1.2",
"gulp-rename": "^1.2.2",
"pump": "^1.0.1"
},
"dependencies": {
"chart.js": "^2.7.3",
"chartjs-node-canvas": "^2.0.1",
"moment": "^2.24.0"
}
}
However, when I try to use 'var moment = require('moment');' in my node file, it gives me an error saying it cannot find the module 'moment'. I have even renamed node_dependencies to node_modules, but the issue persists. I also attempted using relative paths like './node_dependencies/moment', without success.
Here is the folder structure of the node_dependencies for reference: https://i.sstatic.net/UbGvW.png