I am new to coding in the backend environment, and I'm using glitch.com 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
commands 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
commands 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?
So I finally got this working. There is no need to compile 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-compiled.js && node script-compiled.js
start
script is good for pre-compiling 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 compiles at runtime rather than creating an output file, I don't have to worry about coordinating it's dependencies, which also need to be compiled, it's all compiled 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 compiling 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"]
}