-
Notifications
You must be signed in to change notification settings - Fork 0
NPM
$NODE_PATH set
It's important when using NPM with Shan to have your $NODE_PATH environment variable set. One thing that Shan does is when installing a package or syncing your config, it checks if a package is already installed to avoid unnecessarily reinstalling. In most package managers, finding out if a package is already installed is very fast. NPM however is quite slow at this because it attempts to resolve the entire dependency tree first. Here's an example:
$ time npm list --depth=0 -g underscore
/home/user/.nvm/versions/node/v14.17.6/lib
└── underscore@1.13.1
npm list --depth=0 -g underscore 8.39s user 0.85s system 153% cpu 6.001 total
Even though I specified --depth=0, and underscore exists at the top-level, it still took NPM over 8 seconds to tell me if underscore existed on the system. Compare this to:
$ time npm install -g underscore
+ underscore@1.13.1
updated 1 package in 0.578s
npm install -g underscore 1.52s user 0.22s system 126% cpu 1.374 total
It took 5x less time to reinstall the package, than it took to list if it existed.
Now, with larger packages such as expo-cli, this will cause it to take more time. Reinstalling expo-cli will take upwards of 20 seconds on okay internet, as opposed to the somewhat constant 8 seconds to list. So this could cause significant slowdowns for some people - all the more reason to have $NODE_PATH set.
If you have $NODE_PATH set, all of this can be avoided. Instead of using npm to search the list of installed packages, we can simply do it ourselves by checking if a directory with a given name exists in our global node_modules directory. Doing it this way, we can avoid reinstalling packages by getting an instant response to whether or not the package is installed on our system.
So please, make sure $NODE_PATH is set correctly if you intend to use Shan for managing your global node modules.