javascript - React - 模块解析失败 : Unexpected token

标签 javascript reactjs object npm package

我决定安装 npm 包 react-radio-buttons安装并应用我的组件后出现错误:

./node_modules/react-radio-buttons/index.jsx 80:6
module parse failed: Unexpected token (80:6)
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
|     const style = horizontal ? { display: 'inline-flex', width: '100%' } : {};
|     return (
>       <div style={style} { ...props}>
|         {
|           children.map((c, i) => this.renderChild(c, i, i === checkedIndex)))

我想这个问题可能与 package.json 有关,但我不知道如何解决这个问题。在我提供包文件之前,我想警告 dependencies 对象中的很多属性,我会把所有东西都剪掉,只留下 "react-radio-buttons": "^ 1.2. 2", 实际发生错误的包。

package.json

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react-radio-buttons": "^1.2.2",
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

最佳答案

在我的例子中,错误是由不正确的使用引起的 => import { RadioGroup } from "react-radio-buttons/index"; (/index 应该被删除)

解决方案是将其更改为:

import { RadioGroup } from "react-radio-buttons";

关于javascript - React - 模块解析失败 : Unexpected token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65915509/

相关文章:

javascript - 如何在 JS FETCH API 之外访问返回值

css - 我如何在 React 中将组件堆叠在一起?

javascript - 如何调用作为对象成员的函数?

javascript - 使用 JavaScript 和 lodash 汇总对象数组数据的最佳方法

javascript - 有没有办法检测何时要从 DOM 中删除 DOM 节点?

javascript - 我的jQuery函数只能第一次运行

node.js - 拒绝(TypeError): Cannot read property 'setState' of undefined,未捕获( promise )TypeError:无法读取未定义的属性 'setState'

javascript - 尝试使用资源响应作为所选项目时 ngOptions 的奇怪行为

javascript - JavaScript 中的 self() 属性

javascript - Highcharts:与 setData 同步动态更新 dataLabels