最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How does npm scripts prioritise local dependency over global ones? - Stack Overflow

programmeradmin5浏览0评论

I understand npm scripts adds ./node_modules/.bin to your PATH, therefore you can simply run npm test using the package.json below, and npm will automagically use the local version of mocha found in ./node_modules/.bin

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

This is a nice feature, because it saves me writing package.json files like this:

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

BUT what if I bring on a new developer who has mocha installed globally? or I need to push this to an environment with preconfigured global packages? If I am using the short-hand mocha, rather than ./node_modules/.bin/mocha in my package.json, What takes precedence, the global or local package?

I understand npm scripts adds ./node_modules/.bin to your PATH, therefore you can simply run npm test using the package.json below, and npm will automagically use the local version of mocha found in ./node_modules/.bin

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

This is a nice feature, because it saves me writing package.json files like this:

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

BUT what if I bring on a new developer who has mocha installed globally? or I need to push this to an environment with preconfigured global packages? If I am using the short-hand mocha, rather than ./node_modules/.bin/mocha in my package.json, What takes precedence, the global or local package?

Share Improve this question asked Jun 7, 2017 at 18:37 ChrisChris 3821 gold badge4 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Node.js will try to run first your locally installed packages.

If you require a module, Node.js looks for it by going through all node_modules/ directories in ancestor directories (./node_modules/, ../node_modules/, ../../node_modules/, etc.). The first appropriate module that is found is used.

For a more detailed explanation about how Node.js resolves required modules, here is a nice breakdown.

发布评论

评论列表(0)

  1. 暂无评论