module.createRequireFromPath(filename)


稳定性: 0 - 弃用: 改为使用 createRequire()

  • filename <string> 用于构造相对 require 函数的文件名。
  • 返回: <require> require 函数
const { createRequireFromPath } = require('module');
const requireUtil = createRequireFromPath('../src/utils/');

// 需要 `../src/utils/some-tool`
requireUtil('./some-tool');

Stability: 0 - Deprecated: Please use createRequire() instead.

  • filename <string> Filename to be used to construct the relative require function.
  • Returns: <require> Require function
const { createRequireFromPath } = require('module');
const requireUtil = createRequireFromPath('../src/utils/');

// Require `../src/utils/some-tool`
requireUtil('./some-tool');