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

[Bug]: Invariant failed: Expected package.json#version to be defined in the "undefined" package} #28606

Closed
bakervos opened this issue Jul 15, 2024 · 16 comments · Fixed by #28752
Labels
bug nx Prioritize Nx compatibility for angular sev:S2 upgrade:8.2

Comments

@bakervos
Copy link

bakervos commented Jul 15, 2024

Describe the bug

While upgrading my project from storybook 7 to 8, I noticed that serving does not work any more, with the following error:

Error: Invariant failed: Expected package.json#version to be defined in the "undefined" package}
    at invariant (/home/jojoxd/Development/<PROJECT>/node_modules/@storybook/core/dist/core-server/index.cjs:187629:11)
    at buildDevStandalone (/home/jojoxd/Development/<PROJECT>/node_modules/@storybook/core/dist/core-server/index.cjs:189878:3)
    at Object.build (/home/jojoxd/Development/<PROJECT>/node_modules/@storybook/core/dist/core-server/index.cjs:190006:12)
    at runInstance (/home/jojoxd/Development/<PROJECT>/node_modules/@nx/storybook/src/executors/storybook/storybook.impl.js:26:18)
    at storybookExecutor (/home/jojoxd/Development/<PROJECT>/node_modules/@nx/storybook/src/executors/storybook/storybook.impl.js:12:26)
    at storybookExecutor.next (<anonymous>)
    at getLastValueFromAsyncIterableIterator (/home/jojoxd/Development/<PROJECT>/node_modules/nx/src/utils/async-iterator.js:15:34)
    at iteratorToProcessStatusCode (/home/jojoxd/Development/<PROJECT>/node_modules/nx/src/command-line/run/run.js:44:94)
    at /home/jojoxd/Development/<PROJECT>/node_modules/nx/src/command-line/run/run.js:187:16
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

It looks like this change was made incorrectly, as the package.json's content should be read, instead of fetching the dirname.

Reproduction link

https://example.com/there-is-no-reproduction

Reproduction steps

No response

System

Storybook Environment Info:

  System:
    OS: Linux 6.8 Ubuntu 24.04 LTS 24.04 LTS (Noble Numbat)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 20.12.2 - ~/.nvm/versions/node/v20.12.2/bin/node
    npm: 10.5.0 - ~/.nvm/versions/node/v20.12.2/bin/npm <----- active
  npmPackages:
    @storybook/addon-essentials: 8.2.2 => 8.2.2 
    @storybook/addon-interactions: 8.2.2 => 8.2.2 
    @storybook/core-server: 8.2.2 => 8.2.2 
    @storybook/jest: ^0.2.3 => 0.2.3 
    @storybook/test-runner: ^0.19.1 => 0.19.1 
    @storybook/testing-library: ^0.2.2 => 0.2.2 
    @storybook/vue3-vite: 8.2.2 => 8.2.2

Additional context

This NX Issue is related: nrwl/nx#26914

Copy link
Contributor

greptile-apps bot commented Jul 15, 2024

** Disclaimer** This information might be inaccurate, due to it being generated automatically
To resolve the issue, modify the core-server module to correctly read the package.json version. Specifically, update the index.ts and utils/package-json.ts files.

  1. index.ts: Ensure the package.json is being read correctly.
import { readPackageJson } from './utils/package-json';

const packageJson = readPackageJson(dirname);
const version = packageJson.version;
if (!version) {
  throw new Error(`Invariant failed: Expected package.json#version to be defined in the "${dirname}" package`);
}
  1. utils/package-json.ts: Implement a function to read package.json.
import fs from 'fs';
import path from 'path';

export function readPackageJson(dirname: string) {
  const packageJsonPath = path.join(dirname, 'package.json');
  if (!fs.existsSync(packageJsonPath)) {
    throw new Error(`package.json not found in ${dirname}`);
  }
  return JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
}

About Greptile

This response provides a starting point for your research, not a precise solution.

Help us improve! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

Edit Issue Bot Settings

@shilman shilman added nx Prioritize Nx compatibility for angular sev:S2 upgrade:8.2 and removed needs triage labels Jul 16, 2024
@shilman
Copy link
Member

shilman commented Jul 16, 2024

@bakervos Sorry for the hassle! It seems like we've broken the nx integration in 8.2. We're meeting with the nx team later this week to get it resolved. In the meantime, if you upgrade to 8.1 I think you should be OK (though nx support for 8.x isn't official yet AFAIK).

@dgonzalezr
Copy link

@bakervos Sorry for the hassle! It seems like we've broken the nx integration in 8.2. We're meeting with the nx team later this week to get it resolved. In the meantime, if you upgrade to 8.1 I think you should be OK (though nx support for 8.x isn't official yet AFAIK).

@shilman just a note to add, this only happens when serving Storybook (start command), storybook build still works as expected without any issue

@shilman
Copy link
Member

shilman commented Jul 16, 2024

@bakervos @dgonzalezr As a workaround, you might also try adding the storybook package as a dev dependency in your project.

@dgonzalezr
Copy link

dgonzalezr commented Jul 16, 2024

@shilman I have the storybook package as a dev dependency from some time ago. As a workaround, I used npm patch-package to patch @storybook/core reverting the affecting changes mentioned by @bakervos.

@faisal-ethos
Copy link

faisal-ethos commented Jul 17, 2024

Is there any workaround? Facing this issue after doing nx migration.

@marviobezerra
Copy link

+1

@shubhsk88
Copy link

I am facing this same issue while upgrading my storybook in nx

@parostatkiem
Copy link

I'm waiting for the solution too 🙏

@siiron
Copy link

siiron commented Jul 25, 2024

+1 facing the same issue when upgrading Storybook in my NX monorepo.

@roeb
Copy link

roeb commented Jul 29, 2024

+1 I have the same error. All dependencies of storybook have the same version.

@abcdmku
Copy link
Contributor

abcdmku commented Jul 29, 2024

Hey guys I opened a PR here let me know if this is working on your machine.

cc: @Coly010

@ilhamsa1
Copy link

ilhamsa1 commented Aug 2, 2024

+1 I have same issues when upgraded from 7.5.3 to 8.2.7

image
image

@christopher-drifte
Copy link

Also seeing this issue, looking forward to a fix

@abcdmku
Copy link
Contributor

abcdmku commented Aug 2, 2024

@ilhamsa1 and @christopher-drifte if you want to get storybook running in the meantime try removing the storybook target on your project.json file listed on the libraries you are running storybook for:

    "storybook": {
      "executor": "@nx/storybook:storybook",
      "options": {
        "port": 4402,
        "configDir": "libs/component-lib/.storybook"
      },
      "configurations": {
        "ci": {
          "quiet": true
        }
      }
    }

if your project doesn't already have it defined, go to the nx.json file and add storybook as a plugin:

{
 // other config definitions
 "plugins": [ // this might be defined already. if so, just add storybook to the list if its not there.
   {
     "plugin": "@nx/storybook/plugin",
     "options": {
       "serveStorybookTargetName": "storybook", // <-- this is the the command that you are running 
       "buildStorybookTargetName": "build-storybook",
       "testStorybookTargetName": "test-storybook",
       "staticStorybookTargetName": "static-storybook"
     }
   },
   // other plugins
 ]
}

@Benjythebee
Copy link

Benjythebee commented Aug 5, 2024

Am using Storybook 8.2.7 and am still facing this issue apparently;

EDIT: just noticed the fix was merged 3 days ago; and the release isnt out yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug nx Prioritize Nx compatibility for angular sev:S2 upgrade:8.2
Projects
Archived in project