node.js - 即使加载了 babel 库,我的 Node 服务器也无法识别 es6 语法

标签 node.js reactjs

我在服务器上渲染我的react/redux应用程序时遇到很多麻烦。首先,我需要所有必要的 babel 库,以便 Node 服务器在读取 jsx 文件时可以识别语法,但我仍然遇到语法错误。我得到的错误是 > 8 |返回 { ...状态,检查:true };意外的 token 指向...

服务器.js

require('babel-register')({ presets: ['es2015', 'react'] });
require('import-export');
require('babel-polyfill');

const reducers = require('../src/reducers').default; //written with es6 import statements and export default

reducer

export default function(state = INITIAL, action) {
  switch(action.type) {
    case "CHECK":
      return { ...state, check: true }; //error reading this line
    default:
      return state;
  }
}

最佳答案

{...obj} 语法是 babel 下名为 object-rest-spread 的 ES7 功能,因此 2015/react 预设不包含它。你必须将它添加到你的 babel 配置中,例如 "plugins": ["transform-object-rest-spread"]

更多信息on the babel site

关于node.js - 即使加载了 babel 库,我的 Node 服务器也无法识别 es6 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42334042/

相关文章:

javascript - 使用 Node js 在 TypeScript 中获取操作系统环境

mysql - 使用nodejs将excel表上传到MySQL时如何忽略第一行?

javascript - 如何将类切换到 reactjs 中的 div 元素?

javascript - React 0.13 中的模态

javascript - --是否可以检测到用React点击的元素的ID?

node.js - 为什么 npm 使用这个 package.json 安装这么多模块?

node.js - 使用 NodeJS 从 MongoDB 中的集合中获取数据

javascript - 每次状态更新时都会调用 shuffle 函数?

javascript - es6 在 react 中对多个状态使用 ${}

node.js - 使用 mocha/superagent 测试本地 https 服务器