Get Smarter
Get Smarter

Reputation: 191

How to put local node package on path?

Newbie question. I have chosen not to install express with -g option. I did not use npm -g which would put it on the path globally. Instead it is installed in my local mac user directory. What I am not clear on is exactly what or how you put a package like express on the path so it can be invoked etc? What exactly needs to be on the path (node_modules?) so these packages are available just like a -g installation? I could have used home-brew I suppose but anyway, I now have all node packages and everything local. Another situation is that I am not able to run any of the nodejs tutorials. Although there might be smarter ways to do this, I wonder if sudo is really such a good way to install a development package ....

Now for example, I want to run the tutorial javascripting which is a nodejs tutorial. How do I do this. If I just type: Mac1$ javascripting

it finds nothing.

Same for Mac1$ express

UPDATE: THIS WAS ANSWERED IN THE COMMENTS

The commands exist in a hidden directory after a regular

    install npm install express 

in my case this the command goes here: /users/MAC1/node_modules/.bin

It is this path that needs to be placed on the $PATH as described in the first comment.

Thanks guys.

Upvotes: 1

Views: 421

Answers (1)

Sukima
Sukima

Reputation: 10084

npm installes executable to two places. By default running a npm install in a project will install any binaries in ./node_modules/.bin. When you use the -g flag (npm install -g package-name) it will install into a global path. You can find out the global path by running npm bin -g. Add that to your path and globally installed executables will be accessible.

You can also add ./node_modules/.bin to your path to allow easy access to executables added by packages in your project folder. I admit to using this on a trusted local machine. However, this is very dangerous and not a recommended way to expose the executables in the node_modules directory.

Best alternative is to add the executable to the scripts section of the package.json file and then use npm run-script <command> which will auto prepend the ./node_modules/.bin when executing.

package.json
{
  "scripts": {
    "foo": "foo --arguments"
  }
}
Example
$ npm install foo
$ ls ./node_modules/.bin
foo
$ npm run-script foo
# Executes:
./node_modules/.bin/foo --arguments

Upvotes: 1

Related Questions