我正在用Nextjs构建我的网站,并且导入Bablyonjs会引发以下错误。
syntaxError: Unexpected token 'export'
module.exports = require("@babylonjs/core")
我将标准的nextjs设置与tsconfig.json一起使用,是指此Babylon文档并逐字使用示例。
经过不那么微不足道的时间搜索之后,我终于了解了以下内容。
@babylon(es6)不会编译为javascript,而是可以很好地定义(es6)友好的打字稿源代码库。(在想摇树时有帮助)
开箱即用的nextjs未配置为在node_modules中编译任何内容。它希望可以使用预编译的javascript。
第2点是为什么我收到错误消息,nextjs期待已编译的js,并且未得到编译。
要解决此问题,您需要添加一个next.config.js
,并使用next-transpile-modules
和对其进行配置next-compose-plugins
。
yarn add next-transpile-modules
yarn add next-compose-plugins
next.config.js
const withTM = require('next-transpile-modules')(['@babylonjs']);
const withPlugins = require('next-compose-plugins');
const nextConfig = {
target: 'serverless',
webpack: function (config) {
/// below is not required for the problem described. Just for reference.(es6)
config.module.rules.push({test: /\.yml$/, use: 'raw-loader'})
return config
}
}
module.exports = withPlugins([withTM], nextConfig);
此后它编译没有错误。
参考方便的链接,我遇到了解决此问题的方法。
有助于某些人理解问题的链接。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句