Universal importer for CommonJS and ESM in Node.js
Installations
npm install @humanwhocodes/module-importer
Developer Guide
Typescript
Yes
Module System
ESM
Min. Node Version
>=12.22
Node Version
12.22.12
NPM Version
6.14.16
Score
98.9
Supply Chain
92.2
Quality
75.1
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Languages
JavaScript (100%)
validate.email 🚀
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Developer
humanwhocodes
Download Statistics
Total Downloads
2,373,907,593
Last Day
6,237,938
Last Week
32,817,845
Last Month
139,550,700
Last Year
1,318,627,116
GitHub Statistics
Apache-2.0 License
25 Stars
22 Commits
5 Forks
1 Watchers
8 Branches
2 Contributors
Updated on Feb 19, 2025
Bundle Size
510.00 B
Minified
301.00 B
Minified + Gzipped
Sponsor this package
Package Meta Information
Latest Version
1.0.1
Package Id
@humanwhocodes/module-importer@1.0.1
Unpacked Size
20.68 kB
Size
6.92 kB
File Count
10
NPM Version
6.14.16
Node Version
12.22.12
Total Downloads
Cumulative downloads
Total Downloads
2,373,907,593
Last Day
6.9%
6,237,938
Compared to previous day
Last Week
2.7%
32,817,845
Compared to previous week
Last Month
11%
139,550,700
Compared to previous month
Last Year
56.6%
1,318,627,116
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
9
ModuleImporter
If you find this useful, please consider supporting my work with a donation.
Description
A utility for seamlessly importing modules in Node.js regardless if they are CommonJS or ESM format. Under the hood, this uses import()
and relies on Node.js's CommonJS compatibility to work correctly. This ensures that the correct locations and formats are used for CommonJS so you can call one method and not worry about any compatibility issues.
The problem with the default import()
is that it always resolves relative to the file location in which it is called. If you want to resolve from a different location, you need to jump through a few hoops to achieve that. This package makes it easy to both resolve and import modules from any directory.
Usage
Node.js
npm install @humanwhocodes/module-importer
# or
yarn add @humanwhocodes/module-importer
Import into your Node.js project:
1// CommonJS 2const { ModuleImporter } = require("@humanwhocodes/module-importer"); 3 4// ESM 5import { ModuleImporter } from "@humanwhocodes/module-importer";
Bun
Install using this command:
bun add @humanwhocodes/module-importer
Import into your Bun project:
1import { ModuleImporter } from "@humanwhocodes/module-importer";
API
After importing, create a new instance of ModuleImporter
to start emitting events:
1// cwd can be omitted to use process.cwd() 2const importer = new ModuleImporter(cwd); 3 4// you can resolve the location of any package 5const location = importer.resolve("./some-file.cjs"); 6 7// you can also import directly 8const module = importer.import("./some-file.cjs");
For both resolve()
and import()
, you can pass in package names and filenames.
Developer Setup
- Fork the repository
- Clone your fork
- Run
npm install
to setup dependencies - Run
npm test
to run tests
License
Apache 2.0

No vulnerabilities found.

No security vulnerabilities found.