JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1394114
  • Score
    100M100P100Q189403F
  • License BSD-2-Clause

Simple module to add additional directories to the Node module search for top-level app modules

Package Exports

  • app-module-path

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (app-module-path) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

app-module-path

This simple module enables you to add additional directories to the Node.js module search for top-level app modules only. This allows application-level modules to be required as if they were installed into the node_modules directory.

Installation

npm install app-module-path --save

Usage

Given the following example directory structure:

  • src/ - Source code and application modules directory
    • foo/ - A module directory
      • index.js
    • bar/ - Another module directory
      • index.js
  • node_modules/ - Installed modules
    • baz/ - An installed module
      • index.js
  • index.js - Main script

The additional application module paths should be added before any modules are loaded (i.e. at the very beginning of your main script) using code similar to the following:

Example index.js

var path = require('path');

// Add the "src" directory to the app module search path:
require('app-module-path').addPath(path.join(__dirname, 'src'));

var foo = require('foo'); // Works
var bar = require('bar'); // Works
var baz = require('baz'); // Works

In addition, application level modules can be required from any other application level modules using a top-level module path. For example:

Example src/foo/index.js

var bar = require('bar'); // Works
var baz = require('baz'); // Works

Lastly, installed modules (i.e. modules installed into node_modules will not be able to require application-level modules so the following will not work:

Example node_modules/baz/index.js

var bar = require('foo'); // Will *not* work!
var baz = require('bar'); // Will *not* work!

Contribute

Pull requests, bug reports and feature requests welcome.

License

BSD-2-Clause