问题
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