Summary
To create a package.json file, enter the following command in your terminal:
npm init
To create a package.json file and skip the questions, enter the following command in your terminal:
npm init -y
Details
A package.json file contains a list of npm packages that you’ve installed for your project. It also allows you to specify the version of each package you’re using. If other developers clone your project, they can use npm install
to download and install all of the packages listed in your package.json file. If you commit this file to your Git repository, you don’t have to commit the packages you’re using to your repo.
To create a package.json file, enter the following command in your terminal:
npm init
After entering this command, you will be asked several questions to fill out some basic information listed in your package.json file. You can answer the questions or hit enter to accept the default values. You can always update the answers later by editing the package.json file directly.
If you want to skip the questions and automatically accept the default values, enter the following command in your terminal:
npm init -y
A default package.json file will look similar to the following:
{
"name": "package-json-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "",
"license": "ISC"
}
Exercises
Run npm init
or npm init -y
in your terminal to create a package.json
file.