w3resource

Yarn Init Command: Create and Customize your package.json easily


The yarn init command is a command used to create or update a package.json file interactively.

yarn init

when you run this command from the terminal, it will walk you through an interactive session to create a package.json file. some of the defaults such as the license and initial version are found in the init-* config settings of yarn.

Here is an example of running the yarn init in a directory named movieapp:

yarn init v1.21.1
question name (movieapp):
question version (1.0.0):
question description: This is a sample movie application built with node
question entry point (index.js): app.js
question repository url:
question author: Aryan
question license (MIT):
question private: n
success Saved package.json
Done in 37.31s.

This will result in a package.json file with the following content:

{
  "name": "movieapp",
  "version": "1.0.0",
  "description": "This is a sample movie application built with node",
  "main": "app.js",
  "author": "Aryan ",
  "license": "MIT",
  "private": false
}

In the case where you already have an existing package.json file, yarn init will use the file's entries as defaults.

Given a package.json with the following package.json entries:

{
  "name": "movieapp",
  "version": "1.0.0",
  "description": "I was here",
  "main": "index.js",
  "author": "Awam ",
  "license": "MIT",
  "private": false
}

Running the yarn init command will yield the following defaults:

yarn init v1.21.1
question name (movieapp):
question version (1.0.0):
question description (I was here):
question entry point (index.js):
question repository url:
question author (Awam ):
question license (MIT):
question private:
success Saved package.json
Done in 15.07s.

Setting defaults for yarn init

You can use the following config variables to customize the defaults for yarn init.

  • init-author-name
  • init-author-email
  • init-author-url
  • init-version
  • init-license

yarn init --yes/-y

whenever you run this command, it will skip the interactive session and then generate a package.json based on your defaults.

Here is an example output from running yarn init:

yarn init v1.21.1
warning The yes flag has been set. This automatically answers yes to all questions, there may be security implications.
success Saved package.json
Done in 0.27s.

yarn init --private

Running the yarn init with the --private flag will set the private option to true.

As shown in below:

yarn init v1.21.1
question name (movieapp):
question version (1.0.0):
question description (I was here):
question entry point (index.js):
question repository url:
question author (Awam ):
question license (MIT):
question private (true):
success Saved package.json
Done in 8.24s.

The package.json file for the above output will be:

{
  "name": "movieapp",
  "version": "1.0.0",
  "description": "I was here",
  "main": "index.js",
  "author": "Awam ",
  "license": "MIT",
  "private": true
}

You can use both the private and yes flags at the same time. All you have to do is run:

yarn init -yp

This will automatically answer yes to all questions.

Previous: Mastering the Yarn Info command.
Next: Yarn Install commands.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/yarn/yarn-init-cli-command.php