Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cross-env and esm together for gatsby-develop on windows powershell #28

Closed
wesbos opened this issue Sep 9, 2020 · 40 comments
Closed

Comments

@wesbos
Copy link
Owner

wesbos commented Sep 9, 2020

Here is a tricky one I can't get working. I'm using es modules for my gatsby-config.js and gatsby-node.js files. Currently they only support common JS, so I am using the esm package to use import/export syntax.

The way we require esm is by setting NODE_OPTIONS="-r esm" env variable, which requires the esm package first before running gatsby develop. To get it working on windows, we use the cross-env package.

So the command looks like this:

"develop": "cross-env NODE_OPTIONS=\"-r esm\" gatsby develop",

Works great on a mac, but with windows powershell I get Error 10123 We encountered an error while trying to load your site's gatsby-config. Please fix the error and try again.. This only happens when there is a plugins array in gatsby-config.js. Taking it out solves the problem.

So to reproduce the error:

  1. Make sure you are on Node >=14
  2. Clone this repo
  3. Go to finished-files/gatsby
  4. run npm install
  5. run npm run develop

If you see GraphQL / Sanity errors, that is fine and means it worked. Don't worry about the site building, I just want to get past the above error.

Not acceptable solutions:

  1. Just use a .env file - that is introduced later in the course
  2. Just use WSL. I need this to work on powershell
  3. Just use docker - see above
  4. LOL BUY A MAC

Ideally npm run develop will work on all platforms, but I'm okay with a npm run develop:win command.

Things I've tried:

  1. Using set NODE_OPTIONS="-r esm" && gatsby develop - gatsby runs in a sub-process and it doesn't seem to transfer the variable down
  2. using cross-env-shell - it didn't work but not sure if I'm using it correctly.

Happy to pay $250 USD to anyone who can get it running.

@wesbos wesbos changed the title Use cross-env and esm together for gatsby-develop on windows powersell Use cross-env and esm together for gatsby-develop on windows powershell Sep 9, 2020
@Daniel-Griffiths
Copy link

This is not a solution, but one thing to note is that that it tries to find a file that doesnt exist \node_modules\\gatsby-plugin-react-helmet\\gatsby-config

It's got me curious now too 😆

@askkaz
Copy link

askkaz commented Sep 9, 2020

yeah, the comments around similar issues mention that mac is more graceful at handling missing files than windows. I'm guessing there is some filename weirdness between windows/mac, but I'm still waiting for my old windows machine to boot/update

@wesbos
Copy link
Owner Author

wesbos commented Sep 9, 2020

yes! and if you take out that plugin, it just errors on the next plugin. I'm bettering esm is doing something weird with the require path lookup

@Asjas
Copy link
Contributor

Asjas commented Sep 9, 2020

Removing the plugins in their entirety makes it boot up to the Gatsby GraphQL errors 👀

@wesbos
Copy link
Owner Author

wesbos commented Sep 9, 2020

Yeah that is noted above, if you get those graphql errors, it means it's working! So what about the plugins array is making it do that..

@Daniel-Griffiths
Copy link

Daniel-Griffiths commented Sep 9, 2020

For reference running

$env:NODE_OPTIONS='-r esm'; npm run develop

with the develop command set to

"develop": "gatsby develop",

Results in the same error as using cross env, so I don't think the issue lies with the cross env package.

@wesbos
Copy link
Owner Author

wesbos commented Sep 9, 2020

Here is the whole stack trace from the error:

 ERROR #10123  CONFIG

We encountered an error while trying to load your site's gatsby-config. Please fix the err
or and try again.



  Error: C:\Users\wes\Desktop\master-gatsby-master\finished-files\gatsby\node_modules\gats
  by\dist\bootstrap\get-config-file.js:1
  Error: Cannot find module 'C:\\Users\\wes\\Desktop\\master-gatsby-master\\finished-files
  \\gatsby\\node_modules\\gatsby-plugin-styled-components\\gatsby-config'
  Require stack:
  - C:\Users\wes\Desktop\master-gatsby-master\finished-files\gatsby\node_modules\gatsby\di
  st\bootstrap\get-config-file.js
  - C:\Users\wes\Desktop\master-gatsby-master\finished-files\gatsby\node_modules\gatsby\di
  st\bootstrap\index.js
  - C:\Users\wes\Desktop\master-gatsby-master\finished-files\gatsby\node_modules\gatsby\di
  st\commands\develop-process.js
  - C:\Users\wes\Desktop\master-gatsby-master\finished-files\gatsby\.cache\tmp-16336-XwaIQ
  L5eWhQb

@Daniel-Griffiths
Copy link

Not sure if it helps but I get a slightly longer stack trace

 ERROR #10123  CONFIG

