Node.js Packages
Let's see how the Node platform provides a way to structure an application under the form of a package.
We'll cover the following
Anatomy of a package
Technically, a package is a folder containing the following elements:
- A
package.json
file that describes the application and its dependencies. - A entry point into the application, defaulting to the
index.js
file. - A
node_modules/
subfolder, which is the default place where Node looks for modules to be loaded into the application. - All the other files forming the source code of the application.
The package.json
file
This JSON file describes the application and its dependencies, you can think of it as the app’s ID document. It has a well-defined format consisting of many fields, most of them optional. The two mandatory fields are:
name
(all lowercase letters without dots, underscores, and any non-URL safe character in it)version
(following the semantic versioning format - more on that later)
Below is an example of a typical package.json
file.
{
"name": "thejsway-node-example",
"version": "1.0.0",
"description": "Node example for the book \"The JavaScript Way\"",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"moment": "^2.18.1",
"semver": "^5.3.0"
},
"keywords": [
"javascript",
"node",
"thejsway"
],
"author": "Baptiste Pesquet"
}
Get hands-on with 1400+ tech skills courses.