What is the purpose of .bin folder in node_modules?
That is a folder where binaries (executables) from your node modules are located.
NPM site states:
Executables When in global mode, executables are linked into {prefix}/bin on Unix, or directly into {prefix} on Windows.
When in local mode, executables are linked into ./node_modules/.bin so that they can be made available to scripts run through npm. (For example, so that a test runner will be in the path when you run npm test.)
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",