简体   繁体   English

为什么简写作业在我的 babel 设置中不起作用?

[英]Why does short-hand assignment not work in my babel setup?

Background背景

I am currently in the process of migrating the backend of my project to ES6 via babel and am running into an error that seems to be related to a short-hand assignment that is not working anymore?我目前正在通过 babel 将我的项目后端迁移到 ES6,并且遇到了一个似乎与不再起作用的速记分配有关的错误? I am not familiar with babel.我对通天塔不熟悉。 Might be that I am not seeing something super obvious here.可能是我在这里没有看到非常明显的东西。

Problem问题

now when trying to run my app in dev via yarn start I am seeing the following error (simplified example) this did work prior to setting up babel (I am on node.js 13.13.0 ).现在,当尝试通过yarn start在 dev 中运行我的应用程序时,我看到以下错误(简化示例),这在设置 babel 之前确实有效(我在node.js 13.13.0上)。 Any idea what's going on here?知道这里发生了什么吗?

Codesnippet:代码片段:

const a = {
  hi:1, bye:2, ciao: 3
}
const b  = {hi, bye} = a
console.log(b)

Errormessage:错误信息:

[nodemon] restarting due to changes...
[nodemon] starting `NODE_ENV=development babel-node server.js server.js`
/Users/user/Projects/fb/fb-flow-app/server/config/config.js:21
var b = (_a = a, hi = _a.hi, bye = _a.bye, _a);
                    ^

ReferenceError: hi is not defined
    at Object.<anonymous> (/Users/user/Projects/fb/fb-flow-app/server/config/config.js:12:8)
    at Module._compile (internal/modules/cjs/loader.js:1123:30)
    at Module._compile (/Users/user/Projects/fb/fb-flow-app/server/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:1143:10)
    at Object.newLoader [as .js] (/Users/user/Projects/fb/fb-flow-app/server/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:972:32)
    at Function.Module._load (internal/modules/cjs/loader.js:872:14)
    at Module.require (internal/modules/cjs/loader.js:1012:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/Users/user/Projects/fb/fb-flow-app/server/server.js:1:14)
[nodemon] app crashed - waiting for file changes before starting...

High-level project structure:高层项目结构:

root
|-frontend
|-server

The frontend is a next.js project and has it's own .babelrc which seems to be working.前端是一个next.js项目,它有自己的.babelrc似乎正在工作。

Steps taken so far迄今为止采取的步骤

I've installed a couple of babel-dev-dependencies (in my server folder):我已经安装了几个 babel-dev-dependencies(在我的服务器文件夹中):

"devDependencies": {
    "@babel/cli": "^7.10.1",
    "@babel/core": "^7.10.2",
    "@babel/node": "^7.10.1",
    "@babel/preset-env": "^7.10.2",
    ...
}

and also tried (with no result)并且也尝试过(没有结果)

@babel/plugin-transform-shorthand-properties
@babel/plugin-transform-spread

I've configured my nodemon.json :我已经配置了我的nodemon.json

{
    "watch": ["../server"],
    "exec": "NODE_ENV=development babel-node server.js",
    "ext": "js"
}

I've set up a .babelrc我已经建立了一个.babelrc

{
    "presets": ["@babel/preset-env"]
}

And then I am running via:然后我通过以下方式运行:

"scripts": {
    "start": "concurrently --prefix none \"cd server && NODE_ENV=development yarn nodemon server.js\" \"cd frontend && yarn dev\""
},

When you do the following:当您执行以下操作时:

const a = {
  hi:1, bye:2, ciao: 3
}
const b  = {hi, bye} = a

Babel sees an assignment to two variables, named hi and bye that don't yet exist. Babel 看到对两个变量的赋值,名为hibye尚不存在。 It seems that Babel is operating in strict mode where this is an error. Babel 似乎在严格模式下运行,这是一个错误。 You'll get the same in Node while in strict mode.在严格模式下,您将在 Node 中获得相同的结果。 Solve this by defining them explicitly:通过明确定义它们来解决这个问题:

const a = {
  hi:1, bye:2, ciao: 3
}
let hi, bye;
const b  = {hi, bye} = a

Destructuring does not make an object, despite it looking like an object initialization.解构不会产生 object,尽管它看起来像 object 初始化。

Note how, because of that, the value of b is not an object that looks like {hi:1, bye:2} but a reference to a , just like what would happen if you just do const b = a :请注意,因此, b的值不是看起来像{hi:1, bye:2}的 object 而是对a的引用,就像您只执行const b = a会发生什么一样:

const b = a;
a.hi = 5;
console.log(b.hi); // 5

Other things you can do:你可以做的其他事情:

  • const {hi, bye} = a; to get two const variables named hi and bye (rather than var or let )获取两个名为hibye的 const 变量(而不是varlet
  • const {hi: b, bye: c} = a; to get two variables named b and c ("renaming")得到两个名为bc的变量(“重命名”)
  • const {hi, bye} = a; const b = {hi, bye}; to make an object with just those two keys in variable b , but this will also create variables hi and bye .用变量b中的这两个键制作 object ,但这也会创建变量hibye

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

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