为什么在nodejs中传播语法抛出错误 [英] Why Spread syntax throwing error in nodejs

查看:102
本文介绍了为什么在nodejs中传播语法抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码创建文档。哪个工作正常。

I am using this code to create a document. Which works as expected.

await Promise.all(
  payload.exercises.map(async(exercise) => {
      exercise.user = user
      exercise.workoutName = payload.workoutName
      await Exercise.create(exercise)
  })
)

但为了优化代码,我想使用带有练习的扩展语法对象

But to optimise the code I want to use spread syntax with the exercise object

await Promise.all(
  payload.exercises.map(async(exercise) => {
      await Exercise.create({ ...exercise, user, workoutName: payload.workoutName })
  })
)

但是当我编译它时,它会抛出错误意外的令牌(58:36)

But when I compile it, It throws error Unexpected token (58:36)

有人可以帮忙,为什么我不能在后端使用它,而只能在前端使用它 reactjs

Can someone please help why I am not able to use this on the back end while can simply use this at the front end reactjs

.babelrc文件

{
  "presets": [
    ["env", {
      "targets": {
        "node": "11.5.0"
      }
    }]
  ],
  "env": {
    "test": {
      "plugins": ["istanbul"]
    }
  }
}

package.json

{
  "main": "index.js",
  "scripts": {
    "start:dev": "nodemon ./src/index.js",
    "start": "npm run build && node ./build/index.js",
    "build": "babel src -d build -s --source-maps --copy-files",
    "seed": "babel-node ./seeders/index.js",
  },
  "dependencies": {
    "@sendgrid/mail": "^6.2.1",
    ...
  },
  "devDependencies": {
    "async": "2.3.0",
    "babel-cli": "6.24.0",
    "babel-core": "6.24.0",
    "babel-eslint": "7.2.1",
    "babel-plugin-istanbul": "4.1.1",
    "babel-polyfill": "6.23.0",
    "babel-preset-env": "^1.2.2",
    "babel-preset-node6": "11.0.0",
    "chalk": "1.1.3",
    "chance": "1.0.6",
    "eslint": "3.19.0",
    "eslint-config-walmart": "1.2.2",
    "eslint-plugin-filenames": "1.1.0",
    "eslint-plugin-import": "2.2.0",
    "lab-babel": "1.1.1",
  }
}


推荐答案

我会坦率地说:我不太确定你在Babel v6中的Babel配置有什么问题。可能是这个虽然我认为所有命名更改都处理了这个问题,但是与从v6到v7的转换相关的npm模块存在一些问题。我不再使用Babel v6了。

I'll be frank: I'm not quite sure what the problem is with your Babel configuration in Babel v6. It may be that there's some problem with the npm modules related to the transition from v6 to v7 though I thought all the naming changes handled that. I don't use Babel v6 anymore.

在复制你的问题后,我已经能够用Babel v7解决它。方法如下:

After replicating your problem, I've been able to solve it with Babel v7. Here's how:


  1. 我从 devDependencies 中删除​​了所有babel条目 package.json 所以它看起来像这样:

  1. I removed all the babel entries from devDependencies in package.json so it looks like this:

"devDependencies": {
  "async": "2.3.0",
  "chalk": "1.1.3",
  "chance": "1.0.6",
  "eslint": "3.19.0",
  "eslint-config-walmart": "1.2.2",
  "eslint-plugin-filenames": "1.1.0",
  "eslint-plugin-import": "2.2.0",
  "lab-babel": "1.1.1"
}


  • 完全删除 node_modules

    重新创建 node_modules

    npm install


  • 已安装的Babel v7 (遗憾的是,声称适用于v7.1.0的当前文档的CLI安装行不正确,我提交了一个问题

    npm install --save-dev @babel/core @babel/cli @babel/preset-env

    ...这给了我这些 devDependencies

    ...which gives me these devDependencies:

    "devDependencies": {
      "@babel/cli": "^7.2.3",
      "@babel/core": "^7.2.2",
      "@babel/preset-env": "^7.2.3",
      "async": "2.3.0",
      "chalk": "1.1.3",
      "chance": "1.0.6",
      "eslint": "3.19.0",
      "eslint-config-walmart": "1.2.2",
      "eslint-plugin-filenames": "1.1.0",
      "eslint-plugin-import": "2.2.0",
      "lab-babel": "1.1.1"
    }
    


  • 确保 .babelrc 已设置使用新的 @ babel / env

    {
      "presets": [
        ["@babel/env", {
            targets: {
                "node": "11.5.0"
            }
        }]
      ],
      "env": {
        "test": {
          "plugins": ["istanbul"]
        }
      }
    }
    


  • 然后, npm run build 按预期工作。传播语法是独立的(因为Node v11.5.0本身支持它)。

    Then, npm run build worked as expected. The spread syntax was left alone (since Node v11.5.0 supports it natively).

    为了确定,因为您的问题的早期版本使用节点:4.3 4.3 应该在引号中,顺便说一句),我切换到那个,它工作正常,转发传播语法(和 async / await 以及很多其他东西,因为Node.js v4.3真的没有了日期)。

    Just to be sure, since an earlier version of your question used "node": 4.3 (the 4.3 should be in quotes, btw), I switched to that, and it worked just fine, transpiling the spread syntax (and async/await and lots of other stuff, as Node.js v4.3 is really out of date).

    这篇关于为什么在nodejs中传播语法抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