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?
Oldest comments (14)
I want to mention two things for beginners out there like me.
npm init -yto skip all the questions in npm init--saveflag in theirnpm installquickstart or install guide. This had me confused at first. I wondered why won't they add a--saveflag 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
--saveor--save-devflag 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.
With the latest version of node, you can just do
npx mochainstead of having to add./node_modules/.bin/to yourPATH.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
npxfor 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.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.
As other folks mentioned,
npxships with npm as of 5.0, so you can justnpx jest(for example) instead of$(npm bin)/jestorPATH=$(npm bin):$PATH jest.Setting up your ~/.npmrc can save a lot of time with
npm init -y. The interesting keys are theinit.fooones. That section of my npmrc looks like:The
--silentflag fornpm runis 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 -diffto.gitattributes.npxis also the greatest for non-installed executables. Rather thannpm i -g create-react-appevery once in a while to keep it up to date, you can justnpx create-react-app fooand it'll install CRA temporarily and run it for you. (There's a whole list of things that work great withnpxhere, including keeping Node, npm, and npx up to date withnpx dist-upgrade).There's also the super-fast
npm ciin npm canary right now, which will save a lot of time on installs in the future.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 usenpx yo, however, there is an open issue for using it withnpx. 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/#...Now could use yonpx