【问题标题】:NextJS - Unexpected Token ImportNextJS - 意外的令牌导入
【发布时间】:2020-03-16 21:52:32
【问题描述】:

在将 react-syntax-highlighter 集成到我的 next-js 项目中时,我使用了以下代码:

import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { okaidia } from "react-syntax-highlighter/dist/esm/styles/prism";

...

<SyntaxHighlighter language="jsx" style={okaidia}>
{some code goes here}
</SyntaxHighlighter>

...

我在运行 npm run dev 时收到以下错误,但前提是我直接运行该页面。

Unexpected token export
/Users/johndetlefs/repos/tal-gel-framework/node_modules/react-syntax-highlighter/dist/esm/styles/prism/index.js:1
(function (exports, require, module, __filename, __dirname) { export { default as coy } from './coy';
                                                              ^^^^^^

SyntaxError: Unexpected token export
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:617:28)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.react-syntax-highlighter/dist/esm/styles/prism (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:7242:18)
    at __webpack_require__ (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:23:31)
    at Module../pages/components.js (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:6839:104)
    at __webpack_require__ (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:23:31)
    at Object.3 (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:7175:18)
    at __webpack_require__ (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:23:31)
    at module.exports../js/config/libs/_theme-foundation--colors.js.config.themes.list.name (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:91:18)

如果我通过另一个页面导航到该页面,那么一切正常。如果我然后刷新页面,我会收到错误。

删除线

import { okaidia } from "react-syntax-highlighter/dist/esm/styles/prism";

从组件中删除 style 属性可以修复所有问题,但使用默认的 prism 样式,这不是预期的结果。

环顾四周,我可以看到人们有类似的问题,并且修复可能与 next.js.config 文件有关,以及 css 文件是如何在服务器端加载的,但我不是 100% 在那里做什么。

假设 next.js.config 文件是解决方案的一部分,这里是当前内容。

const withSass = require("@zeit/next-sass");
const withCSS = require("@zeit/next-css");

module.exports = withCSS(
  withSass({
    webpack(config) {
      config.module.rules.push({
        test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
        use: {
          loader: "url-loader",
          options: {
            limit: 100000
          }
        }
      });
      config.module.rules.push({
        test: /\.svg$/,
        use: ["@svgr/webpack"]
      });
      return config;
    }
  })
);

我已经尝试过使用和不使用 withCSS 并且问题保持不变。

任何帮助将不胜感激! ????

【问题讨论】:

    标签: node.js webpack next.js


    【解决方案1】:

    翻了一圈后,我查看了 npm packages 目录,发现有两种类型的 dists:cjs &amp; esm。简单的解决方法是使用cjs dist 而不是esm dist

    import { darcula } from 'react-syntax-highlighter/dist/cjs/styles/prism';

    希望这会有所帮助:)

    【讨论】:

    • 对不起@Rotinx,我不是要投反对票,但它说我的投反对票在帖子被编辑之前被锁定,所以我将删除投反对票并在编辑后立即投赞成票.
    • 哈哈没问题感谢您的诚实,我已批准您的编辑。
    • 谢谢!完美运行:D
    • 这为我修好了!
    猜你喜欢
    • 1970-01-01
    • 2016-06-18
    • 2018-03-14
    • 2019-03-21
    • 2020-05-07
    • 1970-01-01
    • 2017-05-20
    • 2019-08-07
    • 2018-09-24
    相关资源
    最近更新 更多