2018-10-12 admin


什么是install,Minimal JavaScript module loader


install Build Status Greenkeeper badge

The CommonJS module syntax 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.

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.


From NPM:

npm install install

From GitHub:

cd path/to/node_modules
git clone git://
cd install
npm install .


The first step is to create an install function by calling the makeInstaller method. Note that all of the options described below are optional:

var install = require("install").makeInstaller({
  // Optional list of file extensions to be appended to required module
  // identifiers if they do not exactly match an installed module.
  extensions: [".js", ".json"],

  // If defined, the options.fallback function will be called when no
  // installed module is found for a required module identifier. Often
  // options.fallback will be implemented in terms of the native Node
  // require function, which has the ability to load binary modules.

  // Boolean flag indicating whether the installed code will be running in
  // a web browser.

  // List of fields to look for in package.json files to determine the
  // main entry module of the package. The first field listed here whose
  // value is a string will be used to resolve the entry module. Defaults
  // to just ["main"], or ["browser", "main"] if options.browser is true.
  mainFields: ["browser", "main"],

The second step is to install some modules by passing a nested tree of objects and functions to the install function:

var require = install({
  "main.js"(require, exports, module) {
    // On the client, the "assert" module should be install-ed just like
    // any other module. On the server, since "assert" is a built-in Node
    // module, it may make sense to let the options.fallback function
    // handle such requirements. Both ways work equally well.
    var assert = require("assert");

      // This require function uses the same lookup rules as Node, so it
      // will find "package" in the "node_modules" directory below.
    ); =;

  node_modules: {
    package: {
      // If package.json is not defined, a module called "index.js" will
      // be used as the main entry point for the package. Otherwise the
      // exports.main property will identify the entry point.
      "package.json"(require, exports, module) { = "package";
        exports.version = "0.1.0";
        exports.main = "entry.js";

      "entry.js"(require, exports, module) { =;

Note that the install function merely installs modules without evaluating them, so the third and final step is to require any entry point modules that you wish to evaluate:

// => "/main.js"

This is the “root” require function returned by the install function. If you’re using the install package in a CommonJS environment like Node, be careful that you don’t overwrite the require function provided by that system.

If you need to change the behavior of the module object that each module function receives as its third parameter, the shared Module constructor is exposed as a property of the install function returned by the makeInstaller factory:

var install = makeInstaller(options);
var proto = install.Module.prototype;

// Wrap all Module.prototype.require calls with some sort of logging.
proto.require = wrapWithLogging(proto.require);

// Add a new method available to all modules via module.newMethod(...).
proto.newMethod = function () {...};

Many more examples of how to use the install package can be found in the tests.


转载请注明:文章转载自 JavaScript中文网 []



How to detect the installed Chrome version?
XanSkizit提出了一个问题:How to detect the installed Chrome version?,或许与您遇到的问题类似。 回答者Danserg给出了该问题的处理方式: Get major version of Ch...
Install Bower components into two different directories?
Robb Schiller提出了一个问题:Install Bower components into two different directories?,或许与您遇到的问题类似。 回答者Sindre Sorhus给出了该问题的处理方式: ...
科普文:为什么不能在服务器上 npm install ?
## 背景 Node.js 很简单,容易上手。但也因此缺乏不少规范,使用者水平参差不齐。 最近经常看到的一个问题是:很多新手,在部署的时候,是直接在服务器上 npm install ,这是非常不推荐的。 ## 存在的问题 1. 无法确定唯...
install-peerdeps是什么 什么是install-peerdeps,CLI to automatically install peerDeps install-peerdeps官网:官网 install-peerdeps源码仓...
Error installing node-gyp on ubuntu
Artjom B.Pratik Mandrekar提出了一个问题:Error installing node-gyp on ubuntu,或许与您遇到的问题类似。 回答者LoganPratik Mandrekar给出了该问题的处理方式: T...
read-installed是什么 什么是read-installed,Read all the installed packages in a folder, and return a tree structure with all th...
spawn-npm-install是什么 什么是spawn-npm-install,programmatically install npm dependencies spawn-npm-install官网:官网 spawn-npm-in...
npm-install-package是什么 什么是npm-install-package,Install an npm package npm-install-package官网:官网 npm-install-package源码仓库:源...
npm-install-global是什么 什么是npm-install-global,Simple API for globally installing or uninstalling one or more NPM packages....
strong-cached-install是什么 什么是strong-cached-install,Speed up npm install in unit-tests by using a cache on subsequent runs...