6

我了解 npm 脚本会添加./node_modules/.binPATHnpm testpackage.json../node_modules/.bin

"scripts": {
    "test": "mocha"
}

这是一个不错的功能,因为它可以节省我编写package.json这样的文件:

"scripts": {
    "test": "./node_modules/.bin/mocha"
}

但是,如果我引入一个在全球范围内安装 mocha 的新开发人员怎么办?或者我需要将其推送到具有预配置全局包的环境?如果我使用的是简写mocha,而不是./node_modules/.bin/mocha在 mypackage.json中,那么全局或本地包的优先级是什么?

4

1 回答 1

6

Node.js 将首先尝试运行您本地安装的包

./node_modules/如果您需要一个模块,Node.js 通过遍历祖先目录( 、、、../node_modules/../../node_modules/)中的所有 node_modules/ 目录来查找它。使用找到的第一个适当的模块。

有关 Node.js 如何解析所需模块的更详细说明,这里有一个很好的细分

于 2017-06-07T21:14:42.173 回答