• Stars
    star
    411
  • Rank 105,247 (Top 3 %)
  • Language
    JavaScript
  • License
    BSD 2-Clause "Sim...
  • Created almost 11 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

NPM

app-module-path

This simple module enables you to add additional directories to the Node.js module search path (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

// ***IMPORTANT**: The following line should be added to the very
//                 beginning of your main script!
require('app-module-path').addPath(baseDir);

IMPORTANT: The search path should be modified before any modules are loaded!

Example:

In your my-app/index.js (or my-app/server.js) file:

// Add the root project directory to the app module search path:
require('app-module-path').addPath(__dirname);

Given the following example directory structure:

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

The following will work for any modules under the src directory:

// All of the following lines will work in "src/foo/index.js" and "src/bar/index.js":
var foo = require('src/foo'); // Works
var bar = require('src/bar'); // Works
var baz = require('installed-baz'); // Works

Lastly, by design, installed modules (i.e. modules under the node_modules directory) will not be able to require application-level modules so the following will not work:

// All of the following lines will *not* work in "node_modules/installed-baz/index.js"!
var foo = require('src/foo'); // Fails
var bar = require('src/bar'); // Fails

Alternate Usage (app-module-path/register)

This module supports an alternate method of adding a path to the Node.js module search path that requires less code. Requiring or importing the app-module-path/register module will result in the directory of the calling module being added to the Node.js module search path as shown below:

Explicitly enabling a directory/package

By default, app-module-path will not attempt to resolve app modules from a directory that is found to be within a node_modules directory. This behavior can be changed by explicitly enabling app-module-path to work for descendent modules of a specific directory. For example:

var packageDir = path.dirname(require.resolve('installed-module-allowed'));
require('../').enableForDir(packageDir);

ES5

require('app-module-path/register');

// Is equivalent to:
require('app-module-path').addPath(__dirname);

ES6

import "app-module-path/register";

// Is equivalent to:
import { addPath } from 'app-module-path';
addPath(__dirname);

Alternative Usage (app-module-path/cwd)

Additionally, requiring or importing app-module-path/cwd will result in the current working directory of the Node.js process being added to the module search path as shown below:

ES5

require('app-module-path/cwd');

// Is equivalent to:
require('app-module-path').addPath(process.cwd());

ES6

import "app-module-path/cwd";

// Is equivalent to:
import { addPath } from 'app-module-path';
addPath(process.cwd());

Additional Notes

  • Search path order:

    • App module paths will be added to the end of the default module search path. That is, if a module with the same name exists in both a node_modules directory and an application module directory then the module in the node_modules directory will be loaded since it is found first. This behavior is new in v2.x. In v1.x, this search order was reversed
  • Node.js compatibility:

    • This module depends on overriding/wrapping a built-in Node.js method, and it is possible (but unlikely) that this behavior could be broken in a future release of Node.js (at which point a workaround would need to be used)
    • This module will not change or break modules installed into the node_modules directory.
  • Recommendations:

    • Since this module changes the Node.js convention of how non-relative modules are resolved, it is recommended (but not required) to put all app modules in a common directory below the application root (such as my-app/src or my-app/app_modules) and then to add the application root to the search path. The require calls would then be something like require('src/foo') or require('app_modules/foo'). The common prefix makes it more clear that the module can be found in the application's modules directory and not in the node_modules directory.

Contribute

Pull requests, bug reports and feature requests welcome.

License

BSD-2-Clause

More Repositories

1

morphdom

Fast and lightweight DOM diffing/patching (no virtual DOM needed)
JavaScript
3,165
star
2

child-process-promise

Simple wrapper around the "child_process" module that makes use of promises
JavaScript
250
star
3

browser-refresh

Node.js module to enable server restart and browser refresh in response to file modifications
JavaScript
110
star
4

marko-vs-react

DEPRECATED - Test app to benchmark and compare Marko and React
HTML
90
star
5

warp10

Transport complex/circular JavaScript objects from the server to the web browser at lightning fast speeds
JavaScript
46
star
6

codemirror-atom-modes

Use Atom grammar files to apply syntax highlighting in a CodeMirror editor
JavaScript
16
star
7

warmup

Warmup server apps by hitting URLs or performing tasks.
JavaScript
12
star
8

require-self-ref

Solves the relative path problem in Node.js by allowing the target module argument of a require call to be relative to the root directory of the containing package
JavaScript
11
star
9

meta-router

Declarative URL router for Express that provides support for associating metadata with a route.
JavaScript
11
star
10

http-stats

Node.js module for benchmarking HTTP servers
JavaScript
7
star
11

listener-tracker

Allows added event listeners to be tracked for easy removal
JavaScript
7
star
12

express-view-streaming

Sample app that demonstrates streaming template rendering with Express
JavaScript
7
star
13

argly

A flexible command line arguments parser that is easy to configure and offers robust type handling.
JavaScript
7
star
14

async-config

A simple Node.js module for loading environment-specific config files.
JavaScript
6
star
15

express-resetter

Utility module to reset an Express instance to a previous state after registering middleware
JavaScript
6
star
16

browser-refresh-taglib

Taglib to enable browser page refresh in response to file modifications on the server
JavaScript
4
star
17

ignoring-watcher

Watch an entire directory tree while ignoring specific directories/files based on .gitignore rules.
JavaScript
4
star
18

browser-refresh-client

Small module to interface with the parent browser-refresh process to control reloading
JavaScript
3
star
19

events-light

Lightweight and fast implementation of the 'events' module for the browser and server
JavaScript
3
star
20

github-project-inspector

Node.js module that uses the Github API to gather metadata about a project on Github
JavaScript
2
star
21

velociblog

A static blog generator built on top of JavaScript, Node.js and RaptorJS
JavaScript
1
star
22

dynamically-generated-static-site

Sample dynamically generated static site
CSS
1
star
23

marko-async-fragment-wrapper

JavaScript
1
star
24

nodejs-training

JavaScript
1
star
25

ghcrawler-in-a-box

Easily start ghcrawler
JavaScript
1
star
26

property-handlers

Utility for mapping object properties to handler functions
JavaScript
1
star
27

deresolve

The inverse of require.resolve()
JavaScript
1
star
28

raptorjs3-kraken-sample-app

JavaScript
1
star