0% found this document useful (0 votes)
0 views

The Basics of Package.json - NodeSource

The document provides an introduction to the package.json file, which is essential for understanding and working with Node.js and npm. It outlines key properties such as name, version, license, description, and dependencies, along with their significance. Additionally, it mentions that this is part of a series aimed at enhancing knowledge about Node.js, with further guides to be released weekly.

Uploaded by

anas170p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

The Basics of Package.json - NodeSource

The document provides an introduction to the package.json file, which is essential for understanding and working with Node.js and npm. It outlines key properties such as name, version, license, description, and dependencies, along with their significance. Additionally, it mentions that this is part of a series aimed at enhancing knowledge about Node.js, with further guides to be released weekly.

Uploaded by

anas170p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

SIGN CONTACT

N|Solid Services Solutions Resources Company IN  


US


The NodeSource Blog
SHARE
Search


N E XT P O ST
You have reached the beginning of time!
Solving Memory Leaks in Node.js has Never

Been Easier, Introducing the Latest Version of
N|Solid

A L L P O STS / CO M M U N I T Y

The Basics of Package.json


by: Gabby T and Marian Villa in Community on Feb 15 2022

Packages Node.js NodeSource npm

The Basics of Package.json


In this chapter, we'llHello,
give No
youmatter
a kickstart introduction
what your to e ectively
current investment in Node.jsusing
is, we can help you to reach the
next level.
package.json with Node.js and npm .

The package.json file is core to the Node.js ecosystem and is a fundamental part
of understanding and working with Node.js , npm , and even modern JavaScript .
This file is used as a manifest, storing information about applications, modules,
packages, and more. Because understanding it is essential to working with
Node.js , it's a good idea to grasp the commonly found and most crucial properties
of a package.json file to use it e ectively.

This is a series, based on one of the most featured whitepapers we have done by
developers in the Node.js ecosystem. If you are interested in the complete guide,
you can get it through this link.

The 2022 guide will include this, which we will be releasing by units of knowledge
every Thursday in the following weeks. Today you are in part 1 of the guide:

1. The Essential npm Commands

Using npm init to initialize a project

Using npm init --yes to instantly initialize a project

Install modules with npm install

Install modules and save them to your package.json as a dependency

Install modules and save them to your package.json as a developer


dependency

Install modules globally on your system

2. The Basics of package.json

2.1. Identifying Metadata Inside package.json

The name property

The version property

The license property

The property description

The keywords property

2.2. functional metadata inside package.json

The main property

The repository property

The script property

The dependencies property

The devdependencies property

3. Understanding the di erent types of dependencies and other host Specs inside
package.json

PeerDependencies

PeerDependenciesMeta

OptionalDependencies

BundledDependencies

engines

os

cpu

Identifying Metadata Inside


package.json

The name property

The name property in a package.json file is one of the fundamental components


of the package.json structure. At its core, the name is a string that is exactly what
you would expect: the name of the module that the package.json is describing.

Inside your package.json , the name property as a string would look something
like this:

"name": "metaverse"

There are only a few material restrictions on the name property: • Maximum length
of 214 URL-friendly characters • No uppercase letters • No leading periods (.) or
underscores (_) (Except for scoped packages)

However, some so ware ecosystems have developed standard naming conventions


that enable discoverability. A few examples of this namespacing are the babel
plugin- for Babel and the webpack-loader tooling.

The version property

The version property is a crucial part of a package.json , as it denotes the


current version of the module that the package.json file is describing.

While the version property isn't required to follow semver (semantic versioning)
standards, which is the model used by the vast majority of modules and projects in
the Node.js ecosystem, it's what you'll typically find in the version property of a
package.json file.

Inside your package.json , the version property as a string using semver could
look like this:

"version": "5.12.4"

The license property

The license property of a package.json file is used to note the module that the
package.json file describes. While there are some complex ways to use the
licensed property of a package.json file (to do things like dual-licensing or
defining your own license), the most typical usage is to use an SPDX License
identifier. Some examples that you may recognize are MIT, ISC, and GPL-3.0.

Inside your package.json , the license property with an MIT license looks like
this:

"license": "MIT"

The description property

The description property of a package.json file is a string that contains a


human-readable description of the module. It's the module developer's chance to
let users know what precisely a module does quickly. Search tools frequently index
the description property like npm search and the npm CLI search tool to help find
relevant packages based on a search query.

Inside your package.json , the description property would look like this:

"description": "The Metaverse virtual reality. The final outcome of


all virtual worlds, augmented reality, and the Internet."

The keywords property

