How to fix Error: module.exports is undefined (trying to use exported value that doesn't exist) in Node.js

Node.jsINTERMEDIATEMEDIUMUpdated Dec 9, 2025ยท 168 views

This error occurs when you try to access a property or function from a module that was not properly exported, or when module.exports is assigned inside a callback. It commonly happens when mixing CommonJS and ES Module syntax or importing non-existent named exports.

Quick fix

Fixes Error: module.exports is undefined (trying to use exported value that doesn't exist)

// Wrong - exports assigned in callback
const fs = require('fs');
fs.readFile('config.json', (err, data) => {
  module.exports = JSON.parse(data); // Too late!
});

// Correct - exports assigned immediately
const config = require('./config-loader');
module.exports = config;
  1. Verify exports are at module top level
  2. Check if the export actually exists
  3. Do not reassign module.exports after setting properties
  4. Ensure consistent module syntax
node.js โ€” error
// Wrong - exports assigned in callback
const fs = require('fs');
fs.readFile('config.json', (err, data) => {
module.exports = JSON.parse(data); // Too late!
});
// Correct - exports assigned immediately
const config = require('./config-loader');
module.exports = config;
Error: module.exports is undefined (trying to use exported value that doesn't exist)

What this error means

The "module.exports is undefined" error indicates that Node.js cannot find the exported value you're trying to import. In CommonJS modules, everything you want to expose must be explicitly assigned to module.exports or exports. When you require() a module, you receive whatever value was assigned to module.exports. If that assignment never happens, happens inside a callback, or if you try to destructure a named export that doesn't exist, you'll get undefined. This error is particularly common when developers try to use ES6 destructuring syntax (const { myFunction } = require('./module')) on a module that exports a single value or doesn't export the named property at all. It also occurs when reassigning module.exports after setting individual properties, which overwrites the original exports object.

Typical symptoms

  • TypeError: Cannot read property 'x' of undefined when accessing imported values
  • Function or variable from required module is undefined
  • Code works locally but fails after deployment or on other machines
  • Destructured import results in undefined values
  • Module returns undefined instead of expected object or function

Common causes

  1. Trying to import a named export that was never exported from the module
  2. Assigning module.exports inside a callback or async function
  3. Reassigning module.exports after setting individual properties
  4. Mixing CommonJS (require/module.exports) with ES Module (import/export) syntax
  5. Using destructuring to import from a module that exports a single default value
  6. Circular dependencies causing modules to be partially loaded
  7. Typo in the exported or imported property name
  8. Module was cached before exports were properly set

How to fix "Error: module.exports is undefined (trying to use exported value that doesn't exist)"

1Verify exports are at module top level

Ensure module.exports assignments happen immediately, not inside callbacks or async functions:

javascript
// Wrong - exports assigned in callback
const fs = require('fs');
fs.readFile('config.json', (err, data) => {
  module.exports = JSON.parse(data); // Too late!
});

// Correct - exports assigned immediately
const config = require('./config-loader');
module.exports = config;

If you need to export async values, export the promise or use a factory function that returns a promise:

javascript
// Export a function that returns a promise
module.exports = async function loadConfig() {
  const data = await fs.promises.readFile('config.json');
  return JSON.parse(data);
};
2Check if the export actually exists

Verify that the module exports what you're trying to import. In the exporting file:

javascript
// module.js
function myFunction() { return 'hello'; }
function anotherFunction() { return 'world'; }

// Export both functions
module.exports = { myFunction, anotherFunction };

Then import with matching names:

javascript
// main.js
const { myFunction, anotherFunction } = require('./module');
console.log(myFunction()); // 'hello'

If the module exports a single value, don't use destructuring:

javascript
// Wrong - trying to destructure single export
const { myFunction } = require('./module'); // undefined!

// Correct - import the whole export
const myFunction = require('./module');
3Do not reassign module.exports after setting properties

Once you set properties on module.exports, avoid reassigning the entire object:

javascript
// Wrong - reassignment loses previous properties
module.exports.foo = 'bar';
module.exports.baz = 'qux';
module.exports = { newValue: 'test' }; // foo and baz are now gone!

// Correct - set all properties at once
module.exports = {
  foo: 'bar',
  baz: 'qux',
  newValue: 'test'
};

// Or add properties without reassignment
exports.foo = 'bar';
exports.baz = 'qux';
exports.newValue = 'test';

Remember: exports is a reference to module.exports. Reassigning exports itself breaks this reference, so always use module.exports for full reassignments.

4Ensure consistent module syntax

Check your package.json to see which module system you're using:

json
{
  "type": "module"  // ES Modules
}

If "type": "module" is present, use ES Module syntax throughout:

javascript
// ES Module syntax
export const myFunction = () => 'hello';
export default myFunction;

// Import
import myFunction, { myFunction as named } from './module.js';

Without "type": "module" (CommonJS), use require/module.exports:

javascript
// CommonJS syntax
module.exports = { myFunction };

// Import
const { myFunction } = require('./module');

Mixing these syntaxes causes compatibility issues and undefined exports.

5Clear Node.js require cache

If modules were previously loaded with incorrect exports, Node.js may have cached the old version. Clear the cache by restarting your application or manually clearing it:

javascript
// Clear cache for a specific module (development only)
delete require.cache[require.resolve('./module')];

// Then require again
const myModule = require('./module');

In development, tools like nodemon automatically restart Node.js when files change, which clears the cache. For production, simply restart the Node.js process.

If using a transpiler (TypeScript, Babel), ensure you rebuild before testing:

bash
npm run build
node dist/index.js

Advanced notes

Circular dependencies: When module A requires module B, and module B requires module A, one of them will receive an incomplete exports object. To diagnose, add console.log statements in both modules to see the load order. Refactor to remove the circular dependency by extracting shared code into a third module.

Using `this` in exports: When exporting object methods that reference this, the context may not be what you expect. Arrow functions inherit this from the enclosing scope, while regular functions have their own this. Explicitly bind methods or use regular functions assigned to module.exports.

TypeScript interop: When importing CommonJS modules in TypeScript with esModuleInterop: true, you can use import module from 'module' syntax. Without this flag, use import * as module from 'module'. Ensure your tsconfig.json module settings match your runtime environment.

Webpack and bundlers: Module bundlers transform require() calls during build time. If exports are conditionally assigned or computed at runtime, the bundler may not capture them correctly. Use static exports whenever possible.

Default exports in CommonJS: When a CommonJS module uses module.exports = value (single value), importing it with ES Module syntax may require accessing .default: import mod from './module'; const value = mod.default; depending on your transpiler configuration.

Frequently asked questions

What does "Error: module.exports is undefined (trying to use exported value that" mean?
js cannot find the exported value you're trying to import. exports or exports. exports.
How do I fix "Error: module.exports is undefined (trying to use exported value that"?
This error occurs when you try to access a property or function from a module that was not properly exported, or when module.exports is assigned inside a callback. It commonly happens when mixing CommonJS and ES Module syntax or importing non-existent named exports.
What causes "Error: module.exports is undefined (trying to use exported value that"?
Trying to import a named export that was never exported from the module Assigning module.exports inside a callback or async function Reassigning module.exports after setting individual properties
Is the "Error: module.exports is undefined (trying to use exported value that" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading