I have a file with 3 objects, and I want to import each object in a different file.
const object1 {
s1: {
title: "placeholder title",
description: "placeholder description"
}
}
const object2 {
s1: {
title: "placeholder title",
description: "placeholder description"
}
}
const object3 {
s1: {
title: "placeholder title",
description: "placeholder description"
}
}
To export them I am using this code -
module.exports = { object1, object2, object3 }
and in my separate file to import them
import object1 from "../data";
import object2 from "../data";
import object3 from "../data";
I can see in my console logs that this doesn't load each object separately, it loads them as one big object.
How do I export/import this correctly?
module.exports.object1 = object1;. Otherwise you can also, parse your import object into separate object by doing this:importedObjectVariable.object1and so on.export object1and thenimport object1 from "../data";or usemodule.exports = { object1, object2, object3 }andconst {object1} = require('../data');. Don't try to mix and match CommonJS module syntax with ESM module syntax. While, it can be done in some circumstances, it's not easy to do it right. And, if using ESM syntax, make sure the loader knows you're using an ESM module.