Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NPM : how to source ./node_modules/.bin folder?

I have a problem on npm installation

I have created a project say project A

cd ~/projectA
npm install sails

but sails command is not found after installation.

I know that it is successfully install in ~/projectA/node_modules directory. but the executable cannot be sourced. And i know it is installed ~/projectA/node_modules/.bin

How can I source the .bin automatically whenever I enter into this projectA folder?

Did I did something wrong?

like image 455
TheOneTeam Avatar asked Aug 03 '13 14:08

TheOneTeam


People also ask

What is node_modules .bin folder?

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.

Where can I find node_modules folder?

On Unix systems they are normally placed in /usr/local/lib/node or /usr/local/lib/node_modules when installed globally.

Can I copy node_modules folder?

You can always copy node_modules and then run npm install or npm update in the new project to make sure you've got up-to-date versions. npm will use the files in node_modules as a cache and should only bring down newer content if required. In short: it won't hurt.

Can I copy node_modules folder to another project?

Yes you can copy whole node_modules (have done it multiple times) from one project to another and use same package.


2 Answers

You should use the npm bin command to get an absolute path to your current node bin directory.

For example:

➤ lessc
bash: lessc: command not found
➤ npm bin
/home/brice/[...]/node_modules/.bin
➤ export PATH=$(npm bin):$PATH
➤ lessc --version
lessc 1.7.3 (Less Compiler) [JavaScript]

This avoids the problem of relative paths, especially if you're going to be using this in a build system that will invoke the command in subdirectories.

like image 128
brice Avatar answered Sep 22 '22 19:09

brice


A bit more robust is:

export PATH=$(npm bin):$PATH

You can either run it, add it to your shell profile, or create an alias like:

alias snpm='export PATH=$(npm bin):$PATH'

If you do go the alias route, be sure to use single quotes so it delays the execution of the variables!

like image 36
cadlac Avatar answered Sep 19 '22 19:09

cadlac