We encountered an error while trying to load your site's gatsby-config. Please fix the error and try again.



  Error: C:\Users\Daniel\master-gatsby\finished-files\gatsby\node_modules\gatsby\dist\bootstrap\get-config-file.js:1
  Error: Cannot find module 'C:\\Users\\Daniel\\master-gatsby\\finished-files\\gatsby\\node_modules\\gatsby-plugin-react-helmet\\gatsby-config'
  Require stack:
  - C:\Users\Daniel\master-gatsby\finished-files\gatsby\node_modules\gatsby\dist\bootstrap\get-config-file.js
  - C:\Users\Daniel\master-gatsby\finished-files\gatsby\node_modules\gatsby\dist\bootstrap\index.js
  - C:\Users\Daniel\master-gatsby\finished-files\gatsby\node_modules\gatsby\dist\commands\develop-process.js
  - C:\Users\Daniel\master-gatsby\finished-files\gatsby\.cache\tmp-6348-M4LsCDK3VOYP

  - v8-compile-cache.js:162 Function.require.resolve
    [gatsby]/[v8-compile-cache]/v8-compile-cache.js:162:23

  - get-config-file.js:32 getConfigFile
    [gatsby]/[gatsby]/dist/bootstrap/get-config-file.js:32:30

  - index.js:72 resolveTheme
    [gatsby]/[gatsby]/dist/bootstrap/load-themes/index.js:72:46

  - index.js:146
    [gatsby]/[gatsby]/dist/bootstrap/load-themes/index.js:146:28

  - util.js:16 tryCatcher
    [gatsby]/[bluebird]/js/release/util.js:16:23

  - reduce.js:166 Object.gotValue
    [gatsby]/[bluebird]/js/release/reduce.js:166:18

  - reduce.js:155 Object.gotAccum
    [gatsby]/[bluebird]/js/release/reduce.js:155:25

  - util.js:16 Object.tryCatcher
    [gatsby]/[bluebird]/js/release/util.js:16:23

  - promise.js:547 Promise._settlePromiseFromHandler
    [gatsby]/[bluebird]/js/release/promise.js:547:31

  - promise.js:604 Promise._settlePromise
    [gatsby]/[bluebird]/js/release/promise.js:604:18

  - promise.js:641 Promise._settlePromiseCtx
    [gatsby]/[bluebird]/js/release/promise.js:641:10

  - async.js:97 _drainQueueStep
    [gatsby]/[bluebird]/js/release/async.js:97:12

  - async.js:86 _drainQueue
    [gatsby]/[bluebird]/js/release/async.js:86:9

  - async.js:102 Async._drainQueues
    [gatsby]/[bluebird]/js/release/async.js:102:5


not finished open and validate gatsby-configs - 0.077s
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! slicks-slices-frontend@0.1.0 develop: `cross-env NODE_OPTIONS="-r esm" gatsby develop`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the slicks-slices-frontend@0.1.0 develop script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Daniel\AppData\Roaming\npm-cache\_logs\2020-09-09T16_38_29_809Z-debug.log
PS C:\Users\Daniel\master-gatsby\finished-files\gatsby>







@Asjas
Copy link
Contributor

Asjas commented Sep 9, 2020

@Daniel-Griffiths It's the same stack-trace 🙂 - Wes just shortened it when he posted it here

@Daniel-Griffiths
Copy link

Woops 😆

@louisjrdev
Copy link

Looks like this might be the issue, might need a PR to gatsby.
nodejs/node#31710

@Daniel-Griffiths
Copy link

@louisjrdev Yeah I noticed the same thing, the closest thing I have gotten this to work is to touch a .env and remove it later. But I know that doesnt meet the requirements 🤔

@wesbos
Copy link
Owner Author

wesbos commented Sep 9, 2020

does putting it in a .env make it run?

@louisjrdev
Copy link

See also: gatsbyjs/gatsby#23705

@Daniel-Griffiths
Copy link

Daniel-Griffiths commented Sep 9, 2020

@wesbos It changes the error

 ERROR #10123  CONFIG

