有没有办法让 npm 取消构建 node_modules 下的所有模块?像 npm rebuild 这样的东西会删除所有构建工件但不重建它们?
9 回答
您可以删除 node_module 目录
rm -rf node_modules/
这个工作实际上有特殊的命令
npm ci
它将删除node_modules
目录并根据您的package-lock.json
文件安装软件包
我将此添加到我的 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",
似乎运作良好。
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());
对于windows环境:
"scripts": {
"clean": "rmdir /s /q node_modules",
...
}
我在 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",
...
}
如果你只想clean
你可以使用这个rimraf node_modules
。
npm ci
适用于这种情况,但仅当您的package.json
和package-lock.json
同步时,如果您一直致力于快速解决冲突或通过删除目录/符号链接在目录级别进行更新,则情况可能并非总是如此。这个问题的综合答案将是这个..
- 用你想要的东西编辑
package.json
并删除你不需要的东西。 - 生成
package-lock.json
这样的,npm install --package-lock-only
- 运行
npm ci
。这应该删除所有工件并且不应该重建它们。