gatsbyjs / gatsby

The best React-based framework with performance, scalability and security built in.
https://www.gatsbyjs.com
MIT License
55.27k stars 10.31k forks source link

"Either the current working directory does not contain a valid package.json…" when running with Node.js 20 #38625

Open wojtekmaj opened 1 year ago

wojtekmaj commented 1 year ago

Preliminary Checks

Description

Running gatsby completely fails on Node.js 20 on my configuration.

isLocalGatsbySite function is silently crashing here:

https://github.com/gatsbyjs/gatsby/blob/47ec2ac04235d9ae45614a4596a250a44398e9cf/packages/gatsby-cli/src/create-cli.ts#L449-L451

with the following error:

TypeError [Error]: [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module
"/my-gatsby/package.json" needs an import assertion of type "json"
    at load$1 (/my-gatsby/.pnp.loader.mjs:1458:17)
    at nextLoad (node:internal/modules/esm/hooks:832:28)
    at Hooks.load (node:internal/modules/esm/hooks:415:26)
    at MessagePort.handleMessage (node:internal/modules/esm/worker:168:24)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:807:20)
    at exports.emitMessage (node:internal/per_context/messageport:23:28) {
  code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING'
}

causing the function to return false, causing the error in question.

Reproduction Link

See steps to reproduce

Steps to Reproduce

Use Yarn v2/v3/v4 in PnP mode and run:

yarn dlx gatsby new my-gatsby
cd my-gatsby
yarn build

Expected Result

Gatsby CLI commands to execute.

Actual Result

Either the current working directory does not contain a valid package.json or 'gatsby' is not specified as a dependency

Environment

System:
    OS: macOS 14.0
    CPU: (8) arm64 Apple M2
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.7.0 - /private/var/folders/3y/swkjq1x101b76mzjgt4pm2d80000gp/T/xfs-64dd8c65/node
    Yarn: 3.6.1 - /private/var/folders/3y/swkjq1x101b76mzjgt4pm2d80000gp/T/xfs-64dd8c65/yarn
    npm: 10.1.0 - /opt/homebrew/bin/npm
  Browsers:
    Edge: 117.0.2045.55
    Safari: 17.0

Config Flags

No response

wojtekmaj commented 1 year ago

Proposal: change the line in question, and a few others where package.json is require()d to:

const { /* … */ } = JSON.parse(fs.readFileSync(path.resolve('./package.json')));