SyntaxError: Unexpected token export when using NextJS
See original GitHub issueDescribe the bug Error appears when I trying to import any react-syntax-highlighter styles in NextJS app.
To Reproduce Steps to reproduce the behavior:
- Create NextJS app using ‘create-next-app’
- Import any react-syntax-highlighter style.
Expected behavior Working Syntax Highlight.
Desktop (please complete the following information):
- OS: ArchLinux
- Browser: Chrome
- Version: 77
Additional context Full error message:
Unexpected token export
/home/vista1nik/Documents/es-snippets/node_modules/react-syntax-highlighter/dist/esm/styles/hljs/index.js:1
export { default as a11yDark } from './a11y-dark';
^^^^^^
SyntaxError: Unexpected token export
at Module._compile (internal/modules/cjs/loader.js:718:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
at Module.load (internal/modules/cjs/loader.js:641:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:681:19)
at require (internal/modules/cjs/helpers.js:16:16)
at Object.react-syntax-highlighter/dist/esm/styles/hljs (/home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:2563:18)
at __webpack_require__ (/home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:23:31)
at Module../pages/index.js (/home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:2156:104)
at __webpack_require__ (/home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:23:31)
at Object.4 (/home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:2320:18)
at __webpack_require__ (/home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:23:31)
at /home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:91:18
at Object.<anonymous> (/home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:94:10)
at Module._compile (internal/modules/cjs/loader.js:774:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:80
- Comments:27
Top Results From Across the Web
Next.js SyntaxError "Unexpected token 'export'" - Stack Overflow
I was getting the same error: Unexpected token 'export' ; with a node_modules dependency made for the browser (client-side).
Read more >SyntaxError: Unexpected token 'export' #31518 - vercel/next.js
Nextjs is failing on build of a third party package. The third party package is using ES6 syntax for exports. export { Root...
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 >AmCharts in NextJS - SyntaxError: Unexpected token 'export'
Coding example for the question AmCharts in NextJS - SyntaxError: Unexpected token 'export'-Reactjs.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
you’re welcome @zemuldo!
Actually I found a better solution. You just need to use
cjs
module.react-syntax-highlighter/dist/cjs/...
instead of:react-syntax-highlighter/dist/esm/...
And you won’t have to use
next-transpile-modules
.you can fix the issue using
next-transpile-modules
but it doesn’t seem to do tree-shaking.Something like this in
next.config.js
: