React Starter Kit在OSX出现Unexpected token export报错求助
SyntaxError: Unexpected token export on macOS for React Starter Kit Hey fellow dev, let's break down why this error is popping up on your Mac but not Windows. This issue almost always ties to differences in how Node.js handles ES modules or environment inconsistencies between the two systems. Here are the most likely causes:
Mismatched Node.js versions
Older Node.js versions (pre-v13.2) don’t support ES module syntax likeexportby default in.jsfiles without extra configuration. It’s possible your Windows machine runs a newer Node version that handles this natively, while your Mac has an outdated release. Check your Node version on both systems with:node -vIf your Mac’s version is behind, upgrade it to the latest LTS release—this often fixes the problem right away.
Missing ES module configuration in
package.json
The React Starter Kit may rely on Node recognizing.jsfiles as ES modules. For Node versions <v18 (even some newer ones if unconfigured), you need to add"type": "module"to your project’spackage.jsonto tell Node to treat.jsfiles as ES modules instead of CommonJS. If this line is missing or wasn’t properly set up on your Mac, Node will throw that export syntax error.Incomplete or corrupted dependencies
When moving projects between operating systems, dependencies sometimes install incorrectly—especially transpilation tools (like Babel) that convert ES modules to code Node can understand. Try wiping your existing dependencies and reinstalling fresh:rm -rf node_modules package-lock.json npm installThis ensures you get OS-specific, uncorrupted builds of all required packages.
Directly running the script instead of using npm/yarn commands
If you’re executingnode tools/run.jsdirectly on your Mac, you’re probably skipping the transpilation step that the project’s npm scripts handle. The starter kit’spackage.jsonlikely includes a script (likenpm start) that uses tools such asbabel-nodeto convert ES module syntax before running the file. Always use the project’s official npm/yarn commands instead of running Node directly on individual files.
内容的提问来源于stack exchange,提问作者Keslavi

