Accessing non-existent property 'padLevels' of module exports inside circular dependency
Asked Answered
A

10

71

I just > npm i -g [email protected] and > phonegap --version.
It says not only 9.0.0 but also:

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

when I > node --trace-warnings ..., I got this:

internal/modules/cjs/loader.js:883
  throw err;
  ^

Error: Cannot find module 'C:\Users\twori\...'
[90m    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)[39m
[90m    at Function.Module._load (internal/modules/cjs/loader.js:725:27)[39m
[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)[39m
[90m    at internal/main/run_main_module.js:17:47[39m {
  code: [32m'MODULE_NOT_FOUND'[39m,
  requireStack: []
}

I did only install and check version. Why does the phonegap module throw this error?
My teacher and other students go well with this, but only I can't do nothing.

Anglocatholic answered 6/11, 2020 at 11:15 Comment(2)
which node version are you using? please check using node -v – Afferent
#61407474 – Consultant
I
98

Solved Root Cause

exports inside circular dependency

πŸ‘† This phrase gave hint to solve the same issue for me, it seems that newer NodeJS versions no longer allows circular dependencies.

Eg - There are two files - FileA.js, FileB.js

Where FileA.js has

const FileB = require("FileB");

and FileB.js has

const FileA = require("FileA");

After removing one of these circular dependencies by modifying either FileA.js or FileB.js, it was solved!

Inbreed answered 9/12, 2020 at 8:10 Comment(6)
How did you find which files had circular dependencies? – Revareval
@Revareval by looking at my recent changes – Inbreed
How should I modify the file to remove the error @Yossi? – Grasmere
I didn't fix it, it doesn't cause any issues in my case – Revareval
Note that this is an example where the files DIRECTLY require each other, but a dependency can still exist indirectly. For example, FileA might not require FileB but FileA can depend on FileC and FileC depends on FileB....resulting in a circular dependency still. – Fiduciary
@Revareval try to run your node command line with --trace-warnings – Trutko
R
18

I discovered that the arrangement had no effect in the error, I tweaked the Javascript destructuring when I was calling the function in another file and it magically solved the problem.

I simply changed the way I exported the function from the Main.js

from:

module.exports = { Scan };

to:

exports.Scan = Scan

And in Event.js, I was able to access the file like this

const main = require("./Main.js");
let result = main.Scan();

This solved my problem, I hope it helps another developer 😎

Radius answered 7/6, 2022 at 17:43 Comment(1)
did you try just the latter (Event.js tweak) instead of the former? To resolve similar warning I found that I did not need to change how I export but instead just needed to avoid importing via { destructuring } = require('like-that') – Salivation
D
9

That the node version was too high. Previously, the 14+ version used by the latest was used. After switching back to the 12 version, there would be no such problem.

Mac command

sudo n 12.0.0
Drobman answered 20/1, 2021 at 8:16 Comment(6)
if you don't already have n, then npm install -g n first. Also, I don't think you should have to sudo – Edrisedrock
alternatively, nvm is a great node version manager – Unorganized
You shouldn't be using sudo on npm commands – Sclerenchyma
it gives an error mkdir: /usr/local/n: Permission denied on my Mac without sudo – Wheal
@Wheal that is because your env var NODE_PATH is not set to a user writable dir ... update that env var to some dir you have write permissions on and retry – Kennith
I had the same cause, I already have nvm so nvm use 12 solved the issue – Stunt
P
3

After I updated my node from version 12 to version 14, I also faced the same warning. In my case, this warning was coming due to an older version of surge. after updating surge, the warning was gone. If you have surge installed in your system then update it to the latest version. You can run this command to update surge to the latest version:

  sudo npm i -g surge
Percale answered 11/8, 2021 at 9:38 Comment(4)
npm update worked for me too. I guess package providers already made changes to comply with newer nodejs engines – Albion
calling sudo on npm i is a bad practice – Laurencelaurene
@a11r it's necessary in most cases when installing global packages with -g flag – Majolica
@Majolica you're right, i didn't noticed the -g flag – Laurencelaurene
S
2

The best and fastest way to find out is to install madge globally and it will tell you where the circular dependencies are located.

npm install -g madge
madge --circular .

I will give you an output like this one:

1) api/models/users.js > api/models/users_groups.js
2) helpers/date_helpers.js > api/controllers/birthday.js > helpers/format_helpers.js
Sabra answered 31/7, 2023 at 15:15 Comment(0)
S
1

This issue would generally be caused due to incompatible packages or SDKs that you are using. Best is to do npm update and update any older version SDKs you might be using. Sometime a force update using --force option may be helpful with npm install on already existing libraries.

Sodomite answered 19/5, 2022 at 6:4 Comment(0)
L
1

after npm update, the same case will resolve enter image description here

Logical answered 16/6, 2022 at 0:9 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review – Leprosarium
I
1

If somebody is looking at this thread to resolve for the given error, my issue was that I didn't update .env file on my production server before the build and that caused new packages to throw the error.

Irrefragable answered 29/12, 2022 at 2:35 Comment(0)
B
0

node.js supports circular dependencies, but it's really messy to do that, here is an example (my case)

fields.js

const $boards = require('./boards');

const $fields = {
async func() {},
async oneMoreFunc() {},
}

module.exports = $fields;

boards.js

const $fields = require('./fields');
  
const $boards = {
async func() {},
async oneMoreFunc() {},
        }
module.exports = $boards;

In my case, I wanted in fields.js import oneMoreFunc() from boards.js and saw the same error with Warning: Accessing non-existent property

so just for KISS sake:

boards.js gets exports.oneMoreFunc = $boards.oneMoreFunc;

and fields.js can now use as you expected before $boards.oneMoreFunc()

But pay attention that you still use the same circular dependencies and error can still appear there in the console, but despite this, it will work 🀫 (I do not recommend doing this because I do not want to be responsible for your technical debt πŸ˜‡)

LONG STORY SHORT: node.js does allow circular require dependencies, and in the future, there is a high probability that you will turn back to this error again and again, it's better to create a third file like utils.js where you can store your "universally demanded" functions, If A.js and B.js are always used together they are effectively a single module, so merge them. If not, so it's better to find a way to break the code into separate files.

Good luck, and I hope it will help someone πŸ™πŸ»

Beyond answered 12/3, 2023 at 10:5 Comment(0)
V
0

you need to run node --trace-warnings src/app.js or wherever your app.js file is.. then it'll display which file you are getting the circular dependancy error.

Vocalic answered 2/10, 2023 at 7:46 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.