DEFRA / sroc-service-team

Guides, info and issue management for the Charging Module Team
Other
0 stars 1 forks source link

JwtStrategyAuth '... module exports inside circular dependency' #66

Open Cruikshanks opened 3 years ago

Cruikshanks commented 3 years ago

Whilst working on Fix missing customer validations we came across an issue with const { StaticLookup } = require('../lib').

When we added that to the top of app/translators/customer.translator.js we got the warning

(node:1340) Warning: Accessing non-existent property 'StaticLookup' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)

When we actually tried to use it we got the error Cannot read property 'regions' of undefined thrown.

By running the test with --trace-warnings on we got this

/home/node/app # NODE_ENV=test node --trace-warnings /home/node/node_modules/.bin/lab --silent-skips --shuffle
(node:1211) Warning: Accessing non-existent property 'StaticLookup' of module exports inside circular dependency
    at emitCircularRequireWarning (internal/modules/cjs/loader.js:650:11)
    at Object.get (internal/modules/cjs/loader.js:664:5)
    at Object.<anonymous> (/home/node/app/app/translators/customer.translator.js:7:92)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.require.extensions.<computed> [as .js] (/home/node/node_modules/@hapi/lab/lib/modules/coverage.js:122:36)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/home/node/app/app/translators/index.js:7:97)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.require.extensions.<computed> [as .js] (/home/node/node_modules/@hapi/lab/lib/modules/coverage.js:122:36)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/home/node/app/app/services/calculate_charge.service.js:7:148)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.require.extensions.<computed> [as .js] (/home/node/node_modules/@hapi/lab/lib/modules/coverage.js:122:36)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/home/node/app/app/services/index.js:7:98)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.require.extensions.<computed> [as .js] (/home/node/node_modules/@hapi/lab/lib/modules/coverage.js:122:36)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/home/node/app/app/lib/jwt_strategy.js:4:104)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.require.extensions.<computed> [as .js] (/home/node/node_modules/@hapi/lab/lib/modules/coverage.js:122:36)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/home/node/app/app/lib/index.js:4:86)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.require.extensions.<computed> [as .js] (/home/node/node_modules/@hapi/lab/lib/modules/coverage.js:122:36)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/home/node/app/server.js:5:83)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.require.extensions.<computed> [as .js] (/home/node/node_modules/@hapi/lab/lib/modules/coverage.js:122:36)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/home/node/app/test/controllers/admin/authorised_systems.controller.test.js:12:24)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.require.extensions.<computed> [as .js] (/home/node/node_modules/@hapi/lab/lib/modules/coverage.js:127:28)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at /home/node/node_modules/@hapi/lab/lib/cli.js:135:13
    at Array.forEach (<anonymous>)
    at Object.internals.traverse (/home/node/node_modules/@hapi/lab/lib/cli.js:129:15)
    at Object.exports.run (/home/node/node_modules/@hapi/lab/lib/cli.js:60:31)
    at main (/home/node/node_modules/@hapi/lab/bin/lab:56:48)
    at Object.<anonymous> (/home/node/node_modules/@hapi/lab/bin/lab:60:1)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

The reference that stood out was at Object.<anonymous> (/home/node/app/app/lib/jwt_strategy.js:4:104). Commenting out the JwtStrategyAuth entry in app/lib/index.js made the warning and error go away.

Line 4 of app/lib/jwt_strategy.js is an import of a service. So, we wonder if a service is requiring StaticLookup, and something about the chain is putting node out of sorts.

This will take some noodling to figure out but it may cause come up again so recording it here until we do.

To run the tests with --trace-warnings on OPEN the running docker container and then run NODE_ENV=test node --trace-warnings /home/node/node_modules/.bin/lab --silent-skips --shuffle