首页 > 解决方案 > 如何在nodejs中的函数之外评估()?

问题描述

我不确定这是否可能,但我正在寻找 eval 并在调用它的函数范围之外访问 eval 代码。

我当前的代码:

test.importDependencies = () => 'function myDep() {}'
eval(test.importDependencies())
myDep()

我希望它是什么:

test.importDependencies = () => 'function myDep() {}'
test.importDependencies()
myDep()

这样做的原因是我正在为 Google App Script 编写代码,它不使用 require,一切都在全局范围内。但是我想在节点中编写一些单元测试,并且我不想在我的代码中使用 require 语句,因为它会使我的构建过程复杂化。

标签: node.js

解决方案


推荐阅读