Error in ./node_modules/react-hook-form/dist/index.esm.mjs模块解析失败:意外的标记(126:22)您可能需要适当的加载程序来处理此文件类型。从react-hook-form导入{ useForm,SubmitHandler };我在尝试从React Hook Form包中使用useForm时得到了这个错误,请帮我解决
8ljdwjyq1#
使用较低版本的.cjs文件导出到dist/node_modules下,否则,也可以更新您的package.json以包括:
"type" : "module",
这将使.mjs文件能够工作(ESModules)
dgiusagp2#
如果你的react-scripts版本不是5,你应该在react的webpack中添加.mjs配置细节。因此,安装 react-app-rewired,然后创建 * react-overrides.js* 文件并添加以下内容
module.exports = function override(webpackConfig) { webpackConfig.module.rules.push({ test: /\.mjs$/, include: /node_modules/, type: "javascript/auto" }); return webpackConfig;}
module.exports = function override(webpackConfig) {
webpackConfig.module.rules.push({
test: /\.mjs$/,
include: /node_modules/,
type: "javascript/auto"
});
return webpackConfig;
}
package.json中的react脚本应该更新为
"scripts": { "start": "react-app-rewired start", "build": "react-app-rewired build", "test": "react-app-rewired test", "eject": "react-scripts eject"},
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
},
另一个解决方案是将react-script升级到5.0.1版本
2条答案
按热度按时间8ljdwjyq1#
使用较低版本的.cjs文件导出到dist/node_modules下,否则,也可以更新您的package.json以包括:
这将使.mjs文件能够工作(ESModules)
dgiusagp2#
如果你的react-scripts版本不是5,你应该在react的webpack中添加.mjs配置细节。因此,安装 react-app-rewired,然后创建 * react-overrides.js* 文件并添加以下内容
package.json中的react脚本应该更新为
另一个解决方案是将react-script升级到5.0.1版本