【发布时间】:2019-02-15 06:13:21
【问题描述】:
我正在使用 create-react-app 开发应用程序,并使用未编译的第三方模块,我将 JSX 文件从这里包含到我的项目中。
启动或构建时出现以下错误
******.jsx
Module parse failed: Unexpected token (12:25)
You may need an appropriate loader to handle this file type.
我的反应应用程序没有弹出,也不想弹出。
不想从 react-script 中弹出
******.jsx
Module parse failed: Unexpected token (12:25)
You may need an appropriate loader to handle this file type.
示例代码 库中的 Link.jsx
import React from 'react';
import { string } from 'prop-types';
import './Link.scss';
const STATUS = {
HOVERED: 'hovered',
NORMAL: 'normal',
};
class Link extends React.Component {
constructor(props) {
super(props);
this.onMouseEnter = this.onMouseEnter.bind(this);
this.onMouseLeave = this.onMouseLeave.bind(this);
this.state = {
className: STATUS.NORMAL,
};
}
onMouseEnter() {
this.setState({ className: STATUS.HOVERED });
}
onMouseLeave() {
this.setState({ className: STATUS.NORMAL });
}
render() {
const { className } = this.state;
const { page, children } = this.props;
return (
<a
className={className}
href={page}
onMouseEnter={this.onMouseEnter}
onMouseLeave={this.onMouseLeave}
>
{children}
</a>
);
}
}
Link.propTypes = {
page: string,
children: string,
};
Link.defaultProps = {
page: '#',
children: '',
};
export default Link;
以上代码发布到内部 npm repo 并在应用程序中使用
应用程序中的 App.jsx
import { Link} from '@myScope/myreactlib/Link'; // loaded from node_modules
App.jsx 报错
【问题讨论】:
-
能否提供更多关于您正在使用的模块以及如何使用它的信息?
-
@GarrettMotzner,我们使用的模块不是公开可用的,它的内部模块没有编译和发布,它已经像 JSX 一样发布,可以引用 JSX。虽然我们对此无法控制,但可以将其视为另一个内部库。
-
如果您使用的是开箱即用的 create-react-app,babel 加载器应该可以毫无问题地处理您的 jsx 文件。您的 jsx 文件是否有可能在第 12 行出现某种语法错误?如果您可以发布该 jsx 文件的一部分(例如,前 20 行),这可能有助于您获得指向正确方向的响应。
-
我试图加载的 @AlvinLee jsx 文件来自 node_modules。这给了我错误。
-
@MHussain 我明白了。你能发布 which node_module (with version) 并归档这是吗?这将有助于我们帮助您解决问题。
标签: reactjs react-scripts