DEV Community

Discussion on: npm tips/tricks

Collapse
 
zacanger profile image
Zac Anger

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
init.author.email=zac@zacanger.com
init.author.url=http://zacanger.com
init.license=MIT
init.version=0.0.1

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.