100pic
100pic

Reputation: 387

Node: Can I run global modules/packages from command line without specifying full path [Ubuntu, Nodejs, NPM]

I'm a linux / ubuntu / node newbie. I am running ubuntu 16.04 on a virtual private web server. It's running nodejs & everything is running well.

But when I run a global module/package from the command line, I write it like so:

node /usr/local/bin/forever start /usr/local/bin/http-server /var/www/myWebsite -p 8000

Which works fine. But you'll notice in node I have to specify the full path to my globally installed module/package (/usr/local/bin/)

My question: Is there a way I can run a global node module/package without specifying the full path?

EG., instead of writing:

node /usr/local/bin/forever    // forever is a globally installed module 

Can I make it so I can write:

node forever    // forever is a globally installed module

I have tried editing my ~./bashrc file to include a NODE_PATH like so

nano ~/.bashrc
# added line at bottom of bashrc file
NODE_PATH=/usr/local/bin

But no luck. Also, when I run echo $NODE_PATHI get:

/usr/local/bin

Which is the correct path (as in I use that path for commands such as node /usr/local/bin/forever

Or have I got my wires crossed? Am I even supposed to be able to run a node module/package without specifying the full path? Is there any reason why it's not good practice to do so?

Many thanks.

Upvotes: 4

Views: 2037

Answers (1)

100pic
100pic

Reputation: 387

I am so silly - editing the .bashrc file did seem to work. (Actually I'm not sure if this did work, or if it was automatically set correctly the whole time)

What I didn't realise is that I needed to:

  • Restart the terminal
  • Run a global package with just it's name (no "node" prefix)

EG in ubuntu cmd line:

node forever -h    // WRONG!

forever -h    // Works!

Thought I'd post in case someone in a similar situation finds this.

Upvotes: 3

Related Questions