We encountered an error while trying to load your site's gatsby-config. Please fix the error and try again.



  Error: C:\Users\Daniel\master-gatsby\finished-files\gatsby\gatsby-config.js:1
  (function (exports, require, module, __filename, __dirname) { import dotenv from 'dotenv';
                                                                                                    ^^^^^^
  SyntaxError: Cannot use import statement outside a module

Looks the error shifts from gatbsy config and it looks like esm is not even enabled when using a .env file. Hmm.

@louisjrdev
Copy link

I think it's less a "make ESM work with gatsby issue" than it is a "gatsby doesn't support esm fully yet".

@wesbos
Copy link
Owner Author

wesbos commented Sep 9, 2020

That error means -r esm isn't being used at all

@louisjrdev to be clear, this is using the esm package, not the built in node.js esm experimental esm

@Daniel-Griffiths
Copy link

Daniel-Griffiths commented Sep 9, 2020

@louisjrdev I am sure I got it working in the past by using Typescript but that's waaay out of scope for this 😅

@wesbos
Copy link
Owner Author

wesbos commented Sep 9, 2020

Someone on twitter mentioned it works if you create an empty gatsby-config.js file in each plugin.

So - why is gatsby looking for a config file in the plugin that doesn't exist?

@ejdigby
Copy link

ejdigby commented Sep 9, 2020

FYI you also get to the same point as @Daniel-Griffiths by replacing NODE_OPTIONS=\"-r esm\" with NODE_OPTIONS=$(\"-r esm\").

Potential gets you slightly further without having to use a .env file

@wesbos
Copy link
Owner Author

wesbos commented Sep 9, 2020

That point isn't any further, I think that method doesn't expose the NODE_OPTIONS value to the child process and thus esm package is completely ignored.

@ejdigby
Copy link

ejdigby commented Sep 9, 2020

So if you run the following you can see (I think) that the child process does get the env variable, its just a problem with how handled after that:

test.js

console.log(process.env.NODE_OPTIONS)

package.json

"develop": "cross-env NODE_OPTIONS=\"-r esm\" node test.js",

output

-r esm

Apologies if it doesn't show what I think it shows

@Daniel-Griffiths
Copy link

Ok I got it working but the solution is a little nasty. Gimme a sec to write the solution...

@louisjrdev
Copy link

Oooh I have it working too

@Daniel-Griffiths
Copy link

Daniel-Griffiths commented Sep 9, 2020

So in the path

node_modules/gatsby/dist/bootstrap/get-config-file.js

Go to line 40

image

And remove the code in that if statement so it looks like this:

image

Then I used patch-package to persists the fix: https://www.npmjs.com/package/patch-package

@wesbos
Copy link
Owner Author

wesbos commented Sep 9, 2020

Ah! I was just looking at this: gatsbyjs/gatsby#17008

I wonder if Gatsby can be patched then?

@louisjrdev
Copy link

@wesbos Okay so i think I have solved this, without modifying any node modules.

So in the gatsby-config.js make it:

module.exports = {
  pathPrefix: '/pizza',
  siteMetadata: {
    title: `Slicks Slices`,
    siteUrl: 'https://gatsby.pizza',
    description: 'The best pizza place in Hamilton!',
    twitter: '@slicksSlices',
  },
  plugins: [
    'gatsby-plugin-react-helmet',
    'gatsby-plugin-styled-components',
    {
      // this is the name of the plugin you are adding
      resolve: 'gatsby-source-sanity',
      options: {
        projectId: '0jfvvkkd',
        dataset: 'production',
        watchMode: true,
        token: process.env.SANITY_TOKEN,
      },
    },
  ],
};

Follow the instructions here:
gatsbyjs/gatsby#7810 (comment)

change the develop script in the package.json to just gatsby develop

@louisjrdev
Copy link

A bit hacky but it works, not sure if it does on mac / linux

@wesbos
Copy link
Owner Author

wesbos commented Sep 9, 2020

@louisjrdev thanks but that is removing all the ES Modules and just reverting it back to commonJS. So it gets rid of the problem, but also the feature :)

@Daniel-Griffiths
Copy link

Besides the above I can't find any other way to get this working. I think someone needs to submit a patch to Gatsby and find a real fix instead of deleting the problem 😆

patch-package has worked great for me in the past but I understand it's not the kind of thing you want to use on a course.

@wesbos
Copy link
Owner Author

wesbos commented Sep 9, 2020

Got it running based on @Daniel-Griffiths and this thread: gatsbyjs/gatsby#17008

If I crack open node_modules/gatsby/dist/utils/test-require-error.js and replace this:

const firstLine = err.toString().split(`\n`)[0];

with this:

const firstLine = err.toString().replace(/\\\\/g, '\\').split(`\n`)[0];

It builds! So it's an issue with gatsby.. Perhaps they would fancy a PR

@Asjas
Copy link
Contributor

Asjas commented Sep 9, 2020

Yeah, If I do the same it works on Windows so I can confirm that seems to be the fix to build it, and I tested on MacOS and it's still working there as well after I change it there. 👍

@Daniel-Griffiths
Copy link

I will submit a PR to gatsby shortly, then we wait... 👀

@wesbos
Copy link
Owner Author

wesbos commented Sep 9, 2020

gatsbyjs/gatsby#26840 PR

@Daniel-Griffiths
Copy link

Beat me to it 😆

@askkaz
Copy link

askkaz commented Sep 9, 2020

its surprising that this isn't a more common issue, right?

@wesbos
Copy link
Owner Author

wesbos commented Sep 9, 2020

Yeah - very bizarre. It might have something to do with esm changing how paths reported? Not sure what it does under the hood. There were a few reports of it happening in gatsby, and none in esm though.

@Daniel-Griffiths
Copy link

@askkaz I guess most people end up sticking with commonjs as it's the default. 🤷‍♂️

I saw various articles and github threads trying to find a solution, but a lot of the fixes seem outdated and no longer work.

@wesbos
Copy link
Owner Author

wesbos commented Sep 10, 2020

Merged into gatsby. Thanks everyone! @Daniel-Griffiths shoot me an email and ill get you the $250 wesbos@gmail.com

@wesbos wesbos closed this as completed Sep 10, 2020
@Daniel-Griffiths
Copy link

Daniel-Griffiths commented Sep 10, 2020

Awesome thanks @wesbos! 🙂

Will shoot you a email from [redacted]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants