require returns undefined
- Version:
v13.2.0 - Platform:
Darwin Aprils-MacBook-Pro.local 16.7.0 Darwin Kernel Version 16.7.0: Sun Jun 2 20:26:31 PDT 2019; root:xnu-3789.73.50~1/RELEASE_X86_64 x86_64 - Subsystem:
a.js
const { c } = require('./c');
const { b } = require('./b');
const outputOfC = c();
const outputOfB = b();
b.js
const { c } = require('./c');
const b = c(b);
module.exports = { b };
c.js
function c () {
// ...
};
module.exports = c;
The problem is, if A requires B and C and B requires C as well, C becomes undefined when required in B.
It's actually undefined because module.exports becomes an empty object.
Can you please write an example that can be executed without crashing? There are at least two progamming errors in the provided code.
It will be undefined in a.js as well!j You just hit your error while loading b, before you try to invoke c.c()
c.js is exporting the function c. That's it, the entire export, not {c: c}, the way b.js does.
But both a.js ans b.js are trying to import c as if it exported {c: c}.
Either change c.js to be consistent with its imports (and with b.js), i.e.:
module.exports = {c};
or change a.js and b.js to import c.js correctly as it's written:
const c = require('./c');`
Just to give you a parallel perspective on the difference, and to how it relates to the future:
The way you are exporting c is roughly equivalent to writing in ECMAscript modules:
export default function c() {
// ...
}
and b.js would look like:
import c from './c';
export const b = c;
or even:
import c as b from './c';
and a.js:
import {b} from './b';
import c from './c';
So there's no bug here. You can think of require('./c') as returning whatever you set module.exports to, in this case,the function c, and so don't destructure it. But that model doesn't quite work with ECMA modules, because they become shared, live bindings and ECMA modules are not objects to be destructured, but rather a set of named bindings and an optional default (unnamed) binding. So the concepts are parallel but not equivalent. It would be better to think of this in terms of "the old equivalent of ECMA default exports".
You can have both named and unnamed exports. In this old world, that looks like this:
function c(d) {}
c.b = c;
c.data = "Hi, I am Data!";
module.exports = c;
and
const c = require('./c');
const {b, data} = c;
c(b(data));
@aprilmintacpineda - did @BobKerns 's suggestion help you to resolve the issue? or is this still outstanding?
answered, closing. feel free to re-open if it is outstanding
For someone who sillies as me :)), check if your imported file has the same name but different ext, example:
~/Province.ts ~/province.json
even correct imported with
import { Province } from "@/.../Province"; and console.log({ Province }); still give undefined, because in the Location folder, I have province.json
I had a similar issue with electron, it appeared that require was not working for no reason when I required the file.
const {app, BrowserWindow} = require('electron')
// app == undefined
// BrowserWindow == undefined
Turns out running your code with node file.js in the command line will fail. You need to use electron file.js or all electron imports will be undefined. (And not an issue with a required file requiring a dependency).
electron-quick-start b58e398