npm start fails with unexpected token in file from local dependency

三世轮回 提交于 2021-02-11 08:58:23

问题


I made first-app with create-react-app where a component makes use of some material-ui components, and it is working fine when started with npm start.

From this application I extracted that nice-component component in a new nice-component folder, with its package.json, the component is in a .js file that export default the component class - in Visual Studio Code I can succesfully navigate to its sources, so I would guess the setup / folder configuration is correct.

In final-app application, a new application still made with create-react-app I added nice-component as a local dependency (file:../nice-component), but when I run npm start I get:

SyntaxError: C:\...\other-package.js: Unexpected token (83:8)

  81 |       const { classes } = this.props;
  82 |       return (
> 83 |         <Grid container className={classes.root} style={{margin:30}}>
     |         ^

I can't figure out why that happens, it seems that local dependencies do not bring in their own (transitive) dependencies, but I successfully used a similar approach months ago in another project - where though I didnt use react-scripts, but set up babel, webpack, etc by myself.

I even tried adding all of material-ui dependencies in final-app (although I don't think I shouldn't), but yet it doesn't work.


回答1:


The issue Unexpected token '<' is because of missing the babel preset.

you can simply install the babel dependency using node as follows

npm install babel-preset-react

Thank you



来源:https://stackoverflow.com/questions/60454797/npm-start-fails-with-unexpected-token-in-file-from-local-dependency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!