Gatsby: [v2] Definition of two variables with the same name = cryptic error

Created on 18 Jul 2018  Â·  6Comments  Â·  Source: gatsbyjs/gatsby

Description

Hi,

Adding the following code:

const foo = 1
const foo = 1

to gatsby-node.js leads to a (at least for me) very cryptic error:

success open and validate gatsby-config — 0.149 s
error UNHANDLED REJECTION


  TypeError: Cannot read property 'file' of undefined

  - index.js:398 Scope.checkBlockScopedCollisions
    [my-default-project]/[babel-traverse]/lib/scope/index.js:398:22

  - index.js:592 Scope.registerBinding
    [my-default-project]/[babel-traverse]/lib/scope/index.js:592:16

  - index.js:496 Scope.registerDeclaration
    [my-default-project]/[babel-traverse]/lib/scope/index.js:496:14

  - index.js:244 Object.BlockScoped
    [my-default-project]/[babel-traverse]/lib/scope/index.js:244:28

  - visitors.js:318 Object.newFn
    [my-default-project]/[babel-traverse]/lib/visitors.js:318:17

  - context.js:76 NodePath._call
    [my-default-project]/[babel-traverse]/lib/path/context.js:76:18

  - context.js:44 NodePath.call
    [my-default-project]/[babel-traverse]/lib/path/context.js:44:14

  - context.js:105 NodePath.visit
    [my-default-project]/[babel-traverse]/lib/path/context.js:105:12

  - context.js:150 TraversalContext.visitQueue
    [my-default-project]/[babel-traverse]/lib/context.js:150:16

  - context.js:103 TraversalContext.visitMultiple
    [my-default-project]/[babel-traverse]/lib/context.js:103:17

  - context.js:190 TraversalContext.visit
    [my-default-project]/[babel-traverse]/lib/context.js:190:19

  - index.js:114 Function.traverse.node
    [my-default-project]/[babel-traverse]/lib/index.js:114:17

  - index.js:79 traverse
    [my-default-project]/[babel-traverse]/lib/index.js:79:12

  - index.js:144 NodePath.traverse
    [my-default-project]/[babel-traverse]/lib/path/index.js:144:25

  - index.js:828 Scope._crawl
    [my-default-project]/[babel-traverse]/lib/scope/index.js:828:10

  - index.js:749 Scope.crawl
    [my-default-project]/[babel-traverse]/lib/scope/index.js:749:10

  - index.js:744 Scope.init
    [my-default-project]/[babel-traverse]/lib/scope/index.js:744:32

  - context.js:151 NodePath.setScope
    [my-default-project]/[babel-traverse]/lib/path/context.js:151:30

  - context.js:166 NodePath.setContext
    [my-default-project]/[babel-traverse]/lib/path/context.js:166:8

  - context.js:229 NodePath.pushContext
    [my-default-project]/[babel-traverse]/lib/path/context.js:229:8

  - context.js:138 TraversalContext.visitQueue
    [my-default-project]/[babel-traverse]/lib/context.js:138:14

  - context.js:108 TraversalContext.visitSingle
    [my-default-project]/[babel-traverse]/lib/context.js:108:19

Steps to reproduce

gatsby new my-default-project https://github.com/gatsbyjs/gatsby-starter-default#v2

cd my-default-project

add the code from above to gatsby-node.js

gatsby develop

Expected result

No error or at least a better error message.

Actual result

The error from above.

Environment

System:
OS: macOS Sierra 10.12.6
CPU: x64 Intel(R) Core(TM) i5-2435M CPU @ 2.40GHz
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 10.3.0 - /usr/local/bin/node
npm: 6.2.0 - /usr/local/bin/npm
Browsers:
Chrome: 67.0.3396.99
Firefox: 58.0.1
Safari: 11.1.2
npmPackages:
gatsby: next => 2.0.0-beta.43
gatsby-plugin-react-helmet: next => 3.0.0-beta.3
npmGlobalPackages:
gatsby-cli: 1.1.58

help wanted stale? maintenance

All 6 comments

Btw., I'd like to create a small site with one or two small apps and use it in production in a month or so.

I thought it makes more sense to use the v2 beta directly, instead of upgrading the app later to v2.

Is that a good idea? How long will it take until v2 will be released?

I'm using ReasonML and ran in a few other problems (hot code reloading is not working and gatsby crashes when I rename a ReasonML file), so I'm not really sure if it's a good idea to base my app on v2 beta.

That sure is a cryptic error message, sorry you ran into that! Defining the same const twice is invalid JavaScript so there should definitely be an error of some sort, but what you're seeing there isn't very helpful.

What's happening is that Gatsby analyses your gatsby-node.js file (without actually running it) when loading plugins. The invalid JS is causing the analysis to fail, resulting in that error message.

So instead of seeing something helpful like:

error gatsby-node.js returned an error


  Error: /my-project/gatsby-node.js:10
  const foo = 1
        ^
  SyntaxError: Identifier 'foo' has already been declared

you get a babel error. A fix for this would be to catch errors here https://github.com/gatsbyjs/gatsby/blob/e176b64ca3796b263172342743095e370082999a/packages/gatsby/src/bootstrap/resolve-module-exports.js#L30-L57 and output a nicer message that points to the relevant file that couldn't be parsed.

Defining the same const twice is invalid JavaScript so there should definitely be an error of some sort, but what you're seeing there isn't very helpful.

Oh, right, I totally forgot about the difference between 'const' and 'let' and I'm only using 'const'.

But a better error message still would be great, of course.

Thanks for the information.

@d4h0 wow you just saved my evening sir! 🤗
just ran into the same situation after a huge rebase, hopefully that's the only issue I overlooked^^

Old issues will be closed after 30 days of inactivity. This issue has been quiet for 20 days and is being marked as stale. Reply here or add the label "not stale" to keep this issue open!

This issue is being closed due to inactivity. Is this a mistake? Please re-open this issue or create a new issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikestopcontinues picture mikestopcontinues  Â·  3Comments

KyleAMathews picture KyleAMathews  Â·  3Comments

magicly picture magicly  Â·  3Comments

kalinchernev picture kalinchernev  Â·  3Comments

andykais picture andykais  Â·  3Comments