Gatsby: Optional Chaining Not Working in gatsby-node.js

Created on 5 May 2020  路  3Comments  路  Source: gatsbyjs/gatsby

Description

I noticed optional chaining was added out of the box, which is great. However, I noticed that optional chaining works in source files, but not in the root level gatsby-node.js file.

Steps to reproduce

Go to this sandbox and look at the terminal and the gatsby-node.js file: https://codesandbox.io/s/fervent-lehmann-3hvlb?file=/gatsby-node.js

Expected result

Should be able to use optional chaining throughout the project including plugins, not just inside components, pages and templates. Should not need a library like dlv just for these files, the babel preset should cover the entire project.

Actual result

See this error:

Error: /sandbox/gatsby-node.js:10
    const useChain = actions?.optionalChaining
                             ^
  SyntaxError: Unexpected token .

Environment

Note: this is info from the CodeSandbox terminal:
System: OS: Linux 4.19 Debian GNU/Linux 10 (buster) 10 (buster) CPU: (12) x64 Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz Shell: 5.0.3 - /bin/bash Binaries: Node: 10.20.1 - /usr/local/bin/node Yarn: 1.22.4 - /usr/local/bin/yarn npm: 6.14.4 - /usr/local/bin/npm Languages: Python: 2.7.16 - /usr/bin/python npmPackages: gatsby: ^2.21.0 => 2.21.0 gatsby-image: ^2.4.0 => 2.4.0 gatsby-plugin-manifest: ^2.4.0 => 2.4.0 gatsby-plugin-offline: ^3.2.0 => 3.2.0 gatsby-plugin-react-helmet: ^3.3.0 => 3.3.0 gatsby-plugin-sharp: ^2.6.0 => 2.6.0 gatsby-source-filesystem: ^2.3.0 => 2.3.0 gatsby-transformer-sharp: ^2.5.0 => 2.5.0

question or discussion

All 3 comments

@graysonhicks Optional chaining works in files that in some way are handled by webpack - that includes all source files as well as gatsby-browser and gatsby-ssr. It doesn't actually work in gatsby-node (or gatsby-config), because those files are read as-is and only features that are supported by you current Node.js version will work. Optional chaining didn't land yet in Node so unfortunately you can't use it there yet (even after updating to latest Node version) ... unless you would have build/transpile step for your gatsby-node.

We will be adding some kind of support for running babel on those automatically (as part of efforts to completely support typescript out of the box), so this would solve both problem you described as well as add typescript support, but it's not yet implemented.

@pieh Yea that's what I was thinking. Will be nice to see this added. Thanks.

Thank you for opening this!

Since the original issue was answered I'll close this one. You can follow https://github.com/gatsbyjs/gatsby/issues/18983 to see how our TS progress goes. Implicitly out of this you'll then also be able to use it - for now that's not the case.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Oppenheimer1 picture Oppenheimer1  路  3Comments

dustinhorton picture dustinhorton  路  3Comments

jimfilippou picture jimfilippou  路  3Comments

ferMartz picture ferMartz  路  3Comments

brandonmp picture brandonmp  路  3Comments