最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I can't get Babel Optional Chaining Operators to work in glitch - Stack Overflow

programmeradmin0浏览0评论

I am new to coding in the backend environment, and I'm using glitch to write my code currently. I have a project where I'm experimenting with using passport for registering users and logging in. I've implemented a github strategy for passport, but I'm getting an error trying to record the user info in the DB. Basically passing email: profile.email[0].value || 'no public email' as one of the options is giving me a null reference error when I try to log in myself. On analyzing the profile object, I found that it was missing the email field, so I wans to do something like email: profile.email[0]?.value || 'no public email'

I've been trying for a couple days to get this to work with "@babel/plugin-proposal-optional-chaining": "^7.6.0"I'm trying to avoid npm mands for the time being, because it's a bit harder to follow what's happening so I'm trying to install what I need in packages.json (although I did try npm mands previously and couldn't that to work either).

currently I have the following in my dependencies (I've also tried moving stuff into the devDependencies):

"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"babel-register": "^6.26.0",
"@babel/plugin-proposal-optional-chaining": "^7.6.0",
"@babel/core": "^7.6.0"

I also have the following in packages.json: "babel": {"plugins": [["@babel/core"], ["@babel/plugin-proposal-optional-chaining"]]}, (I also tried it without any options in it)

That wasn't working, so I added

require("@babel/core").transform("code", {
  plugins: [["@babel/core"], ["@babel/plugin-proposal-optional-chaining"]]
});

to the auth.js file (the file that I'm trying to implement the operator in). I also tried it without the @babel/core plugin. When that didn't work, I decided to try added .babelrc with the following code:

{
  "plugins": [["@babel/core"], ["@babel/plugin-proposal-optional-chaining"]]
}

That didn't work, so I thought maybe I needed to change it to .babelrc.json and that didn't work either. Based on the documentation, it seems like at least one of those attempts should have worked. Can someone tell me what I'm doing wrong?

I am new to coding in the backend environment, and I'm using glitch. to write my code currently. I have a project where I'm experimenting with using passport for registering users and logging in. I've implemented a github strategy for passport, but I'm getting an error trying to record the user info in the DB. Basically passing email: profile.email[0].value || 'no public email' as one of the options is giving me a null reference error when I try to log in myself. On analyzing the profile object, I found that it was missing the email field, so I wans to do something like email: profile.email[0]?.value || 'no public email'

I've been trying for a couple days to get this to work with "@babel/plugin-proposal-optional-chaining": "^7.6.0"I'm trying to avoid npm mands for the time being, because it's a bit harder to follow what's happening so I'm trying to install what I need in packages.json (although I did try npm mands previously and couldn't that to work either).

currently I have the following in my dependencies (I've also tried moving stuff into the devDependencies):

"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"babel-register": "^6.26.0",
"@babel/plugin-proposal-optional-chaining": "^7.6.0",
"@babel/core": "^7.6.0"

I also have the following in packages.json: "babel": {"plugins": [["@babel/core"], ["@babel/plugin-proposal-optional-chaining"]]}, (I also tried it without any options in it)

That wasn't working, so I added

require("@babel/core").transform("code", {
  plugins: [["@babel/core"], ["@babel/plugin-proposal-optional-chaining"]]
});

to the auth.js file (the file that I'm trying to implement the operator in). I also tried it without the @babel/core plugin. When that didn't work, I decided to try added .babelrc with the following code:

{
  "plugins": [["@babel/core"], ["@babel/plugin-proposal-optional-chaining"]]
}

That didn't work, so I thought maybe I needed to change it to .babelrc.json and that didn't work either. Based on the documentation, it seems like at least one of those attempts should have worked. Can someone tell me what I'm doing wrong?

Share Improve this question edited Nov 3, 2019 at 17:59 Tara asked Nov 1, 2019 at 22:58 TaraTara 3973 silver badges16 bronze badges 1
  • Just for your reference. Might help. – Nithin Thampi Commented Nov 2, 2019 at 8:05
Add a ment  | 

3 Answers 3

Reset to default 1

So I finally got this working. There is no need to pile the files independently, and honestly, I was having a hard time getting it to work that way anyway. So here's a list of things I found out trying to get this to work.

The babel server.js --out-file script-piled.js && node script-piled.js start script is good for pre-piling the main script file being served. As there were other .js files that server.js depended on; however, I had a hard time getting that to work. Instead, the original line I had, babel-node server.js i sufficient. Because it piles at runtime rather than creating an output file, I don't have to worry about coordinating it's dependencies, which also need to be piled, it's all piled at once at runtime.

There are two reasons it wasn't working: 1: I omitted the @babel/node dependency, and 2: in the process of doing my research, found out that the babel engine itself doesn't do anything, and instead relies on plugins for piling what it needs to. I already had the @babel/plugin-proposal-optional-chaining dependency, but that was it. There is apparently a whole library of pre-set dependencies called preset-env that's included with the dependency @babel/preset-env, after including the dependency, I then had to include it in the .babelrc file, in addition to the plugin I was actually looking for:

{
  "plugins": ["@babel/plugin-proposal-optional-chaining"],
  "presets": ["@babel/env"]
}

The changes should work with the .babelrc file.

{
  "plugins": ["@babelcore", "@babel/plugin-proposal-optional-chaining"]
}

However, you are not running the code transpiled by babel as per the below mand.

"start": "node server.js --exec babel-node --presets es2015,stage-2"

It should look like something like below.

"scripts": {
    "start": "babel server.js --out-file script-piled.js && node script-piled.js"
  }

Here I'm passing only a single file to be transpiled by babel

server.js => script-piled.js

You can take a look at Compiling Directories from documentation.

Also looks like you are depending on some templating engine. You might need to make sure that you use absolute paths for the templates or copy the templates to a location accessible by the transpiled script folder

In my case adding:

"engines": {
   "node": ">=16.x"
}

to package.json resolved problem

发布评论

评论列表(0)

  1. 暂无评论