Import a module like with require() but from a given path
npx @tessl/cli install tessl/npm-import-from@4.0.0Import From provides a simple utility for importing Node.js modules from a specific directory path, similar to the built-in require() function but with the ability to specify the base directory for module resolution. It offers both a standard version that throws errors when modules can't be found and a silent version that returns undefined instead.
npm install import-fromimport importFrom = require('import-from');CommonJS:
const importFrom = require('import-from');const importFrom = require('import-from');
// Import a module from a specific directory
try {
const chalk = importFrom('./some-directory', 'chalk');
console.log(chalk.blue('Hello world'));
} catch (error) {
console.log('Module not found:', error.message);
}
// Silent import - returns undefined instead of throwing
const lodash = importFrom.silent('./some-directory', 'lodash');
if (lodash) {
console.log(lodash.capitalize('hello'));
} else {
console.log('Module not available');
}Import From uses Node.js's built-in createRequire API to create custom require functions that resolve modules from a specified directory. The core design pattern involves:
path.resolve() to convert the target directory into an absolute pathcreateRequire() with a dummy file path (noop.js) in the target directoryThis approach leverages Node.js's native module system while allowing custom resolution contexts, making it a lightweight wrapper around standard module loading behavior.
Imports a module from a specified directory path, similar to require() but with custom base directory resolution.
/**
* Import a module like with require() but from a given path
* @param fromDirectory - Directory to import from
* @param moduleId - What you would use in require()
* @returns The imported module
* @throws MODULE_NOT_FOUND error when the module can't be found
*/
function importFrom(fromDirectory: string, moduleId: string): unknown;Usage Examples:
const importFrom = require('import-from');
// Import a relative module from a specific directory
const helper = importFrom('./src/utils', './helper');
// Import an npm package that's installed in a specific directory
const express = importFrom('./my-project', 'express');
// Import with path resolution
const config = importFrom('/absolute/path/to/project', './config.json');Imports a module from a specified directory path, returning undefined instead of throwing when the module cannot be found.
/**
* Import a module like with require() but from a given path
* @param fromDirectory - Directory to import from
* @param moduleId - What you would use in require()
* @returns The imported module or undefined if not found
*/
importFrom.silent(fromDirectory: string, moduleId: string): unknown;Usage Examples:
const importFrom = require('import-from');
// Safely attempt to import an optional dependency
const optionalDep = importFrom.silent('./plugins', 'optional-plugin');
if (optionalDep) {
optionalDep.activate();
}
// Check for module availability without error handling
const devTool = importFrom.silent('./dev-tools', 'debug-helper');
const debugMode = !!devTool;
// Graceful fallback for missing modules
const preferredLib = importFrom.silent('./libs', 'preferred-lib') ||
importFrom.silent('./libs', 'fallback-lib');Create a bound function for importing from the same directory multiple times:
const importFrom = require('import-from');
// Create a partial function for a specific directory
const importFromFoo = importFrom.bind(null, 'foo');
// Use the bound function multiple times
const bar = importFromFoo('./bar');
const baz = importFromFoo('./baz');The standard importFrom function throws standard Node.js MODULE_NOT_FOUND errors:
const importFrom = require('import-from');
try {
const missing = importFrom('./directory', './missing-module');
} catch (error) {
console.log(error.code); // 'MODULE_NOT_FOUND'
console.log(error.message); // "Cannot find module './missing-module'"
}createRequire API from the module modulepath.resolve() to handle directory path resolutiondeclare const importFrom: {
/**
* Import a module like with require() but from a given path
* @param fromDirectory - Directory to import from
* @param moduleId - What you would use in require()
* @throws Like require(), throws when the module can't be found
*/
(fromDirectory: string, moduleId: string): unknown;
/**
* Import a module like with require() but from a given path
* @param fromDirectory - Directory to import from
* @param moduleId - What you would use in require()
* @returns undefined instead of throwing when the module can't be found
*/
silent(fromDirectory: string, moduleId: string): unknown;
};
export = importFrom;