【问题标题】:Dynamic require a module by string动态地按字符串要求模块
【发布时间】:2015-12-26 03:29:01
【问题描述】:
require('Something') //working fine
require(`${dynamicName}`) //not work with node prompt for out of memory

能否建议如何根据变量 'dynamicName' 动态要求模块?

【问题讨论】:

  • require(dynamicName) 有什么问题?
  • 这是我在 ./src ^\.\/.*$ 中得到警告的错误 Module not found: Error: a dependency to an entry point is not allowed @ ./src ^\.\ /.*$
  • 您是否将正则表达式传递给require 函数?
  • 我最后一次尝试是使用 require(dynamicName) 显示错误
  • 好的,但我可以看到 dynamicName 包含一个正则表达式,这样它就行不通了。

标签: node.js webpack


【解决方案1】:

您始终可以使用自己的版本覆盖 require,因为它是一个函数。

function setupMyRequire() {
    var tmpreq = global.require;
    global.require = function(name) {
        // parse name here
        console.log('will require', name);
        tmpreq(name);
    };
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多