source: imaps-frontend/node_modules/@humanwhocodes/module-importer/README.md@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.0 KB
Line 
1# ModuleImporter
2
3by [Nicholas C. Zakas](https://humanwhocodes.com)
4
5If you find this useful, please consider supporting my work with a [donation](https://humanwhocodes.com/donate).
6
7## Description
8
9A 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.
10
11The 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.
12
13## Usage
14
15### Node.js
16
17Install using [npm][npm] or [yarn][yarn]:
18
19```
20npm install @humanwhocodes/module-importer
21
22# or
23
24yarn add @humanwhocodes/module-importer
25```
26
27Import into your Node.js project:
28
29```js
30// CommonJS
31const { ModuleImporter } = require("@humanwhocodes/module-importer");
32
33// ESM
34import { ModuleImporter } from "@humanwhocodes/module-importer";
35```
36
37### Bun
38
39Install using this command:
40
41```
42bun add @humanwhocodes/module-importer
43```
44
45Import into your Bun project:
46
47```js
48import { ModuleImporter } from "@humanwhocodes/module-importer";
49```
50
51## API
52
53After importing, create a new instance of `ModuleImporter` to start emitting events:
54
55```js
56// cwd can be omitted to use process.cwd()
57const importer = new ModuleImporter(cwd);
58
59// you can resolve the location of any package
60const location = importer.resolve("./some-file.cjs");
61
62// you can also import directly
63const module = importer.import("./some-file.cjs");
64```
65
66For both `resolve()` and `import()`, you can pass in package names and filenames.
67
68## Developer Setup
69
701. Fork the repository
712. Clone your fork
723. Run `npm install` to setup dependencies
734. Run `npm test` to run tests
74
75## License
76
77Apache 2.0
78
79[npm]: https://npmjs.com/
80[yarn]: https://yarnpkg.com/
Note: See TracBrowser for help on using the repository browser.