@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: {
port: messageChannel.port2,
importMap: {
imports: {},
scopes: {},
},
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({
importMap: {
imports: {},
scopes: {},
},
importMapUrl: "./node.importmap",
});
node --import ./register-hooks.js main.js