14

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?

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
TheOneTeam
  • 25,806
  • 45
  • 116
  • 158
  • Are you sure it is installed under `~/projectA/node_modules/.bin` and not `~/projectA/node_modules/sails/bin` instead? What do you mean by source? – verybadalloc Aug 04 '13 at 01:00
  • @verybadalloc source is like the unix dot http://en.wikipedia.org/wiki/Dot_(Unix) and http://superuser.com/a/46146 – SHernandez Apr 07 '15 at 11:29
  • 2
    possible duplicate of [How to use package installed locally in node\_modules?](http://stackoverflow.com/questions/9679932/how-to-use-package-installed-locally-in-node-modules) – Shahar 'Dawn' Or Jul 07 '15 at 21:43

8 Answers8

16

I can give you an inelegant solution, which is working for me. I just exported my path in my .bashrc file.

export PATH="$PATH:./node_modules/.bin"

Edit: It's been a while, but I have changed my strategy to use npm scripts instead. In the above case, setup package.json as follows:

"scripts": {
    "sails": "sails"
}

Which you can then run with

npm run sails

or with arguments

npm run sails -- <args>
bmavity
  • 2,477
  • 2
  • 24
  • 23
14

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.

brice
  • 24,329
  • 7
  • 79
  • 95
3

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!

cadlac
  • 2,802
  • 3
  • 18
  • 34
1

To use on the command line like sails generate foo you will need to install the npm module globally.

npm install -g sails

You could also use the path to the bin in the command if you don't want to install globally:

./node_modules/sails/bin/sails.js generate foo
particlebanana
  • 2,416
  • 21
  • 22
1

The official instructions for sails (https://github.com/balderdashy/sails) advises

To install the latest stable release with the command-line tool:

sudo npm -g install sails

This installs globally and adds to a directory like /usr/local/bin that should be in your $PATH.

But to answer the general question regarding the location of the binaries if you install locally, they should be placed in ./node_modules/.bin directory (so run ./node_modules/.bin/sails ...)

SheetJS
  • 22,470
  • 12
  • 65
  • 75
0

If you don't like to mess up with your PATH for running a npm script that isn't global -- e.g. you are the only one to use it --, I would personally recommend the use of an sh "alias".

  1. npm install (locally) your beloved package (json-diff here, for instance)

    cd ~ && npm install json-diff
    
  2. alias it (save it in your ~/.xxxxrc file):

    alias diffj "\`npm bin\`/json-diff !*"
    

Then, for diffing 2 json's:

diffj old.json new.json
Dimitri Podborski
  • 3,714
  • 3
  • 19
  • 31
Petrus
  • 31
  • 2
0

In my ~/.bashrc, I have the following:

function nbin {
  local dir;
  dir=$(npm bin)
  if [ -d "$dir" ]; then
    ( # subshell to not change this shell's $PATH
      PATH=$dir:$PATH
      eval "$@"
    )
  else
    echo "\"$dir\" is not an npm binary directory." >&1
    return 1
  fi
}

I can then run executable foo in the .bin directory as:

nbin foo
Tom Hale
  • 40,825
  • 36
  • 187
  • 242
-1

Possible workaround with NPM 5.2+ using the npx command.

npx sails new test-project

See this question for a similar use case and elegant solutions.

LessQuesar
  • 3,123
  • 1
  • 21
  • 29
Titu
  • 11
  • 1
  • 4