Regular: npm install pkg, Shorthand: npm i pkg.
Regular: npm i --global pkg, Shorthand: npm i -g pkg.
Regular: npm i --save pkg, Shorthand: npm i -S pkg.
Regular: npm i --save-dev pkg, Shorthand: npm i -D pkg.
Other Shorthand Commands: https://docs.npmjs.com/misc/config#shorthands-and-other-cli-niceties
We all know npm init, it’s the first thing we do when creating a new package.
But, all those questions are quite annoying and we gonna modify it anyway, so why not just avoid it?
npm init -y and npm init -f to the rescue!
We get to a new project and we wonder how to get started. We usually ask ourselves things like: how do we run it? which scripts are available?
One way to discover is to open the package.json file and check the scripts section.
We can do better of course, so we simply run npm run and get a list of all the available scripts.
Additional option is to install ntl (npm i -g ntl), and then run ntl in the project’s folder. It also allows to run the scripts, which makes it very convenient.
Similar to available scripts, sometimes we ask ourselves which dependencies we have in our project.
We can once again open the package.json file and check, but we already know we can do better.
Meet npm ls --depth 0.
To list the globally-installed packages, we can run the same with -g flag, npm ls -g --depth 0.
You might came across the repository entry in the package.json file and wondered: “What is it good for?”.
To answer it, simply run npm repo and watch it open in your browser.
Same applies, by the way, for the npm home command and the homepage entry.
If you want to open your package page on npmjs.com, there’s a nice shorthand for that as well, npm docs.
You’re probably familiar with scripts such as pretest, which allows you to define code that would run before the test script.
What you might be surprised to find out, is that you can have pre and post scripts for every script, including your own custom scripts!
It’s very useful for projects in which you use npm as your build tool and have many scripts you need to orchestrate. Ex:
{
"name": "hooks",
"version": "1.0.0",
"description": "Example",
"main": "index.js",
"scripts": {
"precustom": "echo 'Custom pre-script example.'",
"custom": "echo 'Custom script.'",
"postcustom": "echo 'Custom post-script example.'"
},
"author": "Joey Burzynski <[email protected]> (https://github.com/JoeyBurzynski)",
"license": "ISC"
}
$ npm run custom
Output:
Custom pre-script example.
Custom script.
Custom post-script example.
You have a package, you use semver for versioning, and you need to bump the version before a new release.
One way to do this is to open the package.json file and change the version manually, but we’re not here for that.
An easier way is to run npm version with major, minor or patch. Ex:
$ npm version major
v2.0.0
$ npm version minor
v2.1.0
$ npm version patch
v2.1.1
Modules are stored in the home directory, but you can't use this offline because it still hits the network.
npm install --cache-min 999999
npm install --offline
Deploy to production EXACTLY what you used in dev. Locks every version of every package and all dependencies in node_modules
npm shrinkwrap
Examples:
npm test
npm start
npm run $anything
Run scripts are composable
"db:migrate": "knex migrate:latest"
"db:seed": "knex seed:run"
"db:reset": "npm run db:migrate && npm run db:seed"
npm i @scope/name
Scoped packages are private by default but you can make them public:
npm publish --access=public
Use your private modules side by side with modules from the public registry
npm init --scope=username
Ex: npm init --scope=joeyburzynski
Create teams and add members, grant and revoke access to packages via scopes
npm team
npm login --registry=http://reg.example.com