A package.json is a JSON file located in the root of a JS project that holds relevant metadata for the project and manages a project’s dependencies, scripts, version, etc.
There are two ways to create a package.json file using node
and yarn
.
If you have the latest version of node installed, run the following command to generate:
npm init
You can also add default values to the file by running:
npm init -y
To manually generate the package.json file, go to the root directory for the project and create a package.json file.
You then need to add the following code to the file filled with your values:
{
"name": "",
"version": ""
}
Name represents the names of the project. The name must be:
The version denotes the current version of the modules. It must follow the semantic versioning guidelines, e.g., 1.0.0
The description provides more information about the project.
The engine property is an object
used to specify the library versions required for the application to run.
Dependencies denote the list of modules and packages required for the application to function. It is added to the dependency list when installed.
devDependencies are modules that are not required to run the application but are needed in the development phase.
You can run these with npm i package --save-dev
.
Scripts are key-value pairs used to perform a series of tasks such as building, testing, etc.
You can run these with npm run scriptname
.
Main is the entry point of the application and should refer to the main page of your app.
Homepage is used to specify the landing page of the app.
If the private property is set to true, the application cannot be published.
Valid licenses are added here.
Author names are added here.
Repository is a key-value pair that contains the version control system used to manage the app.
You can specify the:
*Link to the page where issues can be reported.
An array of keywords that help to identify your project.
Custom properties can be used to specify commands that can be found in the package documentation.
Here is an example that contains all of the properties stated above.
{"name": "package.json-educative","version": "1.0.0","description": "Master file of package.json","private": true,"main": "index.js","scripts": {"start": "node index","dev": "nodemon index"},"repository": {"type": "git","url": "git+https://github.com/Educative/package.json-educative.git"},"keywords": ["node","package.json","javascript","npm"],"author": "Fahad Farid","license": "Standard","homepage": "https://github.com/Educative/package.json-educative#readme","engines": {"npm": "6.10.4","node": "10.14.3"},"dependencies": {},"devDependencies": {}}