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 this after adding to the project "bootcamp" and "bower" tools. What's .bin purpose? What is it created for?
That is a folder where binaries (executables) from your node modules are located.
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",
devDependencies
can be run this way. npm run <script_name>
can be used for this. This is important because allows you to get rid of global dependencies to start/build/test your project, and get rid of big README that describes which version of which dependency you need to install to make all this work. The only global dependency that remains is Node itself. And this also can be solved with some node version manager (E.g. Volta), that can pin Node version to your package.json
. –
Rogozen © 2022 - 2024 — McMap. All rights reserved.