[英]Unexpected token “<” when importing from third party library into React project
I am fairly new to React and have no experience with webpack and am struggling with the following issue:我对 React 相当陌生,对 webpack 没有经验,并且正在努力解决以下问题:
I created a new React application with npx create-react-app
(which to my understanding uses webpack under the hood) and installed a third-party library with with npm install
.我使用 npx npx create-react-app
创建了一个新的 React 应用程序(据我了解,它在后台使用 webpack)并使用npm install
安装了一个第三方库。 So far so good, I am able to use the library and everything runs as expected.到目前为止一切顺利,我能够使用该库并且一切都按预期运行。
Unfortunately, the library has some limitations and I would like to tweak one of its functions to suit my needs.不幸的是,这个库有一些限制,我想调整它的一个功能以满足我的需要。 The original function looks something like this:原来的 function 看起来像这样:
project_root/node_modules/third-party-library/modules/export/tweakableFunction.js
: project_root/node_modules/third-party-library/modules/export/tweakableFunction.js
:
import {someFunction} from "../utils/configUtils";
export const tweakableFunction = (a, b) => {
return someFunction(a, b);
}
project_root/node_modules/third-party-library/modules/export/index.js
: project_root/node_modules/third-party-library/modules/export/index.js
:
"use strict";
export {tweakableFunction } from "./tweakableFunction";
In my project, I would like to replicate tweakableFunction
and add some functionality to it:在我的项目中,我想复制tweakableFunction
并为其添加一些功能:
project_root/src/tweakedFunction/tweakedFunction.js
: project_root/src/tweakedFunction/tweakedFunction.js
:
import {someFunction} from "third-party-library/modules/utils/configUtils";
export const tweakedFunction = (a, b) => {
//... do some stuff the original library can't do ...
return someFunction(a, b);
}
project_root/src/tweakedFunction/index.js
: project_root/src/tweakedFunction/index.js
:
"use strict";
export {tweakedFunction } from "./tweakedFunction";
Unfortunately, when I run my project I get the following error in project_root/node_modules/third-party-library/modules/some_dir/somefile.js
:不幸的是,当我运行我的项目时,我在project_root/node_modules/third-party-library/modules/some_dir/somefile.js
中收到以下错误:
SyntaxError: [...] Unexpected token
...
> 21 | someFunction: (props) => <SomeComponent {...props} />,
| ^
...
Do I have to create a webpack / Babel config in my root project for this to work?我是否必须在我的根项目中创建一个 webpack / Babel 配置才能正常工作? Is what I am trying to achieve even feasible?我想要实现的目标是否可行?
First of all, modifying anything inside node_modules
folder is not a good idea, since npm update
will overwrite anything you change in there.首先,修改node_modules
文件夹中的任何内容都不是一个好主意,因为npm update
将覆盖您在那里更改的任何内容。 Also, those changes will be available only in your local instance of a project, you wouldn't be able to use them anywhere else.此外,这些更改仅在您的本地项目实例中可用,您将无法在其他任何地方使用它们。 Consider making a public commit if other people may benefit from your tweak or fork this library into your personal repo and add your changes to said repo.如果其他人可能会从您的调整中受益,或者将该库分叉到您的个人存储库中并将您的更改添加到所述存储库中,请考虑进行公开提交。
And the error you are seeing happened because you should wrap return statement with React component in parenthesis.并且您看到的错误发生是因为您应该在括号中用 React 组件包装 return 语句。
someFunction: (props) => return (<SomeComponent {...props} />);
If after that error is still popping up, you are trying to use JSX syntax inside common JS file如果在该错误仍然弹出之后,您正在尝试在公共 JS 文件中使用 JSX 语法
Files get compiled with babel while you are importing javascript files, but in create-react-app default WebPack config the node_modules
has been excluded, so while you are importing the exact file, an error has been thrown, as the <div>
JSX syntax is not native js syntax.导入 javascript 文件时使用 babel 编译文件,但在 create-react-app 默认 WebPack 配置中, node_modules
已被排除,因此当您导入确切的文件时,会引发错误,因为<div>
JSX 语法不是原生 js 语法。
the turn => <Som
to => return (<Som
is an absolute wrong, while using the former format the component will be returned directly, the return keyword is not required. turn => <Som
to => return (<Som
是绝对错误的,使用前一种格式会直接返回组件,return 关键字不是必须的。
but sure one thing, do not edit code in node_modules
.但可以肯定的是,不要在node_modules
中编辑代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.