简体   繁体   English

导出默认数据 SyntaxError:在使用 typescript 在 next.js 上构建期间出现意外的令牌导出

[英]export default data SyntaxError: Unexpected token export during bulding on next.js using typescript

Code available here => https://codesandbox.io/s/sweet-mcclintock-dhczx?file=/pages/index.js此处提供代码 => https://codesandbox.io/s/sweet-mcclintock-dhczx?file=/pages/index.js

Initial error when trying to use @iconify-icons/cryptocurrency with next.js and typescript (it happens only when in typescript).尝试将@iconify-icons/ cryptocurrency 与 next.js 和 typescript 一起使用时出现初始错误(仅在打字稿中发生)。

SyntaxError: Unexpected token export
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at eval (webpack-internal:///@iconify-icons/cryptocurrency/usdt:1:18)
    at Object.@iconify-icons/cryptocurrency/usdt (/sandbox/.next/server/pages/index.js:409:1)
    at __webpack_require__ (/sandbox/.next/server/pages/index.js:23:31)
    at eval (webpack-internal:///./pages/index.js:9:92)
    at Module../pages/index.js (/sandbox/.next/server/pages/index.js:398:1)
    at __webpack_require__ (/sandbox/.next/server/pages/index.js:23:31)
    at /sandbox/.next/server/pages/index.js:91:18
    at Object.<anonymous> (/sandbox/.next/server/pages/index.js:94:10)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
/sandbox/node_modules/@iconify-icons/cryptocurrency/usdt.js:6
export default data;

The problem does not happen with all libraries.并非所有库都会出现此问题。 It seems to me that during the transpilation babel has a problem with the library syntax.在我看来,在编译期间,babel 的库语法有问题。

Then I've tried to transpile the lib next-transpile-modules to fix the problem using the code below.然后我尝试使用下面的代码转换 lib next-transpile-modules以解决问题。

const withTM = require("next-transpile-modules")([
  "@iconify-icons/cryptocurrency"
]);

module.exports = withTM();

But a new problem has happened.但是新的问题出现了。

Error: next-transpile-modules - an unexpected error happened when trying to resolve "@iconify-icons/cryptocurrency"
Error: Can't resolve '@iconify-icons/cryptocurrency' in '/sandbox'
    at getPackageRootDirectory (/sandbox/node_modules/next-transpile-modules/src/next-transpile-modules.js:87:11)
    at Array.map (<anonymous>)
    at generateModulesPaths (/sandbox/node_modules/next-transpile-modules/src/next-transpile-modules.js:99:33)

To conclude, transpiling the module is the way I'm trying to achieve the main goal.总而言之,转换模块是我试图实现主要目标的方式。 It might not be necessary.可能没有必要。

The way the @iconify-icons/cryptocurrency library is exported means you need to transpile each icon package you use individually. @iconify-icons/cryptocurrency库的导出方式意味着您需要转译您单独使用的每个图标 package。

const withTM = require("next-transpile-modules")([
  "@iconify-icons/cryptocurrency/usdt"
]);

module.exports = withTM();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM