What is the purpose of .bin folder in node_modules?
Asked Answered
P

2

71

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?

Pachysandra answered 14/8, 2014 at 10:46 Comment(0)
Z
40

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.)

Zante answered 14/8, 2014 at 10:54 Comment(2)
OK. Thanks. I have no idea about how to find this kind of information in Google. Is there any way I could fin .bin purpose in Google? Actually, I'm now trying to find what is a config.rb file but I'm getting nothing. ThanksPachysandra
"bin folder in node_modules" third result for me, titled "npm-folders"Zante
D
35

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",
Decry answered 17/7, 2019 at 19:33 Comment(2)
It is also important to mention that arbitrary binary from 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
Thanks a lot! This should definitely be the accepted answer :)Piero

© 2022 - 2024 — McMap. All rights reserved.