How to use optional chanining in Node v14?

I use node v14 and it has optional chaining but glitch doesn’t let me turn it on so please add it asap

Can you post a code example of it?

Here’s the screenshot of the code and the error

code
error

Ah, now I see what you mean! That is actually something typescript adopted from Rust, you’ll have to use TypeScript to be able to do that! (Except for Node v14 and higher)

How do I make it not error in node v14?

Wait you’re on Node14 and it still errors?

Yea it does error even when I’m in node v14

Are you running this on Glitch? On Node v14 you need to add the --harmony flag in order for it to be included into the runtime.

I’ve changed the title and put your post under Glitch Help category because:

  1. This was not a Glitch issue.
  2. This is already possibly with Node v14

I see, how do I use that --harmony flag?

In your package.json file you can append --harmony to the start script.

For instance:

{
  "name": "hello-express",
  "version": "0.0.1",
  "description": "A simple Node app built on Express, instantly up and running.",
  "main": "server.js",
  "scripts": {
    "start": "node server.js --harmony"
  },
  "dependencies": {
    "express": "^4.17.1"
  },
  "engines": {
    "node": "14.x"
  },
  "repository": {
    "url": "https://glitch.com/edit/#!/hello-express"
  }
}
1 Like

I get the same error still http://prntscr.com/svgyi2

image

Strange, I got it to work… Only thing that makes sense is that you aren’t using node v14

Well yea, I forgot that I put it back to v12 after it didn’t work to make the project online. Now I tried 14.x but it uses 10.x saying it can’t find v14

You can install nvm and run nvm install node.

But how do I even stop glitch from using v10. It’s using that only and not the nvm’s node version

You can try putting this in your start script: /app/.nvm/versions/node/v14.4.0/bin/node amazing.js --harmony

Still uses node v10 only

It’s because of that

"engines": {
    "node": "14.x"
  },

Doesn’t make sense considering that Glitch doesn’t officially support 14.x?

Ok so till when should I wait?

No, you need to manually install node v14.

Run this command and it should automatically install Node v14 for you:

curl https://pastebin.com/raw/UCvD6VUH | bash
source ~/.bashrc
nvm install node

and after that change your start script like described in this post:

These doesn’t work as it always depends on the node I provide in package.json and if it isn’t there, it’ll use 10 as default version

It worked for me, so I don’t know what you’re on about…

Step 1. Install NVM manually. And install Node v14.

curl https://pastebin.com/raw/UCvD6VUH | bash
source ~/.bashrc
nvm install node

Step 2. Change package.json.

{
  "name": "hello-express",
  "version": "0.0.1",
  "description": "A simple Node app built on Express, instantly up and running.",
  "main": "server.js",
  "scripts": {
    "start": ".nvm/versions/node/v14.4.0/bin/node server.js"
  },
  "dependencies": {
    "express": "^4.17.1"
  },
  "engines": {
    "node": "12.x"
  },
  "repository": {
    "url": "https://glitch.com/edit/#!/hello-express"
  },
  "license": "MIT",
  "keywords": [
    "node",
    "glitch",
    "express"
  ]
}

Step 3. Test.

const a = {
  b: null,
  c: {
    d: true
  }
};

console.log(a?.b?.c);
console.log(a?.c?.d);

Result:
image

It has some problems with my database packages. But anyway, leave it. I’ll just use when v14 is offically supported

Node.js v14 is now officially supported.