【发布时间】:2022-06-28 19:43:19
【问题描述】:
在我的 React 应用程序中,我想使用导入断言:
import data from "./json/clients-m.json" assert { type: "json" }
但是,我收到以下错误:
./src/Clients.js 中的错误 模块构建失败(来自 ./node_modules/babel-loader/lib/index.js): SyntaxError: E:\src\Clients.js: 当前未启用对实验性语法“importAssertions”的支持。
将 @babel/plugin-syntax-import-assertions (https://github.com/babel/babel/tree/main/packages/babel-plugin-syntax-import-assertions) 添加到 Babel 配置的“插件”部分以启用解析。
第 1:41 行:解析错误:此实验性语法需要启用解析器插件:“importAssertions”。 (1:41)
我已经安装了这个插件:
npm install @babel/plugin-syntax-import-assertions --save-dev
然后我创建了.babelrc.json:
{
"plugins": [
"@babel/plugin-syntax-import-assertions"
]
}
并且还将这个插件添加到package.json:
{
"name": "clients-frontend",
"version": "0.1.0",
"private": true,
"babel": {
"plugins": [
"@babel/plugin-syntax-import-assertions"
]
},
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.1.1",
"@testing-library/user-event": "^13.5.0",
"bootstrap": "^5.1.3",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"devDependencies": {
"@babel/plugin-syntax-import-assertions": "^7.16.7"
}
}
但是,我不断收到此错误。 ????
【问题讨论】:
-
你能不能用.babelrc.json代替.babelrc作为文件名