Package.json files are configuration files that specify the options and dependencies of a project. They are generated when a new project file is created, often using yarn init. When the source files are run on a different environment, they are expected to give the same results, based on the dependencies and versions that are in use. The package.json below is one example created during a sample project.
{
"name": "backend",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"type": "module",
"scripts": {
"dev": "nodemon index.js"
},
"dependencies": {
"axios": "^0.27.2",
"cheerio": "^1.0.0-rc.10",
"coolsms-node-sdk": "^2.0.1",
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"mongoose": "^6.3.3",
"nodemailer": "^6.7.5",
"nodemon": "^2.0.16",
"swagger-jsdoc": "^6.2.1",
"swagger-ui-express": "^4.4.0"
}
}
The main field specifies the entry point. Often, this refers to the index.js file, but when named with a different file name, this can cause errors. The type field indicates that this project will utilize the module instead of commonjs. This is relevant to how imports and exports are written (commonjs uses the require keyword whereas the module uses import and export keywords). The scripts dev simplifies project run commands by equating yarn dev or npm run dev to nodemon index.js. Instead of globally installing nodemon (like the case with node), nodemon is ran by the use of scripts (because nodemon was not installed globally, the computer cannot recognize the nodemon command). The nodemon tool saves a lot of time, and unnecessary, repetitive calls to node index.js.
The dependencies list show the full list of tools, or packages used in running the program. The node modules directory can be formed based on these specifications (especially useful because node modules file is relatively very heavy and thus cannot be copied and uploaded easily).