简体   繁体   English

节点模块导出,访问不存在的属性错误,Jasmine

[英]node module exports, Accessing non-existent property error, Jasmine

I'm working with a clients legacy code, it's a node.js project for Jasmine tests.我正在处理客户遗留代码,它是用于 Jasmine 测试的 node.js 项目。 My issue is that when I run the test the called function (in a different file) cannot find/see specific methods which are imported into the file using require我的问题是,当我运行测试时,被调用的函数(在不同的文件中)无法找到/查看使用require导入到文件中的特定方法

example:例子:

file1.js文件1.js

const file2 = require('./file2');

fdescribe('Login', file2.login);

file2.js文件2.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
}

Through the IDE (VSCode) all paths are complete and can be followed with 'go to definition'.通过 IDE (VSCode),所有路径都是完整的,可以通过“转到定义”进行跟踪。 Also as a test I put the contents of the login function into the describe block in file1.js, update the dependancies and everything works fine.同样作为测试,我将登录函数的内容放入 file1.js 中的 describe 块,更新依赖项,一切正常。 however my boss & client want to keep the current structure.但是我的老板和客户想保持当前的结构。

when I run as shown here I get the following warnings:当我按照此处所示运行时,我收到以下警告:

(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

The code runs but with 'cannot read property of undefined' errors for createUser and USER_ROLE .代码运行,但createUserUSER_ROLE出现“无法读取未定义属性”错误。
I know the warning mentions a circular dependency but as far as I can see there is none ie only 1->2->Api-> etc.我知道警告提到了循环依赖,但据我所知,没有,即只有 1->2->Api-> 等。

It seems as though the require in file2.js isn't happening when the function in file2.js is being called from file1.js.当从file1.js 调用file2.js 中的函数时,似乎file2.js 中的require 没有发生。

I am fairly junior/inexperienced without more senior support hence me reaching out on here, for what may be a fairly simple problem.我是初级/缺乏经验,没有更多高级支持,因此我在这里伸出援手,这可能是一个相当简单的问题。

Project is running node version 14.17.0项目正在运行node version 14.17.0

The Api.js file is more like a library file that 'links' to the other files which house in this case createUser and USER_ROLE . Api.js 文件更像是一个库文件,它“链接”到在这种情况下包含createUserUSER_ROLE的其他文件。
[as this is legacy client code and it worked at some point in the past, I'm thinking that it may be something to do with either Jasmine or Node JS versions?] [因为这是遗留的客户端代码并且它在过去的某个时候工作过,我认为它可能与 Jasmine 或 Node JS 版本有关?]

more details of node warnings/errors:节点警告/错误的更多详细信息:

(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)

the solution was to add:解决方案是添加:
delete require.cache[require.resolve('./api')]
before the const Api = require('./api');const Api = require('./api'); in file2.jsfile2.js

this had something to do with Node caching the require() calls and not re-calling as was expected.这与 Node 缓存 require() 调用而不是按预期重新调用有关。

See links for more explanation:更多解释见链接:

How to remove module after "require" in node.js?如何在node.js中的“require”之后删除模块?

node.js require() cache - possible to invalidate? node.js require() 缓存 - 可能失效?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:警告:在循环依赖中访问模块导出的不存在属性“findOne” - Error: Warning: Accessing non-existent property 'findOne' of module exports inside circular dependency (节点:52213)警告:在循环依赖中访问模块导出的不存在属性“MongoError”(使用节点--trace-warnings - (node:52213) Warning: Accessing non-existent property 'MongoError' of module exports inside circular dependency (Use node --trace-warnings Firebase 在循环依赖中访问模块导出的不存在属性“db” - Firebase Accessing non-existent property 'db' of module exports inside circular dependency 在循环依赖NodeJS中访问module.exports不存在的属性 - Accessing non-existent property of module.exports inside circular dependency NodeJS 警告:在循环依赖 #4664 中访问模块导出的不存在属性“lineno” - Warning: Accessing non-existent property 'lineno' of module exports inside circular dependency #4664 警告:在循环依赖中访问模块导出的不存在的属性“sequelize” - Warning: Accessing non-existent property 'sequelize' of module exports inside circular dependency 在循环依赖中访问模块导出的不存在的属性“padLevels” - Accessing non-existent property 'padLevels' of module exports inside circular dependency Yeoman - 访问循环依赖中模块导出的不存在的属性 ____ - Yeoman - Accessing non-existent property ____ of module exports inside circular dependency 警告:在循环依赖中访问模块导出的不存在属性“MongoError” - Warning: Accessing non-existent property 'MongoError' of module exports inside circular dependency 从 12v 到 16v 的节点更新问题 - 在循环依赖中访问模块导出的不存在属性“拼接” - Node update from 12v to 16v issue - Accessing non-existent property 'splice' of module exports inside circular dependency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM