Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: Unexpected token export when using NextJS #230

Closed
Vista1nik opened this issue Dec 13, 2019 · 32 comments
Closed

SyntaxError: Unexpected token export when using NextJS #230

Vista1nik opened this issue Dec 13, 2019 · 32 comments

Comments

@Vista1nik
Copy link

Describe the bug
Error appears when I trying to import any react-syntax-highlighter styles in NextJS app.

To Reproduce
Steps to reproduce the behavior:

  1. Create NextJS app using 'create-next-app'
  2. 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)
@iwmitt
Copy link

iwmitt commented Dec 14, 2019

Getting the same issue

@malithmcr
Copy link

I'm having the same issue.

@conorhastings
Copy link
Collaborator

@malithmcr @scottsgcho @Vista1nik do any of you have a repo with the error

@sidujjain
Copy link

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:

const withTM = require("next-transpile-modules");
const withPlugins = require("next-compose-plugins");

module.exports = withPlugins(
  [
    [
      withTM,
      {
        transpileModules: [
          "react-syntax-highlighter",
        ]
      }
    ],
    ...

@zemuldo
Copy link

zemuldo commented Dec 21, 2019

@sidujjain Following your advice and https://www.npmjs.com/package/next-transpile-modules docs helped. Thanks!

@sidujjain
Copy link

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.

AbhyudayaSharma referenced this issue in AbhyudayaSharma/abhyudayasharma.github.io Apr 14, 2020
@SalahAdDin
Copy link

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.

It didn't work for me.

@preshonyee
Copy link

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.

this works for me...after loads of trial and error

@matiasherranz
Copy link

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.

This worked for me! Thanks for sharing!

@TheSecMaven
Copy link

huge save, worked for me #opensource for the win

@f3ve
Copy link

f3ve commented Dec 20, 2020

@SalahAdDin Thanks for the simple fix!

@faustinozanetto
Copy link

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.

Man you deserve something, thanks a lot

@mikhaelesa
Copy link

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.

Not all heroes wear capes

@MuhammadMinhaj
Copy link

MuhammadMinhaj commented Mar 31, 2021

Try to access from node_modules. It's worked on NextJs project!
for example : /node_modules/react-syntax-highlighter/dist/esm/styles/prism';

@DumasOlivier
Copy link

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.

Thank you so much @sidujjain it worked ! 😍

@padunk
Copy link

padunk commented Aug 19, 2021

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.

Thanks for the solution.
Can we update and add this on the docs please? Thanks.

@kimbaudi
Copy link

kimbaudi commented Sep 2, 2021

@padunk - I've submitted a PR to update the README.

@cazterk
Copy link

cazterk commented Nov 27, 2021

awesome awesome awesome 👍🏼👍🏼👌🏼🤜🏼🤛🏼

@ooys
Copy link

ooys commented Feb 4, 2022

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.

Works for me! Thank you for this solution!

@yourcasualdev
Copy link

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.

Thanks It worked for me.

@SujalShah3234
Copy link

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.

This works but at cost of bundle size 💀

@masoudjapalaghi
Copy link

masoudjapalaghi commented May 22, 2022

sidujjain

Hi friends, I had the same problem and our friend @sidujjain solved the problem, but the whole package size will be added to your component. To solve this problem, use this:
import dark from "react-syntax-highlighter / dist / cjs / styles / prism / dark";
Instead of using this:
import {dark} from "react-syntax-highlighter / dist / cjs / styles / prism ";

@AidanLDev
Copy link

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.

Worked a charm, thank you

@kingRayhan
Copy link

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.

This worked for me! Thanks for sharing!

You saved my life

@xylvnking
Copy link

sidujjain

Hi friends, I had the same problem and our friend @sidujjain solved the problem, but the whole package size will be added to your component. To solve this problem, use this: import dark from "react-syntax-highlighter / dist / cjs / styles / prism / dark"; Instead of using this: import {dark} from "react-syntax-highlighter / dist / cjs / styles / prism ";

This fixed it for me!

@Schachte
Copy link

Schachte commented Oct 9, 2022

Full working solution

import React from "react";

import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import d from "react-syntax-highlighter/dist/cjs/styles/prism/nord";

const CodeBlock = {
  code({ node, inline, className, children, ...props }) {
    const match = /language-(\w+)/.exec(className || "");
    return !inline && match ? (
      <SyntaxHighlighter
        style={d}
        language={'javascript'}
        PreTag="div"
        showLineNumbers={true}
        customStyle={{ margin: 0, padding: ".8rem" }} 
        {...props}
      >
        {String(children).replace(/\n$/, "")}
      </SyntaxHighlighter>
    ) : (
      <code className={className} {...props}>
        {children}
      </code>
    );
  },
};

export default CodeBlock;

@thesatishjassal
Copy link

thesatishjassal commented Dec 17, 2022

Try to access from node_modules. It's worked on NextJs project! for example : /node_modules/react-syntax-highlighter/dist/esm/styles/prism';

path fixing true thnx.

@zesertebe
Copy link

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 are the best. You saved my life. Whenever you want I'll buy you a beer or whatever you want.

@dnldmp
Copy link

dnldmp commented Jan 22, 2023

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.

Thank you it help me a lot :D

@at-the-vr
Copy link

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.

thanks a lot, helped me a lot for this OS project

@AgustinLaValla
Copy link

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.

It didn't work for me.

Nice solution!

@masonictemple4
Copy link

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.

A couple years late, and this still worked! Thanks for sharing 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests