What is the purpose of .bin folder in node_modules?

前端 未结 2 413
滥情空心
滥情空心 2020-12-08 13:17

Today one colleague explained me how to create nodejs projects and I notice that in ./node_modules there is an invisible folder named .bin. I must said that I discovered thi

2条回答
  •  死守一世寂寞
    2020-12-08 14:07

    The directory node_modules/.bin is where the binaries of the modules used by your project are stored, normally using symbolic links to the respective binaries in the corresponding module's directory. For example, that is how I see the binary standard from the npm package standard (JavaScript style guide, linter, and formatter)

    $ ls node_modules/.bin/standard -l
    lrwxrwxrwx 1 jfolpf jfolpf 22 jul 17 08:29 standard -> ../standard/bin/cmd.js
    

    When I run node_modules/.bin/standard I am indeed running node_modules/standard/bin/cmd.js from the npm package standard. This symbolic link was created upon the installation of the package, that is, upon npm install standard

    These binaries also allow you to use modules directly from npm scripts. For example, you may not have installed standard globally with npm install standard -g, which means that you cannot run standard directly from your terminal on your module's main directory.

    But you can write an npm start or npm test script by adding the following, respectively, to your package.json:

    "scripts": {
      "start": "standard src/*.js",
      "test": "standard src/*.js && node myTest.js"
    }
    

    and this is completely correct given you have standard as the project dependency. Even though the module is not global and not usable by the operating system directly, npm can look for the bin folder for the given standard module name and trigger the compiled binary. So indeed, npm runs such a script :

    "start": "node_modules/.bin/standard src/*.js",
    

提交回复
热议问题