63

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Cremix_hellven
  • 895
  • 2
  • 7
  • 13

2 Answers2

38

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

Andrew Lohr
  • 5,380
  • 1
  • 26
  • 38
alandarev
  • 8,349
  • 2
  • 34
  • 43
  • 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. Thanks – Cremix_hellven Aug 14 '14 at 11:12
  • 4
    "bin folder in node_modules" third result for me, titled "npm-folders" – alandarev Aug 14 '14 at 11:14
32

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",
João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
  • 5
    It is also important to mention that arbitrary binary from `devDependencies` can be run this way. `npm run ` 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`. – Ruslan Stelmachenko Mar 03 '20 at 21:34
  • 2
    Thanks a lot! This should definitely be the accepted answer :) – Chen Ni Dec 25 '21 at 15:42