【问题标题】:How to identify npm packages installed or not in my project如何识别我的项目中安装或未安装的 npm 包
【发布时间】:2019-08-26 16:26:33
【问题描述】:

我正在尝试使用 nodejs 在我的 Angular 项目中查找已安装或未安装的 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-lock.json 和 package.json 比较安装

标签: javascript node.js angular6 angular7


【解决方案1】:

打开您的文件项目并检查 Package.json 文件,否则检查 node_modules。

【讨论】:

  • 可以试试nodejs吗?
【解决方案2】:

您可以打开 Package.json 并查看依赖项或使用 npm install 安装依赖项中的所有包。

【讨论】:

    【解决方案3】:

    使用 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');
    

    【讨论】:

      【解决方案4】:

      这是一个脚本,用于检查预定义列表中的每个包是否都在 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);
      

      这将在package.jsonpackage-lock.json 中查看您的dependenciesdevDependencies,并打印来自PACKAGES 的任何找不到的内容。

      这假设您使用的是足够现代的节点版本,以便写入 package-lock.json

      【讨论】:

      • 需要指定package.json全路径吗?
      • 我的 json 路径是这样的: const packageJSON = readJSONFile('c:/xampp/htdocs/projects/chemis/package.json'); const packageLockJSON = readJSONFile('c:/xampp/htdocs/projects/chemis/package-lock.json');
      • 如果您没有从项目内部运行脚本,那么是的,您需要指定文件的绝对或相对路径。这在两种情况下都适用于我。可能还有其他事情发生,例如旧的节点版本(是否支持扩展语法),或者丢失的文件,或者拼写错误的包名。您应该尝试删除 try/catch 以查看 fs.readFileSync 是否抛出。否则,一些简单的 console.log 调试应该会告诉你出了什么问题。
      • 无法获得空值,例如:未安装以下软件包:未安装以下软件包:
      • 获取 notDefined 和 notInstalled 为空
      【解决方案5】:

      您也可以使用NCU

      它有各种各样的命令可以帮助你实现你的目标。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-14
        • 2017-09-30
        • 1970-01-01
        • 2021-04-13
        • 2018-09-23
        • 1970-01-01
        相关资源
        最近更新 更多