If you’ve been working on JavaScript and/or Node.js projects at any capacity these past few years, surely you’ve come across some package.json
files, npm’s configuration file for projects and modules. In this post we’ll explore some of the most important keys and values found in a typical package.json file.
The easiest and fastest way to initiate a project with the npm package manager is using the init
command and the -y
flag, which answers yes to all the questions:
$ npm init -y
The name of the project will be the same as the name of the current folder you’re in.
The init
command will write a package.json
file to the current directory with JSON content that looks like this:
{
"name": "hello-alligator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Let’s go over each of the keys in an initial package.json file:
scripts
key expects an object with script names as keys and commands as values. This is useful to specify scripts that can be run directly from the command line and that can do all sorts of things like starting your project on a local server, building for production or running your tests. Chances are that scripts
is where you’ll make the most manual changes in a typical package.json file.name
, email
and url
. It makes it easy for people to then get in touch with the project’s owner.MIT
would be another popular license choice. You can also use UNLICENSED
for projects that are private and closed-source.npm’s main strength is its ability to easily manage a project’s dependencies. It’s therefore only natural that the package.json file for a project centers mostly around specifying the dependencies for a project. There’s the regular dependencies, but there can also be devDependencies, peerDependencies, optionalDependencies and bundledDependencies. Let’s go over them:
$ npm install my-dependency
.--save-dev
flag with the install command.--save-optional
with the install command.--save-bundle
flag with the install command for a dependency to also be added to the list of bundled dependencies.As you’ve probably seen before, dependencies can accept different formats to specify which versions or range of versions can be used for a dependency. For example:
You can even specify multiple possible version ranges by separating each range with ||
.
There’s more configuration that can optionally go into your project’s package.json file, so let’s briefly touch on some of the most useful configurations:
main
for projects that are meant to be used in a browser instead of a server.true
, the project won’t be able to be published publicly to the npm repository. This is useful if you want to prevent accidentally publishing a project to the world.engines
to specify specific versions of Node.js and/or npm that the project works with. It takes an object with a key for node
and/or npm
and a value that looks like what you’d have as values for dependencies that specifies a range of versions.files
key is not provided a default value of ["*"]
is used, which means that all the files will be included. Not to worry though, certain files/folders such as .git
and nome_modules
are always ignored.With this, you should have a pretty good general idea of what can go into your package.json configuration. You can also reference the official documentation for fine grained details on all the possible configuration keys.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!