Using npm init with -y flag explained

The npm init command is used from the command line to transform your current folder into a new npm-based JavaScript project.

The command will generate a package.json file that provides the following information for your project:

  • Metadata for the project such as project title, description, license, author, keywords, etc.
  • Special npm script command to run, assigned to the scripts JSON property

Once you initialize a package.json file, any dependencies you added to the project will also be recorded when you run the npm install command successfully.

By default, the init command will ask you a bunch of questions with sensible defaults.

To try out the init command, you can create a new empty folder and execute the command inside that folder. In the following example, I will name my empty folder npm-init-test.

Here’s the result of me running the init command:

$ mkdir npm-init-test && cd npm-init-test

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 init` 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.
package name: (npm-init-test) 
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /Users/nsebhastian/npm-init-test/package.json:

{
  "name": "npm-init-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes)

As you can see from the command line result above, the npm init command offers a sensible default but still asks you a bunch of questions for any additional informations you’d like to add.

The -y or --yes flag can be used to skip the questionnaire altogether and let npm fills the package.json file with the sensible default obtained from the current project folder.

Here’s the result of npm init -y from the same folder again:

$ npm init -y

Wrote to /Users/nsebhastian/npm-init-test/package.json:

{
  "name": "npm-init-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

The package.json file will be generated automatically with the contents above without asking for your confirmation.

If your current folder a local Git project or repository, the init command will also add the Git information to your project.

For example, let’s say I add the https://github.com/nsebhastian/test.git as the remote origin of the npm-init-test project:

$ git init 
Initialized empty Git repository in /Users/nsebhastian/npm-init-test/.git/
$ git remote add origin https://github.com/nsebhastian/test.git
$ npm init -y

Wrote to /Users/nsebhastian/npm-init-test/package.json:

{
  "name": "npm-init-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/nsebhastian/test.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/nsebhastian/test/issues"
  },
  "homepage": "https://github.com/nsebhastian/test#readme"
}

As you can see in the above snapshot, the Git remote origin URL I added to the project is recorded to the generated package.json file along with other metadata information.

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.