Cheng's Blog

  • About Me
  • Archive
  • feeds

Posts match “ npm ” tag:

about 7 years ago

Reference globally installed NPM package

To reference globally installed NPM packages (i.e. packages installed with the "-g" option, like npm install -g webpack), a NODE_PATH has to be set in your environment. I used NVM to manage node.js and here is my setting (for Mac):

export NODE_PATH=/Users/cheng/.nvm/versions/node/v0.12.7/lib/node_modules

Now, you can reference these packages from anywhere.

If you use other OS, check out this post on StackOverflow.

  • npm
  • July 23, 2015 12:36
  • Permalink
  • Comments
 
about 7 years ago

Create npm list shortcut

The default npm list command not only list the packages installed locally but also the packages they depend on. To most users, showing packages dependency is not necessary, as most of the time, we only want to know what is installed. To fix this, the --depth=0 option can be used:

npm ls --depth=0

So we get the list on the left instead of on the right:

dir_name@1.0.0 /path/blah/blah                 dir_name@1.0.0 /path/blah/blah
|-- babel-core@5.8.3                           |-- babel-core@5.8.3
                                                   |- babel-plugin-constant-folding@1.0.1
                                                   |- babel-plugin-eval@1.0.1
                                                   ...

However, this is not enough. Because if you have installed a package either

  • globally or
  • locally but without adding it to the package.json file

You are likely to see these errors:

npm ERR! max depth reached: webpack@*, required by babel-loader@5.3.2   // this error is caused by installing webpack globally
npm ERR! extraneous: file-loader@0.8.4 /blah/file-loader   // this error is caused by using npm install instead of npm install --save

To ignore these errors, a solution provided by this blog works:

npm ls --depth=0 "$@" 2>/dev/null

To create a proper shorcut, open your .bash_profile (for Mac):

alias nl="npm ls --depth=0 "$@" 2>/dev/null"
alias nlg="npm ls -g --depth=0 "$@" 2>/dev/null"  #for listing global packages

Reload the .bash_profile file and that's it!

. .bash_profile
  • npm
  • July 23, 2015 13:19
  • Permalink
  • Comments
 

Copyright © 2013 GuoCheng . Powered by Logdown.
Based on work at subtlepatterns.com.