Menu Developer Moovweb University

Node Modules

Node Modules add specific functionality to your project. Every Moovweb project starts off with at least two modules that you can find in the package.json file. A sample package.json file is shown below:

{
  "name": "igadgetjs",
  "version": "0.0.0",
  "main": "scripts/moov_main.js",
  "dependencies": {
    "moov_rewriter": "2.2.3",
    "moov_stdlib": "2.6.1",
    "moov_perf": "1.1.21"
  },
  "devDependencies": {
    "gulp": "3.9.0",
    "moov_builder": "2.4.6"
  },
  "scripts": {
    "bundle": "node --harmony ./node_modules/.bin/moov_builder",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Moovweb",
  "license": "MIT"
}

Moovweb Node Modules

In addition to the core module, we have a number of modules that provide different kinds of functionality. The most important module is stdlib which contains all the standard Tritium functions and is automatically included when you generate a project.

Click here for a brief summary for each of the modules, or view the sidebar for the full API documentation.


Adding and Upgrading Modules

Periodically, Moovweb introduces new functionality by creating new modules or upgrading existing ones. You may even find a module that someone else has written that would work well in your Moovweb project.

To add a module to your project, follow these steps.

  1. On the command line, install the module with the -save flag. This adds the module to your package.json file. npm install MODULE -save

  2. Shrinkwrap the project (after deleting any existing npm-shrinkwrap.json file), to lock in the dependencies. npm shrinkwrap

  3. Require the module in your project scripts: var MODULE = require("module");

Modules are downloaded from a Moovweb-specific private NPM server. It will also allow you to download public node modules into your project.

If you want to install a module from your local machine (if, for example, someone has created a module), then you can use a .tar file, URL, or path to a folder on your local machine in the install command. For example:

npm install ~/dev/modules/custom-module -save

will install the custom-module in the current project.

Some modules require a minimum version of the SDK. As in our installation and upgrade guidelines, old modules will always work with new SDK versions.


Creating Modules

Creating custom modules allows the use of novel functions in your project. Creating a module is simple. The NPM documentation on creating modules is useful, but here is a summary.

  1. Create a directory for your module. You may want to set up a new modules folder in your development directory for all your modules.

  2. Use npm init in your new directory to start the process. The initializer will ask some questions on how you want your NPM module to be named and versioned. Most of the default options are sufficient in this process.

  3. Create an index.js file in the same directory as the package.json file. Into this, write all the functions you want your module to use.

To install the module in a project, you can run the local npm install command mentioned above. Then require your module in your scripts, and you’re free to use all your new functions.

var myCustomModule = require("myCustomModule");

myCustomModule.specialFunction();

You can also push your module to NPM if you would like it to be public.