DEV Community

Nick Taylor
Nick Taylor Subscriber

Posted on • Updated on • Originally published at iamdeveloper.com

npm tips/tricks

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?

Top comments (14)

Collapse
 
peter_kuehne profile image
Peter Kühne

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

Collapse
 
nickytonline profile image
Nick Taylor

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?

Collapse
 
nickytonline profile image
Nick Taylor

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.

Thread Thread
 
nickytonline profile image
Nick Taylor

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.

Collapse
 
jjjjcccjjf profile image
endan • Edited

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!

Collapse
 
markadel profile image
Mark Adel

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

Collapse
 
jjjjcccjjf profile image
endan

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!

Thread Thread
 
nickytonline profile image
Nick Taylor

Maybe you were using an npm version lower than 5

Collapse
 
nickytonline profile image
Nick Taylor

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

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.

Collapse
 
javver profile image
Javier Cardona

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.

Collapse
 
paulreiber profile image
Paul Reiber

Why treat Yarn and Yeoman as special cases?

Collapse
 
nickytonline profile image
Nick Taylor • Edited

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/#...

Collapse
 
hillliu profile image
Hill Liu

Now could use yonpx

npx yonpx [your-generator] your-app-name
Enter fullscreen mode Exit fullscreen mode