Skip to content

Instantly share code, notes, and snippets.

@JoeyBurzynski
Created February 20, 2018 08:29
Show Gist options
  • Select an option

  • Save JoeyBurzynski/64cae3f9d36592b8a0efb10642686ca8 to your computer and use it in GitHub Desktop.

Select an option

Save JoeyBurzynski/64cae3f9d36592b8a0efb10642686ca8 to your computer and use it in GitHub Desktop.
NPM: Tips & Tricks (02.20.2018)

NPM Tips & Tricks (02.20.2018)

Basics

Installing a package:

Regular: npm install pkg, Shorthand: npm i pkg.

Installing a package globally:

Regular: npm i --global pkg, Shorthand: npm i -g pkg.

Installing a package and save it as a dependency:

Regular: npm i --save pkg, Shorthand: npm i -S pkg.

Installing a package and save it as a devDependency:

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

Initializing a new package

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!

List available scripts

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.

List installed packages

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.

Find your package on the internet

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.

Run scripts before and after other scripts

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.

Bumping package’s version

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

npm... global cache? (.npm/)

Modules are stored in the home directory, but you can't use this offline because it still hits the network.

Install packages for offline use

npm install --cache-min 999999

We should probably just alias this to:

npm install --offline

Lock down your dependencies

Deploy to production EXACTLY what you used in dev. Locks every version of every package and all dependencies in node_modules

npm shrinkwrap

Run scripts: Automate CLI workflows, e.g. build steps, migrations

Examples:

npm test npm start npm run $anything

Lifecycle events (publish, install, uninstall, version, test, stop, start, restart, pre, post)

Run scripts are composable

"db:migrate": "knex migrate:latest"
"db:seed": "knex seed:run"
"db:reset": "npm run db:migrate && npm run db:seed"

Scoped packages (fancy way of saying namespaced packages!)

npm i @scope/name

Scoped packages are private by default but you can make them public:

npm publish --access=public

npm private modules

Use your private modules side by side with modules from the public registry

npm init --scope=username

Ex: npm init --scope=joeyburzynski

npm organizations

Create teams and add members, grant and revoke access to packages via scopes

npm team

npm on-site: run your own on-premises npm registry

npm login --registry=http://reg.example.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment