Using package.json

All Node modules must include a package.json file in their root directory. package.json is a simple JSON text file that defines a module, including dependencies. The package.json file can contain a number of different directives to tell the Node Package Manager how to handle the module.

The following is an example of a package.json file with a name, version, description, and dependencies:

{
    "name": "my_module",
    "version": "0.1.0",
    "description": "a simple node.js module",
    "dependencies" : {
        "express"   :  "latest"
    }
}

The only required directives in the package.json file are name and version; the rest depend on what you would like to include. Table 3.2 describes the most common directives.

Image
Image

Table 3.2 Directives used in the package.json file

A great way to use package.json files is to automatically download and install the dependencies for your Node.js app. All you need to do is create a package.json file in the root of your project code and add the necessary dependencies to it. For example, the following package.json file requires the express module as a dependency:

{
    "name": "my_module",
    "version": "0.1.0",
    "dependencies" : {
        "express"   :  "latest"
    }
}

Then you run the following command from the root of your package, and the express module is automatically installed:

npm install

Notice that no module is specified in the command npm install. That is because npm looks for a package.json file by default. Later, as you need additional modules, all you need to do is add those to the dependencies directive and then run npm install again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.25.112