How to customize your npm init experience

Today I learnt how to tailor my npm init experience.

If you find yourself often creating package.json files using the npm init command, there is a chance you would be pleased to know you can customize this process to your preferences and liking.

Creating an .npm-init.js file

Here is a straight forward way to do so:

First off, create a file called .npm-init.js in your home directory.

Then edit this file, cherry picking from the below example, and adapting to your liking and save it.

const author = 'Captain Anonymous ';

module.exports = {
	name: prompt('What awesome name should this module carry', 'legend'),
	version: '0.1.0',
	author: author,
	license: 'MIT',
	customField: 'Example custom field',
	otherCustomField: 'This example field is really cool'

In the above example, we basically export the properties that will make up our desired package.json file.

The prompt function is used to prompt the user with a question and its default answer. Any other properties from the example will end up to be set with the value, or the value of the variable, it is assigned with.

Now, whenever you will use the npm init command, you will get a much better experience tailored to your need.

Custom npm init experience output

Below you can find the output generated from the npm init command, using the .npm-init.js file from this blog post.

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
What awesome name should this module carry: (legend)
About to write to /Users/captainanonymous/package.json:

  "name": "legend",
  "version": "0.1.0",
  "author": "Captain Anonymous",
  "license": "MIT",
  "customField": "Example custom field",
  "otherCustomField": "This example field is really cool"

Is this OK? (yes)

Reference: Official npm documentation (Running a CLI questionnaire)