javascript - 如何以编程方式删除 npm 包?
问题描述
我希望能够在运行时在 node.js 应用程序中动态添加和删除 npm 包。使用安装包可以npm.install(...)
正常工作,但npm.remove(...)
只是从文件系统中删除包,但代码中仍然需要实际模块。我尝试清除 npm 缓存和各种模块,decache
但不是运气。如何以编程方式删除包?
下面的代码以“commander”包为例说明了我的过程。
const npm = require("npm");
const assert = require("assert");
const name = "commander";
npm.load({}, error => {
try{
console.log(require(name));
} catch(e){
console.log("not available - good");
}
//install the package
npm.install(name, (error, data) => {
console.log("error install",error);
assert(require(name) != null)
//remove the package
npm.remove(name, (error, data) => {
console.log("error remove",error);
require.cache = {};
//still here, even so the files have been removed from the FS
console.log(require(name));
})
});
});
解决方案
推荐阅读
- python - resnet50 的差验证 Acc 和高验证损失
- javascript - 当我在我的 vue.js 项目中修改一个节点模块时,我是否必须重新编译一些东西才能让它工作?变化没有反映
- javascript - 从 asp.net 获取数据到 highchart.js
- html - Outlook 模板的 HTML/CSS 格式 - 表格/行/单元格高度不起作用
- c++ - 类似的解决方案但不同的答案
- pdf - 使用 pdftk 从 PDF 解压附件时保留目录结构?
- python-3.x - Scrapy从csv或数组中抓取保存的链接
- unix - 从 jenkins 执行 shell 运行 ansible playbook
- javascript - fs.writeFile 在新创建的 vue 项目中不起作用
- asp.net-mvc - Handle Vue Route on Page Reload from MVC Controller