简体   繁体   English

意外的令牌导入-使用React和Node

[英]Unexpected token import - using react and node

I'm getting an error when running my react app: Uncaught SyntaxError: Unexpected token import 运行我的React应用程序时出现错误: Uncaught SyntaxError: Unexpected token import

I know that there are a plethora of similar issues on here, but I think mine is a little different . 我知道这里有很多类似的问题,但是我认为我有点不同 First of all, here is the repository, since I'm not sure where exactly the error is: repo 首先,这是存储库,因为我不确定错误在哪里: repo

I'm using create-react-app, and in a seperate backend directory I'm using babel (with a .babelrc file containing the preset es2015). 我使用的是create-react-app,在一个单独的后端目录中,我使用的是babel(带有一个包含预设es2015的.babelrc文件)。 The app worked fine until I added another file in a new directory in the backend folder ( /backend/shared/validations/signup.js ). 该应用程序运行良好,直到我在后端文件夹( /backend/shared/validations/signup.js )的新目录中添加了另一个文件。
I was using es6 before that too, and it was working perfectly fine. 在此之前,我也使用过es6,并且运行良好。 First I thought it was some problem with windows, but I cloned the repo on my Ubuntu laptop and I'm getting the same error there. 首先,我认为这是Windows的问题,但是我在Ubuntu笔记本电脑上克隆了存储库,并且在此遇到了同样的错误。

Some things I tried already: 我已经尝试过的一些方法:

  • Move the entire folder from /backend to the root folder 将整个文件夹从/ backend移动到根文件夹
  • Move just the file (signup.js) just about everywhere 仅将文件(signup.js)移动到几乎任何地方

So no matter where the file is, the error stays the same. 因此,无论文件在哪里,错误都保持不变。 If I remove the entire file and all references to it the app works again. 如果我删除了整个文件及其所有引用,则该应用程序将再次运行。

I think this error is pretty weird, considering I'm using es6 everywhere else in the app without trouble. 考虑到我在应用程序中的其他所有地方都使用es6却没有遇到麻烦,因此我认为此错误很奇怪。 It would be great if anyone could help me with this error. 如果有人可以帮助我解决此错误,那就太好了。

edit: If you want to test this on your own machine just clone the repo and run npm start in the root folder (and also npm start in the backend folder, but that isn't required for the app to run, or for the error to show up). 编辑:如果要在自己的计算机上进行测试,则只需克隆存储库并在根文件夹中运行npm start (以及在后端文件夹中npm start ,但这对于应用程序的运行或错误不是必需的)露面)。

That's what's happening: 这就是发生的事情:

Your webpack is set to run babel loader only in the src folder ( include directive). 您的webpack设置为仅在src文件夹( include指令)中运行babel loader。

To change that, one approach is: 要改变这一点,一种方法是:

1) extract webpack configuration with npm run eject (don't know if there is another way to override settings in react-create-app ). 1)使用npm run eject exit提取webpack配置(不知道是否有另一种方法可以覆盖react-create-app )。 From the docs: 从文档:

Running npm run eject copies all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. 运行npm run exit将所有配置文件和传递依赖项(Webpack,Babel,ESLint等)直接复制到您的项目中,因此您可以完全控制它们。

2) in config/paths.js , add an appShared key, like that: 2)config/paths.js ,添加一个appShared密钥,如下所示:

module.exports = {
  /* ... */
  appSrc: resolveApp('src'),
  appShared: resolveApp('backend/shared'),
  /* ... */
};

3) in config/webpack.config.dev.js , add the path to the babel loader, like that: 3)config/webpack.config.dev.js ,将路径添加到babel加载器,如下所示:

{
  test: /\.(js|jsx)$/,
  include: [ paths.appSrc, paths.appShared ],
  loader: 'babel',
  /* ... */
},

It works now! 现在可以使用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM