当"typeorm/browser/index.js"抛出"Module parse failed: 'import' and 'export' may appear only with '



我最近在Node.JS上开发了一个新的纯JS web应用程序(不导入任何关于TypeScript的内容(Next.js@12.0.10作为框架和TypeORM@0.2.41作为Azure SQL服务器的ORM层。

一切正常,我已成功连接到SQL服务器。

但是,我不小心在没有提交package.json的情况下删除了它。无论如何,我设法重新安装了丢失的包typeormreflect-metadatamssql。但在重新安装后,当我启动开发服务器时,我遇到了一个以前从未见过的新错误。

error - ./node_modules/typeorm/browser/index.js
Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (3:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| /*!
|  */
> import "reflect-metadata";
| // -------------------------------------------------------------------------
| // Commonly Used exports

我搜索了一下,发现它可能与webpack有关。但我对此没有太多线索。下面是我的next.config.jsbtw.

module.exports = {
reactStrictMode: true,
webpack: (config, options) => {
config.module.rules.push({
test: /.html$/i,
loader: "html-loader",
});
return config;
},
};

我试过一些基本的健全性检查,比如删除node_modules/并重新安装,但没有成功。有人知道发生了什么以及如何解决吗?


更新:

在@hej和GitHub上这个问题的帮助下,我把这个部分添加到了我的next.config.js中,它很有效!

config.module.rules.push({
test: /.[jt]sx?$/,
include: [/node_modules(.*[/\])+typeorm/],
type: "javascript/auto",
use: "babel-loader",
});

尝试将其添加到您的webpack配置中:

{
module: {
rules: [
{
test: /.[jt]sx?$/,
include: [/node_modules(.*[/\])+typeorm/],
use: "babel-loader",
},
],
},
}

最新更新