1 | # install [![Build Status](https://travis-ci.org/benjamn/install.svg?branch=master)](https://travis-ci.org/benjamn/install) [![Greenkeeper badge](https://badges.greenkeeper.io/benjamn/install.svg)](https://greenkeeper.io/)
|
2 |
|
3 | The [CommonJS module syntax](http://wiki.commonjs.org/wiki/Modules/1.1) is one of the most widely accepted conventions in the JavaScript ecosystem. Everyone seems to agree that `require` and `exports` are a reasonable way of expressing module dependencies and interfaces, and the tools for managing modular code are getting better all the time.
|
4 |
|
5 | Much less of a consensus has developed around the best way to deliver CommonJS modules to a web browser, where the synchronous semantics of `require` pose a non-trivial implementation challenge. This module loader contributes to that confusion, yet also demonstrates that an amply-featured module loader need not stretch into the hundreds or thousands of lines.
|
6 |
|
7 | Installation
|
8 | ---
|
9 | From NPM:
|
10 |
|
11 | npm install install
|
12 |
|
13 | From GitHub:
|
14 |
|
15 | cd path/to/node_modules
|
16 | git clone git://github.com/benjamn/install.git
|
17 | cd install
|
18 | npm install .
|
19 |
|
20 | Usage
|
21 | ---
|
22 |
|
23 | The first step is to create an `install` function by calling the
|
24 | `makeInstaller` method. Note that all of the options described below are
|
25 | optional:
|
26 |
|
27 | ```js
|
28 | var install = require("install").makeInstaller({
|
29 | // Optional list of file extensions to be appended to required module
|
30 | // identifiers if they do not exactly match an installed module.
|
31 | extensions: [".js", ".json"],
|
32 |
|
33 | // If defined, the options.onInstall function will be called any time
|
34 | // new modules are installed.
|
35 | onInstall,
|
36 |
|
37 | // If defined, the options.override function will be called before
|
38 | // looking up any top-level package identifiers in node_modules
|
39 | // directories. It can return either a string to provide an alternate
|
40 | // package identifier or a non-string value to prevent the lookup from
|
41 | // proceeding.
|
42 | override,
|
43 |
|
44 | // If defined, the options.fallback function will be called when no
|
45 | // installed module is found for a required module identifier. Often
|
46 | // options.fallback will be implemented in terms of the native Node
|
47 | // require function, which has the ability to load binary modules.
|
48 | fallback
|
49 | });
|
50 | ```
|
51 |
|
52 | The second step is to install some modules by passing a nested tree of
|
53 | objects and functions to the `install` function:
|
54 |
|
55 | ```js
|
56 | var require = install({
|
57 | "main.js": function (require, exports, module) {
|
58 | // On the client, the "assert" module should be install-ed just like
|
59 | // any other module. On the server, since "assert" is a built-in Node
|
60 | // module, it may make sense to let the options.fallback function
|
61 | // handle such requirements. Both ways work equally well.
|
62 | var assert = require("assert");
|
63 |
|
64 | assert.strictEqual(
|
65 | // This require function uses the same lookup rules as Node, so it
|
66 | // will find "package" in the "node_modules" directory below.
|
67 | require("package").name,
|
68 | "/node_modules/package/entry.js"
|
69 | );
|
70 |
|
71 | exports.name = module.id;
|
72 | },
|
73 |
|
74 | node_modules: {
|
75 | package: {
|
76 | // If package.json is not defined, a module called "index.js" will
|
77 | // be used as the main entry point for the package. Otherwise the
|
78 | // exports.main property will identify the entry point.
|
79 | "package.json": function (require, exports, module) {
|
80 | exports.name = "package";
|
81 | exports.version = "0.1.0";
|
82 | exports.main = "entry.js";
|
83 | },
|
84 |
|
85 | "entry.js": function (require, exports, module) {
|
86 | exports.name = module.id;
|
87 | }
|
88 | }
|
89 | }
|
90 | });
|
91 | ```
|
92 |
|
93 | Note that the `install` function merely installs modules without
|
94 | evaluating them, so the third and final step is to `require` any entry
|
95 | point modules that you wish to evaluate:
|
96 |
|
97 | ```js
|
98 | console.log(require("./main").name);
|
99 | // => "/main.js"
|
100 | ```
|
101 |
|
102 | This is the "root" `require` function returned by the `install`
|
103 | function. If you're using the `install` package in a CommonJS environment
|
104 | like Node, be careful that you don't overwrite the `require` function
|
105 | provided by that system.
|
106 |
|
107 | Many more examples of how to use the `install` package can be found in the
|
108 | [tests](https://github.com/benjamn/install/blob/master/test/run.js).
|