Is there a way to get npm to unbuild all the modules under node_modules? Something like npm rebuild that removes all build artifacts but doesn't rebuild them?
11 Answers
There is actually special command for this job
npm ci It will delete node_modules directory and will install packages with respect your package-lock.json file
More info: https://docs.npmjs.com/cli/ci.html
3 Comments
npm ci and not just npm i. The main reason that npm i also updates the lock file and this is probably that you don't want todo.npm i. Its a little misleading because it's called "clean install" but thats not necessarily what it does. Would be better named like rebase or something. If you rely on your project version to be in package.json as well, package-lock doesn't get updated either until you do an npm i, though thats not as big of a deal.You can just delete the node_module directory
rm -rf node_modules/ 12 Comments
package.json: "clean": "rm -rf node_modules", "reinstall": "npm run clean && npm install", "rebuild": "npm run clean && npm install && npm run build",. Seems to work well.I added this to my package.json:
"build": "npm build", "clean": "rm -rf node_modules", "reinstall": "npm run clean && npm install", "rebuild": "npm run clean && npm install && npm run build", Seems to work well.
6 Comments
reinstall is now built-in command: npm cinpm ci does not simply delete and reinstall all modules; it is mainly meant for deploy envs, and doesnt modify the package-lock.rm -rf will not work on windows. Script's should be multiplatform compatible.Try https://github.com/voidcosmos/npkill
npx npkill it will find all node_modules and let you remove them.
2 Comments
npm ci works for this scenario, but only when your package.json and package-lock.json are in sync, which might not always be the case if you have been working on either one to resolve conflicts quickly or are updating on the directory level by removing directories/symbolic links. A comprehensive answer to the question would be this..
- Edit the
package.jsonwith what ever you want and remove what you dont need. - Generate the
package-lock.jsonlike this,npm install --package-lock-only - Run
npm ci. This should remove all artifacts and shouldn't rebuild them.
2 Comments
npm ci --legacy-peer-depsnpm ci or npm install --force, it is not enough and can fail. That npm install --package-lock-only and then doing npm ci worked!!!! Thank you.You can take advantage of the 'npm cache' command which downloads the package tarball and unpacks it into the npm cache directory.
The source can then be copied in.
Using ideas gleaned from https://groups.google.com/forum/?fromgroups=#!topic/npm-/mwLuZZkHkfU I came up with the following node script. No warranties, YMMV, etcetera.
var fs = require('fs'), path = require('path'), exec = require('child_process').exec, util = require('util'); var packageFileName = 'package.json'; var modulesDirName = 'node_modules'; var cacheDirectory = process.cwd(); var npmCacheAddMask = 'npm cache add %s@%s; echo %s'; var sourceDirMask = '%s/%s/%s/package'; var targetDirMask = '%s/node_modules/%s'; function deleteFolder(folder) { if (fs.existsSync(folder)) { var files = fs.readdirSync(folder); files.forEach(function(file) { file = folder + "/" + file; if (fs.lstatSync(file).isDirectory()) { deleteFolder(file); } else { fs.unlinkSync(file); } }); fs.rmdirSync(folder); } } function downloadSource(folder) { var packageFile = path.join(folder, packageFileName); if (fs.existsSync(packageFile)) { var data = fs.readFileSync(packageFile); var package = JSON.parse(data); function getVersion(data) { var version = data.match(/-([^-]+)\.tgz/); return version[1]; } var callback = function(error, stdout, stderr) { var dependency = stdout.trim(); var version = getVersion(stderr); var sourceDir = util.format(sourceDirMask, cacheDirectory, dependency, version); var targetDir = util.format(targetDirMask, folder, dependency); var modulesDir = folder + '/' + modulesDirName; if (!fs.existsSync(modulesDir)) { fs.mkdirSync(modulesDir); } fs.renameSync(sourceDir, targetDir); deleteFolder(cacheDirectory + '/' + dependency); downloadSource(targetDir); }; for (dependency in package.dependencies) { var version = package.dependencies[dependency]; exec(util.format(npmCacheAddMask, dependency, version, dependency), callback); } } } if (!fs.existsSync(path.join(process.cwd(), packageFileName))) { console.log(util.format("Unable to find file '%s'.", packageFileName)); process.exit(); } deleteFolder(path.join(process.cwd(), modulesDirName)); process.env.npm_config_cache = cacheDirectory; downloadSource(process.cwd()); 1 Comment
npm install?npm ci
As mentioned by FDisk You can use npm ci to remove node modules folder and re-install npm packages from scratch.
npm prune
Alternatively, you can also use npm prune to remove extraneous packages installed inside your node_modules folder that aren't defined inside the package.json
References:
Comments
In a word no.
In two, not yet.
There is, however, an open issue for a --no-build flag to npm install to perform an installation without building, which could be used to do what you're asking.
See this open issue.
Comments
I have added few lines inside package.json:
"scripts": { ... "clean": "rmdir /s /q node_modules", "reinstall": "npm run clean && npm install", "rebuild": "npm run clean && npm install && rmdir /s /q dist && npm run build --prod", ... } If you want to clean only you can use this rimraf node_modules.
1 Comment
"clean": "rm -rf node_modules",I have done this in my package.json
"scripts": { "clean": "rm -rf ./node_modules package-lock.json .cache dist && npm i", } 1 Comment
rm -r using the f parameter is not recommended
shrinkwrap(see npmjs.org/doc/shrinkwrap.html), you can lock the versions of ALL your dependencies (instead of just the top level ones).packages.json(usingnpm --save) and putting thenode_modulesdirectory in.gitignore(if using git). CI should not pull your node_modules, but executenpm install. Finally, if downloading from npm registry takes too long, use something like sinopia to cache it.