The Basics of Package.json in Node.js and npm – NodeSource

The package.json file is central to the Node .js ecosystem and is a basic part of understanding and working with Node.js, npm, and even modern JavaScript. The package.json is used as what amounts to a manifest about applications, modules, packages, and more – it’s a tool that \’s uses to make modern development simplified, modular and efficient.

As a developer in the .js Node ecosystem, understanding the basics of package.json is one of the first steps to really starting your development experience with Node.js.

Because of how essential it is to understand the basics of package.json for development with Node.js, I have reviewed and described some of the most common and important properties of a package.json file that you will need to use package.json effectively.

Identifying metadata within package.json The name property

The name

property of a package.json file is one of the fundamental components of the package.json structure. In essence, name is a string

that is exactly what you would expect: the name of the module that package.json describes. Within your package.json, the name

property as a string would look like this

:

“name”: “metaverse”

Despite having only a few material constraints (a maximum length of 214 characters, you cannot start with . or <i>, no uppercase letters, and no characters that are not URL-compatible), an interesting aspect of the name property is that, There have been software ecosystems that have developed standard naming conventions that allow detection simply by using the name property.

Some examples of this type of namespace are babel-plugin- for Babel and webpack-loader tools

. The version property

The

version property is a key part of a package.json file

because it indicates the current version of the module that the package.json file describes. While

the version property is not _required follow semver, semver is the

standard used by the vast majority of modules and projects in the Node ecosystem.js – and the module version, according to Semver, is what you will typically find in the version property of a package.json file.

Within your package.json, the version property

as a string using semver might look like this

: “version”: “5.12.4” The license property

The license property

of a package.json file is

used to note which license is described by the module that the package.json file describes. While there are some complex ways to use the license property of a package.json file (to do things like dual licenses or define your own license), the most typical use is to use an SPDX license identifier: some examples you might 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 about the module – basically, it’s the module developer’s chance to quickly let users know what exactly it is a module does. The description property is frequently indexed using search tools such as 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 virtual reality of the Metaverse. The end result of all virtual worlds, augmented reality and the Internet. The

keywords property inside a package.json file is, as you might have guessed, a collection of keywords about a module. Keywords can help identify a package, related modules and software, and concepts.

The keywords

property will always be an array, with one or more strings as array values – each of these strings will be, in turn, one of the keywords of the project.

Inside your package.json, the keyword array would look like this: “keywords”: [ “metaverse”, “virtual reality”, “augmented reality”, “snow shock” ] Functional metadata inside package.json The main

property The main property

of package.json is an address to the entry point to the module that package.json describes

.

In a Node.js application, when the module is called through a require statement, the exports of the module from the file named in the main property will be what is returned to the Node.js application.

Inside your package.json, the main property, with an app.js entry point

, would look like this

: “main”: “app.js”, The repository property

The

repository property of a package.json file is an array that defines where the module’s source code resides. Normally, for open source projects, this would be a public GitHub repository, with the repository array pointing out that the version control type is git and the URL of the repository itself. One thing to note about this is that it’s not just a URL from which the repository can be accessed, but the full URL from which version control can be accessed.

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 it is functionally complex to the point that many use it as a build tool.

In its simplest form, the scripts property takes an object with as many key/value pairs as desired. Each of the keys in these key/value pairs is the name of a command that can be executed. The corresponding value of each key is the actual command that is executed. Scripts are frequently used to test, compile, and rationalize the commands required to work with a module.

Inside your package.json, the scripts property with a build command to run node app

.js (presumably to build your application) and a test command using Standard would look like

this: “scripts”: { “build”: “node app.js”, “test”: “standard” } The dependencies property The dependencies property of a module’s package.json is where

dependencies

– the other modules that this Uses of the module – are defined. The dependencies property takes an object that has the name and version in which each dependency should be used. Linking things to the version property defined above defines the version that a module needs. Note that you will often find collations (^) and tildes (~) included with package versions. Here are the notations for the version range: Digging into them is beyond the scope of this article, but you can learn more in our manual on semver.

Within your package.json, your module’s dependencies property

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 a package.json file is almost identical to the

dependencies

property In terms of structure, with one key difference. The dependencies property is used to define the dependencies that a module needs to run in production. The devDependencies property is typically used to define the dependencies that the module needs to run in development.

Inside your package.json, the devDependencies property

would look like this: “devDependencies

“: { “escape-html”: “^1.0.3”, “lucene-query-parser”: “^1.0.1” }

Want to move on?

If you want to keep learning about Node.js, npm, package.json, and development with the Node.js stack, I have some amazing articles for you.

See also Have a guide on some great developer utilities .js Node – If you want to narrow down your developer experience to 11, be sure to check it out to find some tools to help you get there.

The goal of this guide was to help you get package.json up and running for development with Node.js and npm. If you want to take the leap and make sure you’re always on a solid foundation with Node.js and npm modules, you should check out NodeSource Certified Modules, an amazing tool to help ensure you spend more time building applications and less time worrying about modules.