The npm init command in the JSON language creates a package.json file for your project’s frontend. A package.json file is a file that contains information about the project’s packages and dependencies. It also contains metadata for the project such as version number, author, and description.
Using npm commands occurs through the command line tool and init is just one of many commands npm responds to. In this introduction, we will learn what npm is, what a package.json file is, and what the npm init command does. By the end of this guide, you’ll have a clearer picture of how to use the npm init command.
What is npm?
Built as a package manager and installer, npm (Node Package Manager) is also the world’s largest code library, containing over 800,000 code packages which can be installed through the command line. Npm itself is installed through Node.js. To use npm in the command line, you must have Node.js installed.
Npm also acts as a dependency manager. A dependency is a library your project relies on to function properly. React is an example of a dependency. These are kept track of in the package.json file mentioned in the introduction.
Let’s take a look at how to use the npm init command.
How to Use npm init
The command init is short for initialize. Before building a new project, we need to specify some of the project’s attributes. These attributes include the project’s name, initial version, and description. There are more attributes such as the project’s main file, github repository, keywords, and license information, but the name and version number are the most important here.
Using npm init is straightforward. First make sure to have Node.js installed, as stated in the previous section. When npm is installed, using the init command in the command line tool will initialize your project.
npm init
This will begin the initialization process. During the process, you will be prompted to provide the above information about your project. After completing the prompts, a package.json file will be created in your current directory. A best practice is to run this command while in the root directory of your project.
A package.json file is a file containing the above information about your project. It must be written in JSON. Structuring the package file in this way makes it easy to read by the developers and easy for the computers to parse the information.
An example package.json file may look like this:
{ "name": "storefront-frontend", "version": "0.1.0", "private": true, "dependencies": { "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.3.2", "@testing-library/user-event": "^7.1.2", "react": "^16.12.0", "react-dom": "^16.12.0", "react-scripts": "3.4.0" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }, "eslintConfig": { "extends": "react-app" }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] } }
Here, we can see that only the name and version number are included from the list of prompts. These are the only ones required, but in general, providing more information is considered a best practice. Then we see the list of dependencies this particular project needs to run.
The structure of the dependency is a nested JSON object with key/value pairs. The keys are the name of the dependency and the values point to their respective version numbers. In this example, there are different testing packages as well as packages for React.
Since this frontend is built with React, the project needs to know that it depends on React to function. And with the React packages listed in the package.json file, they can be installed from the command line as well.
Conclusion
Using npm init from the command line initializes the project’s package.json file. In this file is information on the project itself such as name and version number. Listed alongside this information are the project’s dependencies.
Dependencies are packages the project requires to function properly. Listing them in the package.json file allows them to be installed before building the project. The package.json file generated from calling npm init must be written in JSON format.
Now that you’ve had an introduction to using npm init and what a package.json file is, try initializing your next project by using npm init.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.