module.createRequire(filename)


  • filename <string> | <URL> 用于构造 require 函数的文件名。必须是文件网址对象、文件网址字符串、或绝对路径字符串。

    ¥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.

  • 返回:<require> require 函数

    ¥Returns: <require> Require function

import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);

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

Node.js 中文网 - 粤ICP备13048890号