节点模块导出,访问不存在的属性错误,Jasmine
Posted
技术标签:
【中文标题】节点模块导出,访问不存在的属性错误,Jasmine【英文标题】:node module exports, Accessing non-existent property error, Jasmine 【发布时间】:2021-09-29 19:51:18 【问题描述】:我正在使用客户的遗留代码,它是一个用于 Jasmine 测试的 node.js 项目。
我的问题是,当我运行测试时,调用的函数(在不同的文件中)找不到/查看使用require
导入文件的特定方法
示例:
file1.js
const file2 = require('./file2');
fdescribe('Login', file2.login);
file2.js
const Api = require('./api');
module.exports =
login()
let user;
beforeAll(async () =>
user = await Api.createUser(
first_name: 'login test',
password: 'XXXX',
,
Api.USER_ROLE.ADMIN);
// more code within function...
);
// other functions
通过 IDE (VSCode),所有路径都是完整的,可以在后面加上“转到定义”。另外作为测试,我将登录功能的内容放入 file1.js 的描述块中,更新依赖关系,一切正常。但是我的老板和客户希望保持目前的结构。
当我按此处所示运行时,我收到以下警告:
(node:6021) Warning: Accessing non-existent property 'createUser' of module exports inside circular dependency
(node:6021) Warning: Accessing non-existent property 'USER_ROLE' of module exports inside circular dependency
代码运行,但 createUser
和 USER_ROLE
出现“无法读取未定义的属性”错误。
我知道警告提到了循环依赖,但据我所知没有,即只有 1->2->Api-> 等。
当从 file1.js 调用 file2.js 中的函数时,似乎 file2.js 中的 require 没有发生。
在没有更多高级支持的情况下,我相当初级/缺乏经验,因此我在这里联系,可能是一个相当简单的问题。
项目正在运行node version 14.17.0
Api.js 文件更像是一个库文件,它“链接”到本例中包含 createUser
和 USER_ROLE
的其他文件。
[因为这是遗留的客户端代码,并且它在过去的某个时候工作过,我认为这可能与 Jasmine 或 Node JS 版本有关?]
节点警告/错误的更多细节:
(node:1693) Warning: Accessing non-existent property 'createUser' of module exports inside circular dependency
at emitCircularRequireWarning (internal/modules/cjs/loader.js:675:11)
at Object.get (internal/modules/cjs/loader.js:689:5)
at UserContext.<anonymous> (/home/runner/work/platform-validation/platform-validation/utils/dupes.js:45:24)
at QueueRunner.attempt (/home/runner/work/platform-validation/platform-validation/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:7756:40)
at QueueRunner.run (/home/runner/work/platform-validation/platform-validation/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:7798:25)
SYS.20: Login
✗ User can login
- TypeError: Cannot read property 'login' of undefined
at runNext (/home/runner/work/platform-validation/platform-validation/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:7715:18)
at next (/home/runner/work/platform-validation/platform-validation/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:7722:11)
at QueueRunner.onComplete (/home/runner/work/platform-validation/platform-validation/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:7615:9)
at Immediate.<anonymous> (/home/runner/work/platform-validation/platform-validation/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:7814:12)
at processImmediate (internal/timers.js:464:21)
(node:1693) Warning: Accessing non-existent property 'USER_ROLE' of module exports inside circular dependency
at emitCircularRequireWarning (internal/modules/cjs/loader.js:675:11)
at Object.get (internal/modules/cjs/loader.js:689:5)
at UserContext.<anonymous> (/home/runner/work/platform-validation/platform-validation/utils/dupes.js:49:11)
at QueueRunner.attempt (/home/runner/work/platform-validation/platform-validation/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:7756:40)
at QueueRunner.run (/home/runner/work/platform-validation/platform-validation/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:7798:25)
at runNext (/home/runner/work/platform-validation/platform-validation/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:7715:18)
at next (/home/runner/work/platform-validation/platform-validation/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:7722:11)
at QueueRunner.onComplete (/home/runner/work/platform-validation/platform-validation/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:7615:9)
at Immediate.<anonymous> (/home/runner/work/platform-validation/platform-validation/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:7814:12)
at processImmediate (internal/timers.js:464:21)
【问题讨论】:
Node 抱怨未定义的属性是什么?如果是createUser
和USER_ROLE
,最好检查一下你的 api.js 文件
【参考方案1】:
解决方案是添加:delete require.cache[require.resolve('./api')]
在file2.js
中的const Api = require('./api');
之前
这与 Node 缓存 require() 调用而不是按预期重新调用有关。
更多解释见链接:
How to remove module after "require" in node.js?
node.js require() cache - possible to invalidate?
【讨论】:
以上是关于节点模块导出,访问不存在的属性错误,Jasmine的主要内容,如果未能解决你的问题,请参考以下文章
(节点:52213)警告:在循环依赖中访问模块导出的不存在属性“MongoError”(使用节点--trace-warnings
错误:警告:在循环依赖中访问模块导出的不存在属性“findOne”
在循环依赖中访问模块导出的不存在的属性“padLevels”
Yeoman - 在循环依赖中访问模块导出的不存在属性____