Search code examples
reactjsnext.jsimport

NextJS SyntaxError: Unexpected token 'export' on node_modules


I am trying to test/switch a Vite+React repo to NextJS+React.
After making the basic changes i got this error:

/.../repo-name/node_modules/react-router-typing/dist/index.js:1
export { generateUtils } from "./generate-utils";

SyntaxError: Unexpected token 'export'

I tried changing tsconfig from esnext to es5 but there were no changes in the error message.
The error is thrown in the react-router-typing and I can't make changes to the node_module.
The import location looks like so:

import { defineRoutes, asBrowserRouter, AsRouteConfig, generateUtils } from 'react-router-typing'

...

export const { pagePath, pageMatch } = generateUtils<RouteConfig>()

Solution

  • Add the library to thetranspilePackages in next.config.js

    /** @type {import('next').NextConfig} */
    const nextConfig = {
        reactStrictMode: true,
        transpilePackages: [
            'react-router-typing',
        ],
    }
    
    export default nextConfig
    

    Found the answer here.