Instantly share code, notes, and snippets.

What would you like to do?
Node.js: createRequireFromPath
module.exports = 'cjs';
import {createRequireFromPath as createRequire} from 'module';
import {fileURLToPath as fromPath} from 'url';
const require = createRequire(fromPath(import.meta.url));
const cjs = require('./cjs');
cjs === 'cjs'; // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment