question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug: SyntaxError: Unexpected token 'export' with Next.js

See original GitHub issue

Prerequisites

Ionic Framework Version

  • v4.x
  • v5.x
  • v6.x

Current Behavior

When updating to Ionic 6 and trying to run the app, I get this error, ServiceError: Unexpected token 'export'.

index.js?4755:323 Uncaught     at <path-to-my-app>/node_modules/ (ionic/core/components/index.js:4)
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (file:///<path-to-my-app>/node_modules/@ionic/react/dist/index.js:6:20)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)

Previously, my app was using Ionic 5 with Next.js. Following the guidelines to migrate to Ionic 6, The only thing I’ve changed was to add the setupIonicReact() call as noted as required in v6. Without adding this call, the app still fails to load. The same error just manifests differently.

Expected Behavior

Expected to update from Ionic 5 to Ionic 6 and still run successfully.

Steps to Reproduce

Add Ionic 6 to Next.js project.

Code Reproduction URL

No response

Ionic Info

I am not using Ionic CLI for anything as this is a Next.js app. I am only using Ionic as a UX framework.

[WARN] You are not in an Ionic project directory. Project context may be
       missing.

Ionic:

   Ionic CLI : 6.18.1

Utility:

   cordova-res : not installed globally
   native-run  : not installed globally

System:

   NodeJS : v16.13.1
   npm    : 8.1.2
   OS     : Linux 5.11

Additional Information

As mentioned, I am not using Ionic CLI for anything as this is a Next.js app. I am only using Ionic as a UX framework. I am trying to use @ionic/react for styling and components only.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
liamdebeasicommented, Jan 21, 2022

The issue mentioned in https://github.com/ionic-team/ionic-framework/issues/24481#issuecomment-1017027881 is caused by https://github.com/ionic-team/stencil/issues/3208. We are definitely interested in resolving this and are working closely with the Stencil team.

1reaction
liamdebeasicommented, Jan 19, 2022

Thanks! The issue here appears to be related to the default behavior of Next.js, not Ionic: https://stackoverflow.com/questions/65936222/next-js-syntaxerror-unexpected-token-export

By default Next.js transpiles ES6 code in the src folder and ignores anything in the node_modules directory. This issue is similar to the testing updates required for Jest and Ionic 6: https://ionicframework.com/docs/intro/upgrading-to-ionic-6#testing.

The recommended approach on https://github.com/vercel/next.js/discussions/32223 seems to be to use https://www.npmjs.com/package/next-transpile-modules.

Your next.config.js file should look like this:

const withTM = require('next-transpile-modules')(['@ionic/react', '@ionic/core', '@stencil/core', 'ionicons']);

/** @type {import('next').NextConfig} */
module.exports = withTM({
  reactStrictMode: true,
});

However, in doing that you will likely get a window is not defined error in ion-col.js. That is a separate issue we are tracking in https://github.com/ionic-team/ionic-framework/issues/24446. We are actively investigating and hope to have a fix for that soon.


Can you try the next-transpile-module fix and let me know if it resolves the Unexpected token 'export' error?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js SyntaxError "Unexpected token 'export'" - Stack Overflow
The code will throw error when it running in browser since browser cannot recognize the ES6 module syntax. The reason is that, by...
Read more >
SyntaxError: Unexpected token 'export' #31518 - vercel/next.js
Describe the Bug. Nextjs is failing on build of a third party package. The third party package is using ES6 syntax for exports....
Read more >
Fixing "SyntaxError: Unexpected token 'export'" with NextJS
Fixing “SyntaxError: Unexpected token ”export”” with NextJS. I ran into an error when trying to use reactgrid with NextJS. The error was
Read more >
SyntaxError: Unexpected token 'export' in ScrollTrigger (Next.js)
Hello, I've used similar code in react before, but for some reason in Next.js, even simply importing ScrollTrigger causes this error.
Read more >
[Solved] SyntaxError: Unexpected token '.' Jest Error for Next.js ...
This error could be caused by a number of issues and is related to Jest wanting to process CommonJS code. But it is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found