Search code examples
node.jsjasminerequiremodule-export

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. 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

example:

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
}

Through the IDE (VSCode) all paths are complete and can be followed with 'go to definition'. 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. 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.
I know the warning mentions a circular dependency but as far as I can see there is none i.e. only 1->2->Api-> etc.

It seems as though the require in file2.js isn't happening when the function in file2.js is being called from file1.js.

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

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.
[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?]

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)

Solution

  • the solution was to add:
    delete require.cache[require.resolve('./api')]
    before the const Api = require('./api'); in file2.js

    this had something to do with Node caching the require() calls and not re-calling as was expected.

    See links for more explanation:

    How to remove module after "require" in node.js?

    node.js require() cache - possible to invalidate?