[英]How to run my local angular project by using globally installed npm packages?
[英]How to identify npm packages installed or not in my project
我正在尝试使用nodejs在我的角度项目中找到已安装或未安装的npm软件包。 如何从package.json获取已安装的软件包以进行检查。 我有一个带有包装的阵列。 例:
var npmpackagesarr=["example-ng6-input","example-ng6-radio","example-ng6-combo"];
for(var i=0;i<npmpackagesarr.length;i++){
if(npmpackagesarr[i]=="????"){
console.log("Hey..this package already installed");
}
else{
console.log("Hey..this package not installed");
}
}
如何确定阵列npm软件包是否已安装?
打开文件项目并检查Package.json文件,否则检查node_modules。
您可以打开Package.json并查看依赖项,或使用npm install
在依赖项中安装所有软件包。
使用Package.json上的JSON.parse并将依赖项键提取到数组中。 之后,您可以使用要查找的值在该数组上运行indexOf。
let data;
fetch('./Package.json').then(data => {
data = Object.keys((JSON.parse(res)).dependencies);
})
search(array, package) {
if(array.indexOf(package) !== -1) {
// installed
} else {
// not installed message
}
}
search(data, '@angular/core');
这是一个脚本,用于检查预定义列表中的每个软件包是否都在package.json
定义并已安装。
const fs = require('fs');
const PACKAGES = ['aws-sdk', 'node-cache', 'example-ng6-input', 'lorem-hipsum'];
const readJSONFile = (name) => {
try {
return JSON.parse(fs.readFileSync(name, 'utf8'));
} catch (error) {
return {};
}
};
const isDepMissing = ({ name, json }) => {
const d1 = json.dependencies || {};
const d2 = json.devDependencies || {};
return !(d1[name] || d2[name]);
};
const packageJSON = readJSONFile('package.json');
const packageLockJSON = readJSONFile('package-lock.json');
const notDefined = PACKAGES.filter(name => isDepMissing({ name, json: packageJSON }));
const notInstalled = PACKAGES.filter(name => isDepMissing({ name, json: packageLockJSON }));
console.log('the following packages are not defined:', notDefined);
console.log('the following packages are not installed:', notInstalled);
这将devDependencies
package.json
和package-lock.json
dependencies
和devDependencies
,并打印PACKAGES
中找不到的任何内容。
假设您使用的节点版本足够现代,因此可以编写package-lock.json
。
您可以将NCU用于相同的功能。
它具有各种此类命令,可以帮助您实现目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.