Skip to main content

Function: setImportMap()

setImportMap(map, baseDir): void

Install an import map that remaps bare specifiers (e.g. "lodash") and prefixes (e.g. "lodash/") to file paths or URLs. Equivalent to running with --import-map, but driven from JavaScript.

Must be called before the affected import / dynamic import() runs — modules already loaded into the runtime are not retroactively remapped. Only one import map is active at a time; calling setImportMap() again replaces the previous one.

Parameters

map

ImportMap

The import map. Relative targets resolve against baseDir.

baseDir

string

Directory used to resolve relative paths in map.

Returns

void

Example

tjs.setImportMap({
imports: {
'lodash': './vendor/lodash/index.js',
'lodash/': './vendor/lodash/',
'blocked': null,
},
scopes: {
'./legacy/': {
'pkg': './vendor/pkg-v1/index.js',
},
},
}, import.meta.dirname);

const _ = await import('lodash');