App Module Path is a simple Node.js utility that enables adding additional directories to the Node.js module search path for top-level application modules. This allows application-level modules to be required as if they were installed into the node_modules directory, while maintaining proper isolation to prevent installed modules from accessing application code.
npm install app-module-path --saveconst { addPath, removePath, enableForDir } = require('app-module-path');For automatic path registration:
// Auto-registers the directory containing the requiring module
require('app-module-path/register');
// Auto-registers the current working directory
require('app-module-path/cwd');ES6 imports:
import { addPath, removePath, enableForDir } from 'app-module-path';// Auto-registration patterns
import 'app-module-path/register';
import 'app-module-path/cwd';// IMPORTANT: Add this to the very beginning of your main script
require('app-module-path').addPath(__dirname);
// Now you can require modules from your application directory structure
// as if they were installed in node_modules
const myModule = require('src/my-module');
const anotherModule = require('lib/utils/helper');Alternative automatic registration:
// Automatically adds the directory containing this file to the search path
require('app-module-path/register');
// Or automatically adds the current working directory
require('app-module-path/cwd');App Module Path works by overriding Node.js's built-in Module._nodeModulePaths method to extend the module resolution system:
node_modules are restricted from accessing application codenode_modules to use extended pathsAdd and remove directories from the Node.js module search path.
/**
* Adds a directory path to the Node module search path
* @param {string} path - The directory path to add to the module search path
* @param {Module} [parent] - The parent module context (defaults to module.parent)
*/
function addPath(path, parent);
/**
* Removes a directory path from the Node module search path
* @param {string} path - The directory path to remove from the module search path
*/
function removePath(path);Usage Examples:
const { addPath, removePath } = require('app-module-path');
const path = require('path');
// Add application source directory to module path
addPath(path.join(__dirname, 'src'));
// Add multiple directories
addPath(path.join(__dirname, 'lib'));
addPath(path.join(__dirname, 'app'));
// Remove a path when no longer needed
removePath(path.join(__dirname, 'src'));Explicitly enable app-module-path functionality for specific directories, including those within node_modules.
/**
* Explicitly enables app-module-path functionality for a specific directory
* @param {string} dir - The directory path to enable for app-module-path resolution
*/
function enableForDir(dir);Usage Example:
const { enableForDir } = require('app-module-path');
const path = require('path');
// Allow a specific installed package to access application modules
const packageDir = path.dirname(require.resolve('some-installed-package'));
enableForDir(packageDir);Convenience entry points that automatically register paths without explicit function calls.
register.js Entry Point:
// Automatically calls addPath(dirname(module.parent.filename), module.parent)
require('app-module-path/register');Equivalent to:
const { addPath } = require('app-module-path');
const path = require('path');
addPath(path.dirname(__filename));cwd.js Entry Point:
// Automatically calls addPath(process.cwd())
require('app-module-path/cwd');Equivalent to:
const { addPath } = require('app-module-path');
addPath(process.cwd());Usage Examples:
// At the top of your main application file
require('app-module-path/register');
// Now you can require modules relative to your application root
const utils = require('src/utils');
const config = require('config/settings');// For projects where modules should be resolved from the CWD
require('app-module-path/cwd');
const appModule = require('app/main-module');node_modules take precedencenode_modules cannot access application-level modules unless explicitly enabled via enableForDirregister.js and cwd.js remove themselves from the require cache to ensure the parent module stays up-to-date