Managing Node.js packages with Npm

Npm is a package manager for JavaScript and is similar to Composer for PHP or Pip for Python. (go to https://www.npmjs.com/.) Some people may assure you that npm stands for Node Package Manager, but although it has been Node.js's default package manager since version 0.6.3, npm is not an acronym. Thus, you will often see npm spelled in lowercase.

To quickly check if you have npm installed, you can use a terminal window to query the version of npm that you have installed.

npm -v

For instructions on how to install npm on your particular operating system, ensure that you follow the guidelines on npm's official website. The version used in the sample codes and demo applications in this book is version 1.3.10.

When using npm to install third-party packages, you can choose to install them either locally for your project, or globally so that the package will be visible anywhere in your system.

npm install watch

By default, when you install a package (in the previous example, we install a package named watch that watches directories and files for changes) with no flags, the package is installed locally (assuming a package.json file also exists) and saved to a node_modules directory relative to where the command was executed.

To install a package globally or system-wide, just append the -g flag to the install command:

npm install watch -g

By convention, if you need a package that is used within your code through require statements, you will want to save the package locally. If the intent is to use the package as an executable code from your command line, then you will normally want to install it globally.

If you want to build on your package.json manifest so that the local packages your project depends on can be shared and easily installed, you can either edit the manifest file manually, adding the dependency to the json object under the "dependencies" key, or you can let npm do that for you, but without forgetting to specify the --save flag:

npm install watch --save

Note that running the previous command will download the code that makes up the requested package into your working directory as well as update your package.json manifest so that you can later update the packages or install them anew, as needed. In other words, you can always use your existing package.json file to rebuild your development environment as far as your third-party dependencies are concerned.

Once you have one or more dependencies specified in your package.json file, you can install them by running npm, as follows:

npm install

This will download all the dependencies in your manifest file and save them into node_modules.

Similarly, you can update packages through npm by using the update command:

npm update

If you don't know how to get started to create a package.json manifest file, you can let npm help you to fill in the blanks for the most common attributes.

npm init

This will load an interactive utility that asks you to enter values for the various attributes for the manifest, such as the package name, version, author name, and so on. It also offers some same default values so that you can either ignore attributes you don't know what they do, or you can trust npm with whatever fallback option it offers you, making it easy for you to quickly get a manifest file going.

npm init
// … assume all proposed default values

// - - - - - - -
// package.json

{
  "name": "npm",
  "version": "0.0.0",
  "description": "ERROR: No README data found!",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "author": "",
  "license": "BSD-2-Clause"
}

Once you have a generic package.json manifest, you can add your dependencies to it with npm install commands.

npm install browserify --save

// - - - - - - -
// package.json

{
  "name": "npm",
  "version": "0.0.0",
  "description": "ERROR: No README data found!",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "author": "",
  "license": "BSD-2-Clause" ,
  "dependencies": {
    "browserify": "~9.0.3"
  }
}

Of course, you can always edit the file manually to change values or remove attributes that you feel are unnecessary, such as license, description, or version. Some attributes are only meaningful if you plan to share your package privately or with the global npm registry. Other values, such as scripts, are used for convenience during development. For example, we can register a script to be executed when we run npm <script value>.

// - - - - - - -
// package.json

{
 "scripts": {
    "test": "node test.js"
  }
}

// - - - - - - -
// test.js

console.log('testing npm scripts'),

Thus, we can have Node run a script named test.js through npm with the following command:

npm test

While you may not be saving a lot of typing by using npm in this case, you do make it more standard for others to know, for example, how to run your tests, even if your test runner scripts are not named or executed in any particular standard form.

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

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