~ 2 min
Quick reference of some useful commands
If you’ve been using nodeJS npm package manager, chances are, sooner or later you will get conflicts within your dependencies. Maybe you’re cloning an old project or maybe you just updated your environment and your
node_modules folder must be regenerated.
Sometimes after running
npm install you don’t get exactly the modules that you need and suddenly things don’t work as expected anymore.
This might happen when there’s no specific version defined of some dependencies in your
package.json, but rather an approximate one (e.g.: using a tilde, as in
~1.2.3 or a caret, as in
While tildes and carets can be useful if you want to automatically use more recent versions when available, they can sometimes break your project if anything new isn’t compatible with your current code.
By just specifying a version number without carets and tildes (as in
1.2.3), you fix the specific version of each dependency you need.
Lock down dependency versions
Sometimes using specific module versions is not enough. Saving a list with the specific versions of all your dependencies might save your day.
Another (even better) alternative is to use Yarn to create a
yarn.lock file, which has the same goal as the
npm-shrinkwrap.json, but it seems to follow a more deterministic approach, being therefore safer.
Yarn is compatible with the same
package.json of npm, so you can just install it globally (
npm install -g yarn) and try it out right away by simply running
yarn on your project’s root folder, which will generate the
It is a very good idea to add this
yarn.lock to your version control.
Remove unneeded dependencies
If you want to remove modules from your
node_modules directory that you once installed, but that are not required anymore by your project, you can use prune:
npm prune --production will also remove the devDependencies.
This is particularly useful before running
npm shrinkwrap if you manually removed dependencies from your
List outdated dependencies
If you want to check out which dependencies within your project are outdated, you can use the npm-outdated command:
With the module
npm-check-updates, you can automatically update your dependencies to the latest versions (be careful when doing this, since modifying various depencencies at once might break things down).
npm i -g npm-check-updatesInstall the npm-check-updates module globally
npm-check-updates -uthe option -u upgrades your
npm installin order to install the new versions of the dependencies (you might need to remove the
Some other useful commands
npm lslist the installed packages
npm dedupereduce duplication