zhm
zhm

Reputation: 3641

Why can I run globally installed node modules by name?

I install a node module globally, let's say the grunt module. I install it by:

npm install -g grunt

It's installed in %APPDATA%\npm\node_modules\grunt.

Then I can run it in command line, like grunt --version. How does this happen? I mean, why can I directly use grunt as a command?

BTW, I'm using Windows. And I install NodeJS by .msi installer.

Upvotes: 1

Views: 277

Answers (1)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123463

You aren't really running the grunt package as a whole from the command.

The setup for this starts in grunt's package.json. In that, it's specified a bin script that's named the same as the package.

"bin": {
  "grunt": "bin/grunt"
},

When you install the package globally, npm adds an executable file for each bin script (there can be multiple per package) to a directory in your system's PATH, allowing a command line to find them when you type the command.

When you run grunt, it's sort of a shortcut to running node bin/grunt from the directory where it's installed, passing along any arguments you provided after it.

Upvotes: 2

Related Questions