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

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

node.js supports circular dependencies, but it's really messy to lớn vì thế 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 vì thế not recommend doing this because I vì thế not want to lớn 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 lớn this error again and again, it's better to lớn 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, so sánh merge them. If not, so sánh it's better to lớn find a way to lớn break the code into separate files.

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