NextJS-意外的令牌导入

时间:2019-11-21 03:34:39

标签: node.js webpack next.js

在将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";

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

环顾四周,我可以看到人们有类似的问题,并且此修复程序可能与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和不带withCSS的问题,问题仍然存在。

任何帮助将不胜感激! ?

1 个答案:

答案 0 :(得分:7)

闲逛了一会儿之后,我检查了npm软件包目录,发现有两种类型的dists:cjs & esm。简单的解决方法是仅使用cjs dist而不是esm dist

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

image

希望这会有所帮助:)