Big update!Introducing GitHub Bot Commands. Learn more
Socket
Log inDemoInstall

@humanwhocodes/module-importer

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Issues
File Explorer

Advanced tools

@humanwhocodes/module-importer

Universal module importer for Node.js

    1.0.1latest
    Github

Version published
Maintainers
1
Weekly downloads
10,639,621
increased by11.59%

Weekly downloads

Changelog

Source

1.0.1 (2022-08-18)

Bug Fixes

  • Ensure CommonJS mode works correctly. (cf54a0b), closes #6

Readme

Source

ModuleImporter

by Nicholas C. Zakas

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

Install using npm or yarn:

npm install @humanwhocodes/module-importer # or yarn add @humanwhocodes/module-importer

Import into your Node.js project:

// CommonJS const { ModuleImporter } = require("@humanwhocodes/module-importer"); // ESM import { ModuleImporter } from "@humanwhocodes/module-importer";

Bun

Install using this command:

bun add @humanwhocodes/module-importer

Import into your Bun project:

import { ModuleImporter } from "@humanwhocodes/module-importer";

API

After importing, create a new instance of ModuleImporter to start emitting events:

// cwd can be omitted to use process.cwd() const importer = new ModuleImporter(cwd); // you can resolve the location of any package const location = importer.resolve("./some-file.cjs"); // you can also import directly const module = importer.import("./some-file.cjs");

For both resolve() and import(), you can pass in package names and filenames.

Developer Setup

  1. Fork the repository
  2. Clone your fork
  3. Run npm install to setup dependencies
  4. Run npm test to run tests

License

Apache 2.0

Keywords

FAQs

What is @humanwhocodes/module-importer?

Universal module importer for Node.js

Is @humanwhocodes/module-importer popular?

The npm package @humanwhocodes/module-importer receives a total of 10,110,188 weekly downloads. As such, @humanwhocodes/module-importer popularity was classified as popular.

Is @humanwhocodes/module-importer well maintained?

We found that @humanwhocodes/module-importer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 1 open source maintainer collaborating on the project.

Last updated on 18 Aug 2022

Did you know?

Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install Socket
Socket[email protected]

Product

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc