如何轻松验证安装的正确 npm 依赖项? [英] How to easily verify correct npm dependencies installed?

查看:174
本文介绍了如何轻松验证安装的正确 npm 依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有任何未满足的 package.json 依赖项,我如何知道何时提示用户运行 npm install?

How can I know when to prompt user to run npm install if there are any unmet package.json dependencies?

我想这样做,因为如果任何 require() 失败,用户会收到一条糟糕的错误消息:

I would like to do this, because if any require() fails, the user gets a poor error message:

module.js:340
    throw err;
          ^
Error: Cannot find module 'nopt'

我之前曾尝试只检查 node_modules 目录是否存在,但这仅对新的 git 克隆有效.我也试过只需要 npm 并运行 npm install 作为负载的一部分,但这很重.

I've previously tried to just check for the existence of a node_modules directory, but this only works effectively for fresh git clones. I've also tried just requiring npm and running npm install as part of load, but that is very heavy weight.

我希望有一个更轻量级的库,它只解析 package.json 并确保 node_modules 内容满足要求.

I'm hoping there is a lighter weight library out there that just parses package.json and makes sure node_modules contents satisfy the requirements.

一个想法是使用 process.on('uncaughtException') 仅捕获模块导入错误,但首先查看是否有标准"解决方案.

One idea was to use process.on('uncaughtException') to catch only module import errors, but looking to see if there is a "standard" solution first.

推荐答案

今天发现了这个.不确定您是否还需要这个.

Found this today. Not sure if your still need this.

https://www.npmjs.com/package/check-dependencies

npm install check-dependencies --save-dev

安装这个包并保存到你的 package.json.

Install this package and save to your package.json.

require('check-dependencies')(config, callback);

config 是以下对象,然后传递给回调.

config is the following object, which is then passed to the callback.

{
    status: number,      // 0 if successful, 1 otherwise
    depsWereOk: boolean, // true if dependencies were already satisfied
    log: array,          // array of logged messages
    error: array,        // array of logged errors
}

这篇关于如何轻松验证安装的正确 npm 依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