As a general rule, any project that's using Node.js will need to have a package.json
file. What is a package.json
file?
At its simplest, a package.json
file can be described as a manifest of your project that includes the packages and applications it depends on, information about its unique source control, and specific metadata like the project's name, description, and author.
Let's break down the core parts of a typical package.json
file:
Specific Metadata: name, version, description, license, and keywords
Inside a package.json, you'll almost always find metadata specific to the project - no matter if it's a web application, Node.js module, or even just a plain JavaScirpt library. This metadata helps identify the project and acts as a baseline for users and contributors to get information about the project.
Here's an example of how these fields would look in a package.json
file:
{
"name": "metaverse", // The name of your project
"version": "0.92.12", // The version of your project
"description": "The Metaverse virtual reality. The final outcome of all virtual worlds, augmented reality, and the Internet.", // The description of your project
"main": "index.js"
"license": "MIT" // The license of your project
}
A package.json
file is always structured in the JSON
format, which allows it to be easily read as metadata and parsed by machines.
If needing to format a package.json
file manually to get your project up and running seems a bit daunting, there's a handy command that will automatically generate a base package.json
file for you - if you'd like to learn how to use it, take a peek at the npm init
instructions below!
Understanding and Managing Your Project's Dependencies: dependencies and devDepenendcies
in your `package.json
`
The other majorly important aspect of a package.json
is that it contains a collection of any given project's dependencies. These dependencies are the modules that the project relies on to function properly.
Having dependencies in your project's package.json
allows the project to install the versions of the modules it depends on. By running an install command (see the instructions fornpm install
below) inside of a project, you can install all of the dependencies that are listed in the project's package.json
- meaning they don't have to be (and almost never should be) bundled with the project itself.
Second, it allows the separation of dependencies that are needed for production and dependencies that are needed for development. In production, you're likely not going to need a tool to watch your CSS files for changes and refresh the app when they change. But in both production and development, you'll want to have the modules that enable what you're trying to accomplish with your project - things like your web framework, API tools, and code utilities.
What would a project'spackage.json
look like with dependencies and devDependencies
? Let's expand on the previous example of a package.json
to include some.
`
{
"name": "metaverse",
"version": "0.92.12",
"description": "The Metaverse virtual reality. The final outcome of all virtual worlds, augmented reality, and the Internet.",
"main": "index.js"
"license": "MIT",
"devDependencies": {
"mocha": "~3.1",
"native-hello-world": "^1.0.0",
"should": "~3.3",
"sinon": "~1.9"
},
"dependencies": {
"fill-keys": "^1.0.2",
"module-not-found-error": "^1.0.0",
"resolve": "~1.1.7"
}
}
`
One key difference between the dependencies and the other common parts of a package.json
is that they're both objects, with multiple key/value pairs. Every key in both dependencies and devDependencies
is a name of a package, and every value is the version range that's acceptable to install.
Top comments (0)