In How to use Package Management In Javascript projects we discussed project specific node package management configuration options and also introduced the package.json
file and how it is used as the configuration management repository.
In this, we will take a deeper dive into the package.json
and explore it further.
The package.json
is one of the core components of the Node runtime environment, and it typically included in the
project root directory of all node based projects. It is a plain text JSON file which contains the basic metadata,
dependencies, configuration and build information about the project.
The package.json
file:
package.json
It is possible to create a package.json
manually by creating by simply JSON format text file and adding the required
fields and whichever other fields you desire.
There are only two mandatory directives that all package.json
must contain:
The below is an example of the simplest package.json
file possible
{
"name": "simple-package",
"version": "1.0.0"
}
The most common and popular why of creating a package.json
file is by making use of a package management tool. Two of
the most popular and common package management tools available are npm and yarn.
### Create a package.json using npm
npm init
### Create a package.json using yarn
yarn init
When creating a package.json
file with a package management tool, typically a questionaire will be launched to ask
you to provide some additional information about your project.
The information obtained when then be included in optional directives to help others who will find your project to
understand more about your project by providing some additional information like the author
, contributors
, homepage
and repository
fields.
package.json
filesfor discovery. You typically provide an array of strings to be used in the packages listing on npm search
either providing it as an JSON object format i.e.
"author" : { "name" : "threenine.co.uk",
"email" : "hello@threenine.co.uk",
"url" : "https://threenine.co.uk"
}
or by providing it in a shorthand format on a single string
"author" : "threenine.co.uk <hello@threenine.co.uk> (https://threenine.co.uk)"
formatting rules as authors apply however by supplying an array of people.
"contributors" : [ { "name" : "Gary Woodfine",
"email" : "hello@threenine.co.uk",
"url" : "https://garywoodfine.com"
},
{"Joe Blogs <hello@blogs.com> (https://blogs.com)"}
placed upon it.
{
"name": "simple-package",
"version": "1.0.0",
"description": "An short description describing simple-package",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"Amazing Software",
"innovative solution",
"Technology Education",
"JavaScript Tutorials"
],
"author": "geekiam.io <hello@geekiam.io> (https://geekiam.io)",
"license": "ISC"
}
There are many more directives you can include to supply even more information regarding your project. The more information you provide increases the likelihood of your project being found and even used by others.
dependency. If you omit the field then the default of including all the files in in your directory will be applied.
Alternatively, you can provide an additional .npmignore
file in the root of your directory or in sub-directories
detailing which files and directories to exclude.
The .npmignore
file works just like a .gitignore
. If there is a .gitignore
file, and .npmignore
is missing,
.gitignore
contents will be used instead.
only people who want to contribute to your project but also for people who want to analyse and understand your project before making use of it in theirs.
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/threenine/geekiam.git"
},
"bugs": {
"url": "https://github.com/threenine/gridsome-plugin-amazon-onelink/issues"
},
could be executed to build, deploy, test or run your application.
These may be terminal commands, which are put into the scripts field so other developers can document and use them.
Scripts are powerful tools that the npm CLI can use to run tasks for your project. They can do the job of most task runners used during development.
A typical node application or project may make use of a number of third party modules or applications to extend different levels of functionality.
Modules can be used in various areas of your application and bundled into or even excluded in various builds. It is possible to only require some modules for when you're developing an application i.e. Unit Testing Framework, Mocking Framework, Linting, code formatting etc.
The package.json
enables you to define a number of different levels of dependencies and how to add them. We'll take
a brief look at the different types of dependencies we can add to a node project.
mandatory packages required to run your application and modules used throughout your project.
You can add production dependencies using any of the following commands
### Using npm
npm install package_name
## or
npm i package_name
## or
npm i package_name --save-prod
### using yarn
yarn add package_name
## or
yarn add package_name --production
## or
yarn add package_name -P
Any of these commands will result in an entry being added to the dependencies section in your package.json
"dependencies": {
"package_name": "^1.0.0"
}
## Using npm
npm i package_name --save-dev
## or
npm i package_name -D
## using yarn
yarn add package_name --dev
## or
yarn add package_name -D
Any of these commands will result in an entry being added to the devdependencies section in your package.json
"devdependencies": {
"package_name": "^1.0.0"
}
included. These may be dependencies that provide additional non-breaking functionality to other dependencies.
# uisng npm
npm i package_name --save-optional
## or
npm i package_name -O
# using yarn
yarn add package_name --optional
## or
yarn add package_name -O
Any of these commands will result in an entry being added to the optionalDependencies section in your package.json
"optionalDependencies": {
"package_name": "^1.0.0"
}
project you are developing for will have packages/modules being used in your project.
You won't install that packages but rather just list them, running npm install
on your package will not actually install the packages.
"peerDependencies": {
"package_name": "^1.0.0"
}
certain or all the dependencies bundled together in a single file then you can specify these dependencies under the
key bundledDependencies
in your package.json
file.
To create a single tarball of all the dependencies mentioned in bundledDependencies
you issue the command: npm pack
which will create a .tgz file with name like: bundled_packages.tgz
.
Users can simply issue the command npm install
name_of_the_project-bundled_packages.tgz
to install the project
bundled dependencies using a single file.
This approach is used to preserve dependencies and make them available using a single file.
"bundledDependencies": [
'express','request'
]
The package.json
file may at first seem to be nothing more than a simple JSON file and often new developers tend to
skip learning its importance and how it functions.
The package managers often hide a lot of the complexity and functionality contained within the package.json
.
It is worth taking the time and effort to read nodejs package.json guide the official guide to the package.json file.