module.createRequire(filename)


  • filename <string> | <URL> 用于构造 require 函数的文件名。 必须是文件网址对象、文件网址字符串、或绝对路径字符串。
  • 返回: <require> require 函数
import { createRequire } from 'module';
const require = createRequire(import.meta.url);

// sibling-module.js 是 CommonJS 模块。
const siblingModule = require('./sibling-module');
  • filename <string> | <URL> Filename to be used to construct the require function. Must be a file URL object, file URL string, or absolute path string.
  • Returns: <require> Require function
import { createRequire } from 'module';
const require = createRequire(import.meta.url);

// sibling-module.js is a CommonJS module.
const siblingModule = require('./sibling-module');