Skip to content

node-loader/import-maps

 
 

Repository files navigation

@node-loader/import-maps

NodeJS customization hooks to add support for import maps to NodeJS. This allows you to customize module resolution by creating a node.importmap file.

Installation

npm install --save @node-loader/import-maps pnpm install --save @node-loader/import-maps

Usage

Create a file (e.g. node.importmap) in the current working directory:

{ "imports": { "my-module": "file:///home/name/code/my-module.js" } }

Now create a file main.js that imports the mapped module:

import "my-module";

Now create a startup module register-hooks.js:

import { register } from "node:module"; import { MessageChannel } from "node:worker_threads"; const messageChannel = new MessageChannel(); global.importMapPort = messageChannel.port1; register("@node-loader/import-maps", { data: { // optional, provides a way to update the import map later on port: messageChannel.port2, // optional, import map object importMap: { imports: {}, scopes: {}, }, // optional, file url resolved relative to current working directory // This option is ignored if importMap is provided importMapUrl: "./node.importmap", }, transferList: [messageChannel.port2], });

Now run main.js with the --import NodeJS flag:

To dynamically change the import map after startup, do the following:

global.importMapPort.postMessage({ // Provide either importMap or importMapUrl, but not both importMap: { imports: {}, scopes: {}, }, importMapUrl: "./node.importmap", });
node --import ./register-hooks.js main.js

About

A nodejs loader for import maps

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •