To create a package.json file, run the npm init
command. This command will prompt you with a series of questions that you need to fill out or accept the default values. After this, a package.json file is created in the directory.
Following is a detailed explanation of each step that you should take in order to create a package.json file:
1. Create a folder in which you keep all your project files.
$mkdir project-name
2. In the command prompt or terminal, run the npm init
command. The output of this command will look like this:
$npm init 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` 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: (chatting-app)
3. This command will prompt you to provide input for different fields in package.json.
You can fill out the values in two ways:
Let's see what answers you should provide to the questions prompted by the npm init
command.
a. package name: Both project name and package name are the same. Here, you enter the name you want to keep for your project. By default, it takes the folder name as the package name. The name field in the package.json file points to the package name.
b. version: It defines the current version of the package. 1.0.0 is the default value of the version field.
c. description: You provide a short summary of the project. If you accept the default, nothing will appear in the description field.
d. entry point: Here, you enter the project's main file. It refers to the main field in the package.json file.
e. test command: The test command you want to trigger is entered in this prompt.
f. git repository: If you plan to make your project public, then provide the URL of the source code.
g. keywords: Keywords are like tags that are associated with the project.
h. author: The creator's name, email, and url are mentioned in the author field.
i. license: The license defines how you want to share your code with other people. Here, you can enter ISC, MIT, Apache-2.0, etc. The default is ISC.
To learn more about the different fields in package.json, visit this link.
Note: If you are tired of hitting the enter key again and again to accept the defaults, then run the npm init -y command to generate a package.json file and automatically accept all the default values.
You might be wondering from where NPM picks the default values. Actually, the npm init
command infers the default values from the folder in which you run. For example, the default package name is the name of the folder.
The following example shows what the user entered into the prompt after running the npm init
command.
package name: (chatting-app) version: (1.0.0) 1.2.0 description: Chatting app using Socket.io entry point: (index.js) test command: git repository: https://www.github.com/tutorialsandyou keywords: author: Mohit Natani license: (ISC)
4. After answering the questions, a preview of package.json file is shown. To accept the preview, hit the enter key or type no
to reject.
Accepting results in the creation of package.json file in the directory in which you are currently.
The generated package.json file looks like this:
{ "name": "chatting-app", "version": "1.2.0", "description": "Chatting app using Socket.io", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "https://www.github.com/tutorialsandyou" }, "author": "Mohit Natani", "license": "ISC" }
5. Now, you can install dependencies for your project by running the npm install package-name
command.
$npm install package-name