【问题标题】:How does npm scripts prioritise local dependency over global ones?npm 脚本如何优先考虑本地依赖于全局依赖?
【发布时间】:2017-11-09 06:19:11
【问题描述】:

我了解 npm 脚本会将 ./node_modules/.bin 添加到您的 PATH,因此您只需使用下面的 package.json 运行 npm test,npm 将自动使用在 ./node_modules/.bin 中找到的本地版本的 mocha

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

这是一个不错的功能,因为它让我不用像这样编写package.json 文件:

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

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

【问题讨论】:

    标签: javascript json node.js npm npm-scripts


    【解决方案1】:

    Node.js 将尝试先运行your locally installed packages。

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

    有关 Node.js 如何解析所需模块的更详细说明,here is a nice breakdown。

    【讨论】:

    • 好吧,看起来很有希望,所以../../../../node_modules/ 将始终是检查/usr/local/bin(全局)之前的第一个合适的模块。甜的!谢谢。
    猜你喜欢
    • 2014-08-12
    • 2017-04-08
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多