npm tips/tricks

twitter logo github logo Updated on ・1 min read

I almost never install packages globally unless it’s something like yarn or yeoman. When you’re in the root of your Node/JS project, if you want to run something like mocha without an npm script, you’d do ./node_modules/.bin/mocha.

What I do to be able to just run e.g. mocha is I add ./node_modules/.bin to my path in my shell’s profile/config file and whenever I’m in any of my Node/JS projects, I can just run e.g. mocha.

Have any npm tips/tricks you want to share?

twitter logo DISCUSS (13)
markdown guide

With the latest version of node, you can just do npx mocha instead of having to add ./node_modules/.bin/ to your PATH.


Nice! I had heard of npx, but have not read up on it yet. Guess I don't need to hack my path anymore ;). npx is since npm 5.0?


I think the lazy in me still likes writing it sans npx ;).

I found the Medium article introducing npx for anyone who stumbles across this discussion, Introducing npx: an npm package runner.

I see you can do other cool stuff with it though like npm https://gist.github.com/zkat/4bc19503fe9e9309e2bfaa2c58074d32. I'm gonna take it for a spin.


I want to mention two things for beginners out there like me.

  • Use npm init -y to skip all the questions in npm init
  • Some NPM package documentations don't explicitly mention to use --save flag in their npm install quickstart or install guide. This had me confused at first. I wondered why won't they add a --save flag because if you upload your application it would break because of missing dependencies. The dev said it's up to you if you want to use their package as a dependency or a dev dependency. But obviously, the package they are offering should be saved as a core dependency (and so he modified the docs).

TLDR: Some documentation don't mention the --save or --save-dev flag in their installation. Don't be afraid to add either flags!


Also npm version 5 saves the package in package.json by default, If you don’t want to save, you can override the default saving with --no-save


Oh.. that's a neat addition. Last time I checked, it didn't save in my package.json. Maybe I have a lower version of npm. Thanks for this! Really useful!

Maybe you were using an npm version lower than 5


I think like package-lock.json, this was another nice feature of yarn they implemented.


As other folks mentioned, npx ships with npm as of 5.0, so you can just npx jest (for example) instead of $(npm bin)/jest or PATH=$(npm bin):$PATH jest.

Setting up your ~/.npmrc can save a lot of time with npm init -y. The interesting keys are the init.foo ones. That section of my npmrc looks like:

init.author.name=Zac Anger

The --silent flag for npm run is nice. I have an alias: nr='npm run --silent'.

package-lock.json in 5.0 is the new version of npm-shrinkwrap.json, which is more like yarn.lock (but more deterministic, since it's a snapshot of where modules are on your actual fs). It's also huge and diffs in it are hard to read (and shouldn't be manually edited anyway), so I add package-lock.json -diff to .gitattributes.

npx is also the greatest for non-installed executables. Rather than npm i -g create-react-app every once in a while to keep it up to date, you can just npx create-react-app foo and it'll install CRA temporarily and run it for you. (There's a whole list of things that work great with npx here, including keeping Node, npm, and npx up to date with npx dist-upgrade).

There's also the super-fast npm ci in npm canary right now, which will save a lot of time on installs in the future.


There is also npx for that same purpose, with the added benefit that if the package is not in your local .bin directory, it will install it as well.


Well before I discovered npx, I've never needed Yeoman in a project. It was always to generate something for a project which is why I installed it globally, . But now, I'd use npx yo, however, there is an open issue for using it with npx. For yarn, CI has this installed normally, e.g. Travis, and yarn's docs recommend installing it for global use as well, yarnpkg.com/lang/en/docs/install/#...

Classic DEV Post from Sep 23 '19

How to Use Git to Manage Your Writing Project

In this tutorial you’ll use Git to manage a small Markdown document. You’ll store an initial version, commit it, make changes, view the difference between those changes, and review the previous version.

Nick Taylor (he/him) profile image
Just some dev at DEV. Big fan of TypeScript, (P)React JavaScript and Node. C#/asp.net once upon a time.

Hey there reader...

Do you prefer sans serif over serif?

You can change your font preferences in the "misc" section of your settings. ❤️