Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
module.exports give undefined while print module indicates it's not undefined?

I'm trying to work with a CommonJs module which exports a variable conditionally based on whether module and module.exports are undefined. The issue I'm currently having is that sometimes when I use import { something } from 'themodule' it gives me undefined something. While looking into the source code, I found the following export conditions:

if (typeof module !== 'undefined' && module.exports) {
    module.exports = Prism;
}

So I added some logs to the file to see whether the condition is satisfied or not via:

console.log('module: ', module, 'module.exports: ', module.exports)
if (typeof module !== 'undefined' && module.exports) {
    console.log('module.exports!')
    module.exports = Prism;
}

Which however gives me strange output:

module: { exports: [Getter/Setter] } module.exports: undefined

As you can see, the module log seem to indicate it has an exports property however when log module.exports separately, it says it's undefined. How is this possible ? Can someone help understand why this check is needed and why sometimes module.exports can be undefined ? Thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There are a few mostly-unrelated issues here.

The issue I'm currently having is that sometimes when I use import { something } from 'themodule' it gives me undefined something

This can occur if you have a circular dependency in your import chain. If a.js imports from b.js, and b.js imports from c.js, and c.js imports from a.js - then, for code running at the top level of those modules, there's no guarantee that code running at the top level of other modules has been executed. While you can experiment and see what's going on, a better approach would be to

  • remove the circular dependency, and/or
  • have only a single entry point in your code, from which everything else meaningful is called, so that circular dependencies downstream don't cause issues - have your downstream exports be functions, without top-level code that depends on other imports.

Which however gives me strange output:

module: { exports: [Getter/Setter] } module.exports: undefined

This means that:

  • The exports property is a getter/setter
  • When you invoked the getter, it apparently returned undefined. The property exists, but invoking the getter doesn't return anything. Eg

const obj = {
  get prop() {
  }
};

console.log(obj.hasOwnProperty('prop'));
console.log(obj.prop);


Can someone help understand why this check is needed

The check of

if (typeof module !== 'undefined' && module.exports) {

is simply to check to see if the code is being executed in the CommonJS context, and if it is, it assigns the Prism to the exports.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!