Solving Next.js SyntaxError: Unexpected token ‘export’

Updated: January 2, 2024 By: Guest Contributor Post a comment

If you are developing with Next.js, you might encounter the SyntaxError: Unexpected token ‘export’ error. This issue often occurs due to the use of ES6 modules or incompatible node module versions. Since Next.js utilizes server-side rendering, managing imports and exports can sometimes lead to unexpected behaviors, particularly when a package or a file does not use the CommonJS syntax that is expected in a Node.js environment.

Understanding the Cause of the Error

When you see a SyntaxError complaining about an unexpected ‘export’, you are mostly dealing with an incompatibility between module types. Next.js and Node.js default to CommonJS modules. However, if part of your code or one of the imported modules uses ES6 module syntax (such as export and import statements), Node.js might throw this error because it expects the CommonJS syntax (module.exports and require() statements).

Native Node Support for ES Modules

In more recent versions of Node.js, ES Modules are supported out-of-the-box. Ensuring that you’re using a compatible version of Node.js might solve your problem. If you’re working with an older version, you can enable support by adding "type": "module" in your project’s package.json. But caution is advised: switching a Next.js project to ES6 modules may require rewriting parts of your application or affect dependencies not written to use ES6 modules.

Transpiling Dependencies

If the syntax error points to node_modules, it’s likely due to a third-party dependency published as ES6. You can configure Next.js to transpile these dependencies using the next-transpile-modules package or with custom babel configurations. To use next-transpile-modules, you would install it as a dependency, import it in your next.config.js file, and include the problematic modules in its function call.

const withTM = require('next-transpile-modules')(['module-name']);

module.exports = withTM({
  // Additional Next.js configurations...
});

CommonJS and ES6 Interop

For cases where you control the source of the error, modify your export statements to adhere to CommonJS when dealing with Node.js backend code or the parts of the application that get executed on the server. That means changing export default and export const statements to their CommonJS equivalents. Here’s what that conversion might look like:

// Before (ES6)
export const functionName = () => { /*...*/ };

export default className;

// After (CommonJS)
const functionName = () => { /*...*/ };
module.exports.functionName = functionName;

module.exports = className;

Checking the Source Code

After modifying your code or third-party modules, ensure you check through your source to find instances where module syntax issues could potentially arise. This can be a labor-intensive process, but there are tools and linters that can help identify incorrect usage of imports and exports. After resolving these issues, your application should start working again if there were no other underlying problems.