`node` in the command line
I ran into this hassle the other day when my npm
command failed to work in the command line. It took me a while to sort things out and left me in a foul mood. To prevent something like that from happening again, I decided to write this post. Nothing fancy - just some commands that might prove helpful in the long run.
node
Display global installation directory
1 | npm root -g |
If you installed node
using the official package installer, the global installation directory for npm
packages would be:
/usr/local/lib/node_modules
Or you could’ve installed it using Homebrew. In that case, the global installation directory would be:
/opt/homebrew/lib/node_modules
View global installation
1 | npm ls -g |
/opt/homebrew/lib
├── @e-hentai/home@1.6.0-alpha.9
├── express-generator@4.16.1
├── nodemon@2.0.22
└── npm@9.6.5
Update global installation
1 | npm update -g |
View global configuration
1 | npm config list |
The output should be on the lines of this:
; "builtin" config from /opt/homebrew/lib/node_modules/npm/npmrc
prefix = "/opt/homebrew"
; "user" config from /Users/Mccranky/.npmrc
fetch-retry-maxtimeout = 300000
; node bin location = /opt/homebrew/Cellar/node/20.0.0/bin/node
; node version = v20.0.0
; npm local prefix = /Users/Mccranky
; npm version = 9.6.5
; cwd = /Users/Mccranky
; HOME = /Users/Mccranky
; Run `npm config ls -l` to show all defaults.
From it, we can discern a few key instances that might prove helpful.
“builtin” config is stored at
/opt/homebrew/lib/node_modules/npm/npmrc
“user” config is stored at
/Users/Mccranky/.npmrc
Add/remove configuration
1 | npm config set <name> <value> |
For example, if I want to specify a range of time before timeout occurs, I can use the following command:
1 | npm config set fetch-retry-maxtimeout 300000 // 5 minutes |
The same mechanism applys when it comes to how we delete configurations:
1 | npm config delete fetch-retry-maxtimeout |
express
I’ve only just started dabbling with express
and my expertise is cut pretty thin, but the rule of thumb I tend to follow is:
If you’re installing something that you want to use in your program using
require('something')
, then install it locally, at the root of your project.If you’re installing something that you want to use on the command line or something, install it globally, so that its binaries end up in your
$PATH
environment variable.
Based on this, you would want to install express-generator
using the -g
flag as you will use it as a command line tool, but you’d want to install express
without this flag as it’s a module you will want to require()
it in your application.