Use dynamic imports just like plain old require.
With simport you can:
- get
require
- get
__filename
or__dirname
- load json
- avoid extensions
- avoid destructuring default
- pass
simport
into functions like tryToCatch - use absolute path in windows
npm i simport
Commonjs:
const {createSimport} = require('simport');
const simport = createSimport(__filename);
ESM:
import {createSimport} from 'simport';
const simport = createSimport(import.meta.url);
// you can import json
await simport('./package.json');
// returns
({
name: simport,
});
// you can avoid .js extension
await simport('./server');
// you can avoid destructure default
const validate = await simport('./validate');
// same as
const {default: validate2} = await import('./validate.js');
import {createCommons} from 'simport';
const {
__filename,
__dirname,
require,
} = createCommons(import.meta.url);
// now you have plain old CommonJS variables
MIT