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.
npm init -y
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
@PaiNishant @nodejs if you run npm@5 or later you can drop the --save – it's now automatic :)
22:39 PM - 02 Jan 2019
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.
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:
The --silent flag for npm run is nice. I have an alias: nr='npm run --silent'.
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).
npm i -g create-react-app
npx create-react-app foo
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.
Why treat Yarn and Yeoman as special cases?
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/#...
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.