node.js NPM 清洁模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11351784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:59:31  来源:igfitidea点击:

NPM clean modules

node.jsnpm

提问by Dave Causey

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?

有没有办法让 npm 解构 node_modules 下的所有模块?像 npm rebuild 这样的东西会删除所有构建工件但不重建它们?

回答by Charles

You can just delete the node_module directory

您可以删除 node_module 目录

rm -rf node_modules/

回答by Lucas

I added this to my package.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",

Seems to work well.

似乎运作良好。

回答by theGecko

You can take advantage of the 'npm cache' command which downloads the package tarball and unpacks it into the npm cache directory.

您可以利用“npm cache”命令下载包 tarball 并将其解压到 npm 缓存目录中。

The source can then be copied in.

然后可以复制源。

Using ideas gleaned from https://groups.google.com/forum/?fromgroups=#!topic/npm-/mwLuZZkHkfUI came up with the following node script. No warranties, YMMV, etcetera.

使用从https://groups.google.com/forum/?fromgroups=#!topic/npm-/mwLuZZkHkfU收集的想法,我想出了以下节点脚本。没有保证,YMMV,等等。

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());

回答by qubyte

In a word no.

总之没有

In two, not yet.

两个,还没有

There is, however, an open issue for a --no-buildflag to npm installto perform an installation without building, which could be used to do what you're asking.

但是,有一个未解决的问题,即--no-build标志在npm install没有构建的情况下执行安装,这可用于执行您的要求。

See this open issue.

请参阅此未解决的问题

回答by Daniel De León

For windows environment:

对于 windows 环境:

"scripts": {
    "clean": "rmdir /s /q node_modules",
    ...
}

回答by Amitesh Singh

I have added few lines inside package.json:

我在 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 cleanonly you can use this rimraf node_modules.

如果你只想clean你可以使用这个rimraf node_modules

回答by jeckep

Try https://github.com/voidcosmos/npkill

试试 https://github.com/voidcosmos/npkill

npx npkill

it will find all node_modules and let you remove them.

它将找到所有 node_modules 并让您删除它们。

npkill

杀戮