NPM and package.json

With Browserify, we can create CommonJS modules that can be executed in the browser. When you use CommonJS modules in the browser, Browserify will provide the necessary tools to load the modules, which includes a definition for the require() function.

When you use Browserify, you can use the Node package manager to install and define dependencies for your projects. A useful tool is the npm command tool, used to install and manage project dependencies.

The package.json file in a Node project is a JSON file used to define, install, and manage the version of the libraries that your project depends on. A package.json file can contain many configuration options; you can see the complete documentation on the Node website at https://docs.npmjs.com/. Here is a list of the main values.

  • Name – The name of the project without spaces
  • Description – A short description of the project
  • Version – A version number for the project, typically starting with 0.0.1
  • Dependencies – A list of libraries with the version number that the project depends on
  • devDependencies – Same as dependencies, but this list is used only for development environments—useful for putting libraries for testing, for instance
  • licence – A license name for the project code

We can start with a very simple package.json file that contains only some basic fields, and then we can extend it as needed:

{
  "name": "backbone-contacts ",
  "version": "0.0.1",
  "description": "Example code for the book Mastering Backbone.js",
  "author": "Abiee Alejandro <[email protected]>",
  "license": "ISC",
  "dependencies": {
  },
  "devDependencies": {
  }
}

As you can see, we don't have any dependency yet. We can install our first dependency with npm:

$ npm install --save underscore jquery backbone bootstrap

This command will install the basic dependencies to work with backbone; the save flag will update automatically the package.json file, adding the library names and its current versions:

  "dependencies": {
    "backbone": "^1.2.1",
    "bootstrap": "^3.3.5",
    "jquery": "^2.1.4",
    "underscore": "^1.8.3"
  }

The format of the library version follows the semver standard; you can see more about this format in the official semver website.

One advantage of using the package.json file in your project is that, the next time you want to install the dependencies, you don't need to remember the libraries and their versions; you can just hit Install without any argument and Node will read the package.json file and make the installs for you:

$ npm install

With npm you can install development packages such as the mocha testing library, but instead of using the save flag use save-dev:

$ npm install --save-dev mocha

Now that you know how to install dependencies and save them in the package.json file, we can start using Browserify in the Contacts app.

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

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