Next.js SyntaxError “Unexpected token ‘export'”

UPDATE: All features of next-transpile-modules are now natively built-in Next.js 13.1. you should be able to use Next’s transpilePackages option to transpile external packages


Old Answer: So the dependency in node_modules folder exports a function using ES6 import/export module. The code will throw error when it running in browser since browser cannot recognize the ES6 module syntax.

The reason is that, by default, Next.js configs the babel-loader to only transpile the ES6 code in the src folder, any ES6 code imported from node_modules will directly go into final bundle without transpiling.

Try to modify the webpack config in next.config.js file to let the babel loader transpile the es6 dependency. You may want to use this package next-transpile-modules

Leave a Comment