One Tip a Week: Set some defaults for npm init -y

This week’s tip of the week is for npm. You can create a new project using npm init -y (see npm init docs on npm) to initialize a package.json with the default values.

Whenever I’ve run this in the past, my name and email were not part of the defaults. I naively though it’d pick up my git info for some reason. 🙃

That said, you probably have a global git configuration for your name and email. If you want to copy those as defaults for npm when you run npm init -y, you can run the following commands once.

npm config set init-author-name "$(git config user.name)" --global
npm config set init-author-email "$(git config user.email)" --global

You can also set defaults for the license and version number. Out of the box npm uses the ISC license and version 1.0.0. I wanted to default to MIT and 0.1.0, so I ran the following:

npm config set init-license MIT --global
npm config set init-version "0.1.0" --global

The next time you run npm init -y, boom! You have your desired defaults.

❯ npm init -y
Wrote to /Users/nicktaylor/package.json:

{
  "name": "cool-project",
  "version": "0.1.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "Nick Taylor <[email protected]>",
  "license": "MIT",
  "description": ""
}

That’s it! Short and sweet. Until the next one!