简体   繁体   English

如何枚举模块中的包依赖关系?

[英]How to enumerate package dependencies in a module?

Guys i've been coding an node_module that's gives you the dependencies of the module you choose, So there is my code 伙计们,我一直在编码一个node_module,它为您提供了所选模块的依赖关系,所以有我的代码

function depend(modulename, cb) {
let package = JSON.parse(fs.readFileSync("./node_modules/"+modulename+'/package.json', "utf8"));
var re = 'CHECKING' ? package.dependencies : 'The module has no dependencies';
cb(re)  
}

const fs = require('fs');

module.exports = depend;

When i run it it on a module has no modules its gives me {} Instead of The module has no dependencies , So any ideas? 当我在没有模块的模块上运行它时,它给了我{}而不是The module has no dependencies ,那么有什么想法吗?

I'd suggest taking a look at how the Ternary (conditional) Operator works. 我建议您看一下三元(条件)运算符的工作方式。

In reference to the code you posted a comment: 关于代码,您发表了评论:

Your current code simply checks for the existence of package.dependencies, which even if it is an empty object, something like if(package.dependencies)... will evaluate to true because an empty object is a truthy value. 您当前的代码只是检查package.dependencies的存在,即使它是一个空对象,诸如if(package.dependencies)...类的东西也将评估为true,因为空对象是真实值。

What you need to do is check if the object itself is empty. 您需要做的是检查对象本身是否为空。 One way to do that is to check the length of its keys by using Object.keys . 一种方法是使用Object.keys检查其键的长度。

var re = Object.keys(package.dependencies).length ? package.dependencies : 'The module has no dependencies';

But an even better check would be a combination of your approach and this one: 但是更好的检查是将您的方法与以下方法结合起来:

var re = package.dependencies && Object.keys(package.dependencies).length ? ...

As it checks for both the existence of package.dependencies as well as whether or not it is empty. 当它检查package.dependencies是否存在以及它是否为空。

var re = 'CHECKING' ? package.dependencies : 'The module has no dependencies'; is a ternary . 三元的

Here it means : 这意味着:

If 'CHECKING' then assign package.dependencies to re otherwise assign 'The module has no dependencies' to re 如果为“ CHECKING”,则分配package.dependencies re分配,否则分配为'The module has no dependencies' re分配

This is your error, if ('CHECKING') will always evaluate to true so it will always assign package.dependencies to re . 这是您的错误, if ('CHECKING')始终评估为true ,那么它将始终将package.dependencies分配给re You'll have to refactor your code to something like this : 您必须将代码重构为以下形式:

var re = Object.keys(package.dependencies).length > 0 ? package.dependencies : 'The module has no dependencies';

Which will test if package.dependencies have one or more dependencies in order to affect the correct value to re . 这将测试package.dependencies是否具有一个或多个依赖关系,以便影响re的正确值。 We don't use length on package.dependencies because it is an object which haven't any length property, instead we use Object.keys . 我们不在package.dependencies上使用length ,因为它是一个没有length属性的对象,而是使用Object.keys

In the second example you provided in the comments, if (package.dependencies) will also always evaluate to true because package.dependencies is not null or undefined, so it's considered to be true . 在注释中提供的第二个示例中, if (package.dependencies)还将始终评估为true因为package.dependencies不是null或未定义,因此将其视为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM