npm学习(十七)之node_modules中的bin文件夹

发布时间 2023-11-28 12:12:49作者: 威武的大萝卜

 

 

可执行文件

在本地模式下,可执行文件指向的位置;

  • ./node_modules/.bin 以便可以通过 npm 运行的脚本使用它们。

    • 例如,当您运行测试,将在路径中 npm test

npm为script字段中的脚本路径都加上了node_moudles/.bin前缀

npm为script字段中的脚本路径都加上了node_moudles/.bin前缀,这意味着:你在试图运行本地安装的依赖在 node_modules/.bin 中的脚本的时候,可以省略node_modules/.bin这个前缀。例如:

我刚npm install webpack了,而在我的项目下的node_modules目录的.bin子目录下:

就多了一个叫做webpack的脚本

 

本来运行这个脚本的命令应该是:node_modules/.bin webpack

但由于npm已经自动帮我们加了node_modules/.bin前缀了,所以我们可以直接写成:

"scripts": {"start": "webpack"}

而不用写成:

"scripts": {"start": "node_modules/.bin webpack"}

原文:npm run adds node_modules/.bin to the PATH provided to scripts. Any binaries provided by locally-installed dependencies can be used without the node_modules/.bin prefix

总结

在本地环境下,可行性文件放在node_modules下的.bin文件夹中,npm为scripts字段中的脚本路径自动添加了node_modules/.bin前缀,所以可以直接写

"scripts": {"start": "webpack"}

而不是

"scripts": {"start": "node_modules/.bin webpack"}

参考

NPM node_modules 文档夹的详解

【npm】伙计,给我来一杯package.json!不加糖