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

  • 5,000
  • Tác giả: admin
  • Ngày đăng:
  • Lượt xem: 5
  • Tình trạng: Còn hàng

node.js supports circular dependencies, but it's really messy đồ sộ bởi 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 bởi not recommend doing this because I bởi not want đồ sộ 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 đồ sộ this error again and again, it's better đồ sộ create a third tệp tin lượt thích 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, ví merge them. If not, ví it's better đồ sộ find a way đồ sộ break the code into separate files.

Good luck, and I hope it will help someone 🙏🏻