npm i node-modules-prod --save-dev
node_modules packages contain a lot of clutter, test folders, examples, documentation etc. Production site will never use them.
Small files containing just a few lines like .gitignore or .npmignore are taking a whloe allocation unit on a disk, that's why a 10Mb node_modules folder by size, may take 18Mb of disk space.
Local packages are stored as symbolic links in node_modules - great for development, but npm install on PROD will leave them as symbolic links, which will be broken once pushed to server.
This utility will:
- convert symlink folders to actual folders
- strip unneeded files and folders to bare minimum (
Raspberry Pifriendly) - ignore packages listed as devDependencies
constcopyNodeModules=require('node-modules-prod');vardest='BUILD';gulp.task('copy-node-modules',function(cb){copyNodeModules('./',// sourceRoot relative to DEV rootdest,// destinationRoot relative to DEV root{},// use default optionscb// callback for gulp task);});quiet: true// do not show progress in console, (progress shows only folder names)
logIgnored: false// do not show ignored items in console (in magenta color)
noDevDependencies: true,// do not copy devDependencies (as per package.json)useNpmignore: true,// use npmignore file in each folder to exclude filesnoIgnoreList: false,// use ignore.js listpackageDir:'',// dir name for package.json when not in dev rootlogToFile: false// for debugging, logfiles are created in LOG/ folderThe node-modules-prod only copies existing folders form DEV node_modules to BUILD folder.
- Run
npm installin DEV, to make sure that all your dependencies innode_modulesare in working order for target operating system! - Make sure not to change default options to exclude unneeded folders and files from copying:
noDevDependencies: true,// do not copy devDependencies (form package.json)useNpmignore: true,// use npmignore file in each folder to exclude files- If symlinks are in
package.json, do NOT runnpm installin BUILD folder! - The
node-modules-prodaggresively excludes unneeded files saving a lot of space. Excluded files can be listed in console (color cyan- excluded by npmignore, magenta - other than npmignore) with<-mark, by usinglogIgnored:trueoption. Exclusion list is in ignore.js file. Readme and License files are excluded too. Check licensing requirements for each package.
- do NOT append that subfolderName to
dest - add
options.packageDir=subfolderNameto tell where to look forpackage.jsonotherwise devDependencies exclusion will not work.
constcopyNodeModules=require('node-modules-prod');vardest='BUILD';gulp.task('copy-npm-modules',function(cb){copyNodeModules('./subfolderName',// sourceRoot relative to DEV rootdest,// destinationRoot relative to DEV root'node_modules',// dirName{// options:packageDir: 'subfolderName';// tell that package.json is in this subfolder},cb// callback for gulp task);});copying progress is shown in console with options {quiet:false} only folders are shown. (there are too many files to show) Additionally option {logIgnored: true} will show ignored files.
source->destionation// default color ignored<-// magenta {logIgnored:true}symbolicLink=>folder// blueuse option {logToFile: true} to create file logs for copying operation. Folder LOG/ must exist in DEV root prior to running the task. Separate log files for excluded items will be created, for:
excluded-byConditionexcluded because item is listed indevDependenciesor inignore.jslistexcluded-byNpmignoreexcluded because items match.npmignorepatterns.
File.npmignorein some module may contain patterns that are not correctly interpreted by micromatch
To check if you have symbolic links in node_modules, look for little link arrows on the folder icon in file explorer:
to remove unneeded files and folders from bower_components try bower-purge.
Polymer 2 components are great! Don't bother with Polymer 3 (for C!# or C--).
NodeJS v8 and gulp v3 (tested on Win7)