The keywords property inside a package.json file is, as you may have guessed, a
collection of keywords that describe a module. Keywords can help identify a
package, related modules and so ware, and concepts.

The keywords property is always an array, with one or more strings as the array's
values; each one of these strings will, in turn, be one of the project's keywords.

Inside your package.json , the keywords array would look something like this:

"keywords": [
"metaverse",
"virtual reality",
"augmented reality",
"snow crash"
]

Functional Metadata Inside


package.json

The main property

The main property of a package.json is a direction to the entry point to the


module that the package.json is describing. In a Node.js application, when the
module is called via a require statement, the module's exports from the file
named in the main property will be returned to the Node.js application.

Inside your package.json , the main property, with an entry point of app.js, would
look like this:

"main": "app.js"

The repository property

The repository property of a package.json is an array that defines where the


source code for the module lives. Typically, this would be a public GitHub repo for
open source projects, with the repository array noting that the type of version
control is git and the URL of the repo itself. One thing to note about this is that it's
not just a URL where the repo can be accessed from, but the full URL the version
control can be accessed from.

Inside your package.json, the repository property would look like this:

"repository": {
"type": "git",
"url": "https://github.com/bnb/metaverse.git"
}

The scripts property

The scripts property of a package.json file is simple conceptually but complex


functionally, to the point that it's used as a build tool by many.

At its simplest, the scripts property contains a set of entries; the key for each
entry is a script name, and the corresponding value is a user-defined command to
be executed. Scripts are frequently used to test, build, and streamline the needed
commands to work with a module. Inside your package.json , the scripts

property with a build command to execute tsc (presumably to transpile your


application using TypeScript) and a test command using Standard would look like
this:

"scripts": {
"build": "tsc",
"test": "standard"
}

To run scripts in the scripts property of a package.json , you'll need to use the
default npm run command. So, to run the above example's build, you'd need to
run this:

Usage:

$ npm run build

That said, to run the test suite, you'd need to execute this:

Usage:

$ npm test

Notice that npm does not require the run keyword as part of the given script
command for some tasks like the test, start, and stop by default.

The dependencies property

The dependencies property of a module's package.json is defined by the other


modules that this module uses. Each entry in the dependencies property includes
the name and version of other packages required to run this package.

Note: You'll frequently find carets (^) and tildes (~) included with package versions.
These are the notations for version range — taking a deep dive into these is outside
the scope of this guide, but you can learn more in our primer on semver. In addition,
you may specify URLs or local paths in place of a version range.

Inside your package.json , the dependencies property of your module may look
something like this:

"dependencies": {
"async": "^0.2.10",
"npm2es": "~0.4.2",
"optimist": "~0.6.0",
"request": "~2.30.0",
"skateboard": "^1.5.1",
"split": "^0.3.0",
"weld": "^0.2.2"
},

The devDependencies property

The devDependencies property of package.json is almost identical to the


dependencies property in terms of structure. The main di erence: while the
dependencies property is used to define the dependencies that a module needs to
run in production, devDependencies property is commonly used to define the
dependencies the module needs to run in development.

Inside your package.json , the devDependencies property would look something


like this:

"devDependencies": {
"escape-html": "^1.0.3",
"lucene-query-parser": "^1.0.1"
},

Remember that you can now monitor your applications and take your Node.js
journey to a professional level with N|Solid.

To get the best out of Node.js and low-cost observability, start a free trial of
N|Solid.

If you have any questions, please feel free to contact us at


[email protected] or through this form.

And if you want to find out about our latest content and product releases, these
are the channels to keep up to date with NodeSource:

↳ ↳ Nodesource's Twitter

↳ ↳ Nodesource's LinkedIn

F E AT U R E D A R T I C L E S

Solving Memory Leaks in Node.js has Never Been Easier, Introducing the Latest Version of N|Solid
In Product on Apr 29 2024

Enhancing Node.js Core: Introducing Support for Synchronous ESM Graphs


In Product on Mar 26 2024

N|Solid: Node.js Compatibility Proven through Fastify CI Integration


In Product on Mar 22 2024

C AT E G O R I E S

Community

How To

Node.js

NodeSource

Product

The NodeSource platform o ers a high-definition view of the


performance, security and behavior of Node.js applications and
functions.

STA R T F O R F R E E

  
© 2024 NodeSource

W H AT W E D O S O LU T I O N S LEARN CO M PA N Y
N|Solid Microservices Blog Contact Us
Product Pricing High Tra ic Resources About NodeSource
NodeSource Services Legacy Applications Support Portal Press
Internet of Things Documentation Legal
Privacy Policy

You might also like