What is package.json in Javascript?
package.json
is a configuration file containing JSON code used by Node and NPM (Node's Package Manager). Node is an environment for running Javascript directly on your PC (or on a web server) instead of inside a web browser's sandbox.
In a project that uses Node, the package.json
file contains information about the project itself, such as a list of its dependencies that need to be installed via NPM in order for the project to work. Then you can just run npm install
in the terminal to install all the dependencies.
The package.json
file also contains author information for public packages distributed via NPM (e.g. open source libraries). If you have a private project, you don't need to fill the author information, but you still can use package.json
to keep track of your dependencies.
Running npm install some-library --save
will save the dependency to your package.json
automatically (if you use Node as a web server), and --save-dev
saves a "development" dependency (e.g. if you use Grunt, Webpack, or another task runner or bundler that generates static CSS and Javascript files from Sass and Typescript).
Example of package.json
For reference, an example of package.json
.
{
"name": "my-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "grunt my-build-task",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"grunt": "^1.6.1",
"grunt-contrib-copy": "^1.0.0",
"grunt-exec": "^3.0.0",
}
}
Note: index.js
would be the entry point for Node's web server (similar to main.c
). If you don't use Node as a web server, you don't need an index.js
file in your project at all. If you run npm init
and just press the Enter key every time use default values and create default package.json
, it automatically adds this main = index.js
key-value pair.
Leave a Reply