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

Cannot find package '@sentry/sveltekit' when deploying on production on a monorepo #14565

Open
3 tasks done
fuuuzz opened this issue Dec 4, 2024 · 10 comments
Open
3 tasks done
Assignees
Labels
Package: sveltekit Issues related to the Sentry SvelteKit SDK Waiting for: Community

Comments

@fuuuzz
Copy link

fuuuzz commented Dec 4, 2024

Is there an existing issue for this?

How do you use Sentry?

Self-hosted/on-premise

Which SDK are you using?

@sentry/sveltekit

SDK Version

^8.42.0

Framework Version

^2.0.0

Link to Sentry event

No response

Reproduction Example/SDK Setup

Hello!

I have a monorepo (link) with a SvelteKit client. When deploying my app to production, node build command fails because it can't find @sentry/sveltkit module in server hooks chunks.

My application is deployed on a Scalingo instance, and is using the node-adapter.

I used npx @sentry/wizard@latest -i sveltekit to install Sentry.

"Unfortunately", it works well locally, I can't reproduce the error.

I also tried to install Sentry manually, but the result is the same.

  • client/package.json
{
  "name": "@boris/client",
  "version": "1.0.0",
  "private": true,
  "engines": {
    "node": "20.18.0",
    "npm": "10"
  },
  "scripts": {
    "dev": "vite dev",
    "build": "vite build",
    "start": "node build",
    "preview": "vite preview",
    "test": "npm run test:integration && npm run test:unit",
    "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
    "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
    "lint": "prettier --check . && eslint .",
    "format": "prettier --write .",
    "test:e2e": "playwright test",
    "test:unit": "vitest"
  },
  "devDependencies": {
    "@csstools/postcss-global-data": "^3.0.0",
    "@gouvfr/dsfr": "^1.12.1",
    "@playwright/test": "^1.28.1",
    "@sveltejs/adapter-node": "^5.2.9",
    "@sveltejs/kit": "^2.0.0",
    "@sveltejs/vite-plugin-svelte": "^4.0.0-next.6",
    "eslint-plugin-svelte": "^2.36.0",
    "globals": "^15.0.0",
    "husky": "^9.1.6",
    "nanoid": "^5.0.8",
    "papaparse": "^5.4.1",
    "postcss": "^8.4.49",
    "postcss-custom-media": "^11.0.5",
    "prettier-plugin-svelte": "^3.1.2",
    "svelte": "5.1.12",
    "svelte-check": "^4.0.0",
    "vite": "^5.0.3",
    "vitest": "^2.0.0"
  },
  "dependencies": {
    "@sentry/sveltekit": "^8.42.0"
  },
  "type": "module"
}
  • client/vite.config.ts
import { sentrySvelteKit } from '@sentry/sveltekit';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';

export default defineConfig({
  plugins: [
    sentrySvelteKit({
      sourceMapsUploadOptions: {
        org: 'boris',
        project: 'client',
        url: 'https://sentry.incubateur.net/',
      },
      adapter: 'node',
    }),
    sveltekit(),
  ],
  test: {
    include: ['tests/unit/**/*.{test,spec}.{js,ts}'],
  },
});

  • client/src/hooks.server.ts
import { sequence } from '@sveltejs/kit/hooks';
import { handleErrorWithSentry, sentryHandle } from '@sentry/sveltekit';
import Sentry from '@sentry/sveltekit';

Sentry.init({
  dsn: 'https://[email protected]/207',

  tracesSampleRate: 1.0,

  // uncomment the line below to enable Spotlight (https://spotlightjs.com)
  // spotlight: import.meta.env.DEV,
});

// If you have custom handlers, make sure to place them after `sentryHandle()` in the `sequence` function.
export const handle = sequence(sentryHandle());

// If you have a custom error handler, pass it to `handleErrorWithSentry`
export const handleError = handleErrorWithSentry();

Thanks in advance 😄 🙏

Steps to Reproduce

  1. Use wizzard to install Sentry on a sveltekit app
  2. Deploying app on Scalingo
  3. Build succeeds
  4. node build command fails

Expected Result

@sentry/sveltekit module should be found on SvelteKit's server hook during node build

Actual Result

2024-12-04 10:06:22.967657174 +0100 CET [manager] container [web-1] (67501b789982697789ed7927) started
2024-12-04 10:06:23.237611209 +0100 CET [web-1] > @boris/[email protected] start
2024-12-04 10:06:23.237623402 +0100 CET [web-1] > node build
2024-12-04 10:06:23.336066668 +0100 CET [web-1] at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:542:12)
2024-12-04 10:06:23.336067187 +0100 CET [web-1] at ModuleLoader.resolve (node:internal/modules/esm/loader:510:25)
2024-12-04 10:06:23.336059994 +0100 CET [web-1] throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null);
2024-12-04 10:06:23.336051097 +0100 CET [web-1] node:internal/modules/esm/resolve:854
2024-12-04 10:06:23.336067664 +0100 CET [web-1] at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:239:38)
2024-12-04 10:06:23.336060834 +0100 CET [web-1] ^
2024-12-04 10:06:23.336061872 +0100 CET [web-1] Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@sentry/sveltekit' imported from /app/build/server/chunks/hooks.server-C7Jx9NsL.js
2024-12-04 10:06:23.336068706 +0100 CET [web-1] at link (node:internal/modules/esm/module_job:95:36) {
2024-12-04 10:06:23.336068179 +0100 CET [web-1] at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:96:40)
2024-12-04 10:06:23.336062544 +0100 CET [web-1] at packageResolve (node:internal/modules/esm/resolve:854:9)
2024-12-04 10:06:23.336069085 +0100 CET [web-1] code: 'ERR_MODULE_NOT_FOUND'
2024-12-04 10:06:23.336062995 +0100 CET [web-1] at moduleResolve (node:internal/modules/esm/resolve:927:18)
2024-12-04 10:06:23.336069492 +0100 CET [web-1] }
2024-12-04 10:06:23.336070139 +0100 CET [web-1] Node.js v20.18.0
2024-12-04 10:06:23.336063436 +0100 CET [web-1] at defaultResolve (node:internal/modules/esm/resolve:1169:11) 
@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Dec 4, 2024
@github-actions github-actions bot added the Package: svelte Issues related to the Sentry Svelte SDK label Dec 4, 2024
@Lms24
Copy link
Member

Lms24 commented Dec 4, 2024

Hey @fuuuzz thanks for writing in! Any chance that scalingo caches dependencies or something along these lines? I'm not aware of this error being reported before so not entirely sure if this is really an SDK-related problem (yet).

@getsantry getsantry bot removed the status in GitHub Issues with 👀 3 Dec 4, 2024
@Lms24 Lms24 added Package: sveltekit Issues related to the Sentry SvelteKit SDK Waiting for: Product Owner labels Dec 4, 2024
@Lms24 Lms24 added Waiting for: Product Owner and removed Package: svelte Issues related to the Sentry Svelte SDK labels Dec 4, 2024
@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Dec 4, 2024
@getsantry getsantry bot removed the status in GitHub Issues with 👀 3 Dec 4, 2024
@getsantry getsantry bot moved this to Waiting for: Community in GitHub Issues with 👀 3 Dec 4, 2024
@Lms24 Lms24 self-assigned this Dec 4, 2024
@fuuuzz
Copy link
Author

fuuuzz commented Dec 4, 2024

Hello @Lms24 ! Thanks for reaching back 😃

Scalingo use something they call Deployment cache: https://doc.scalingo.com/platform/deployment/cache

Following their documentation, I cleared the cache, but it had no effect. I also tried to deploy on a fresh instance, and still did not work.

Do you see anything odd I implemented ? I might missed some trivial stuff 😅

Thanks again for your replies!

@getsantry getsantry bot moved this from Waiting for: Community to Waiting for: Product Owner in GitHub Issues with 👀 3 Dec 4, 2024
@Lms24
Copy link
Member

Lms24 commented Dec 4, 2024

Nothing out of the ordinary tbh! The config looks reasonable to me. On first glance I suspected that the SDK package was registered as in devDependencies rather than dependencies but this is not the case (dependencies is the correct one).

Given that it is the only dependency though, any chance something generally goes wrong with packages in dependencies?

The other angle would be the monorepo setup. I'd assume the package is hoisted into the monorepo's top level node_modules? Maybe scalingo has a problem with resolving the dependencies correctly? I think you could check if this is the case by globally installing a @sentry/sveltekit dependency in another version than the one you use in your package. Obviously not an ideal solution but maybe worth a try just to check if we're on the right track

@fuuuzz
Copy link
Author

fuuuzz commented Dec 4, 2024

Given that it is the only dependency though, any chance something generally goes wrong with packages in dependencies?

Not that I know tbh

The other angle would be the monorepo setup. I'd assume the package is hoisted into the monorepo's top level node_modules? Maybe scalingo has a problem with resolving the dependencies correctly? I think you could check if this is the case by globally installing a @sentry/sveltekit dependency in another version than the one you use in your package. Obviously not an ideal solution but maybe worth a try just to check if we're on the right track

So I went down this road after your suggestion 😅

  1. Added a @sentry/sveltekit dependency to the global package.json with a different version (8.41.0 instead of 8.42.0 currently in client/package.json
  2. Deployment failed, this time the missing module was @sentry/node
  3. Installed @sentry/node, both globally and in the client workspace
  4. Deployment failed, this time the missing module was @sentry/core
  5. Installed @sentry/core, both globally and in the client workspace
  6. Deployment failed, this time the missing module was@opentelemetry/api
  7. Installed @opentelemetry/api, but still missing into production logs after node build

I thought I had something but I got stuck at @opentelemetry/api 😢

Here are the packages config, i guess only dependencies section will interest you!

root package.json

{
  "name": "boris",
  "version": "1.0.0",
  "private": true,
  "engines": {
    "node": "20.18.0",
    "npm": "10"
  },
  "workspaces": [
    "client"
  ],
  "scripts": {
    "prepare": "husky"
  },
  "devDependencies": {
    "@eslint/js": "^9.12.0",
    "@types/eslint": "^9.6.0",
    "@typescript-eslint/eslint-plugin": "^8.8.0",
    "@typescript-eslint/parser": "^8.8.0",
    "eslint": "^9.0.0",
    "eslint-config-prettier": "^9.1.0",
    "lint-staged": "^15.2.5",
    "prettier": "^3.1.1",
    "typescript": "5.6.3",
    "typescript-eslint": "^8.0.0"
  },
  "dependencies": {
    "@opentelemetry/api": "1.9.0",
    "@sentry/core": "8.41.0",
    "@sentry/node": "8.41.0",
    "@sentry/sveltekit": "8.41.0"
  }
}

client/package.json

{
  "name": "@boris/client",
  "version": "1.0.0",
  "private": true,
  "engines": {
    "node": "20.18.0",
    "npm": "10"
  },
  "scripts": {
    "dev": "vite dev",
    "build": "vite build",
    "start": "node build",
    "preview": "vite preview",
    "test": "npm run test:integration && npm run test:unit",
    "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
    "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
    "lint": "prettier --check . && eslint .",
    "format": "prettier --write .",
    "test:e2e": "playwright test",
    "test:unit": "vitest"
  },
  "devDependencies": {
    "@csstools/postcss-global-data": "^3.0.0",
    "@gouvfr/dsfr": "^1.12.1",
    "@playwright/test": "^1.28.1",
    "@sveltejs/adapter-node": "^5.2.9",
    "@sveltejs/kit": "^2.0.0",
    "@sveltejs/vite-plugin-svelte": "^4.0.0-next.6",
    "eslint-plugin-svelte": "^2.36.0",
    "globals": "^15.0.0",
    "husky": "^9.1.6",
    "nanoid": "^5.0.8",
    "papaparse": "^5.4.1",
    "postcss": "^8.4.49",
    "postcss-custom-media": "^11.0.5",
    "prettier-plugin-svelte": "^3.1.2",
    "svelte": "5.1.12",
    "svelte-check": "^4.0.0",
    "vite": "^5.0.3",
    "vitest": "^2.0.0"
  },
  "dependencies": {
    "@opentelemetry/api": "1.9.0",
    "@sentry/core": "^8.42.0",
    "@sentry/node": "^8.42.0",
    "@sentry/sveltekit": "^8.42.0"
  },
  "type": "module"
}

Logs output from Scalingo

2024-12-04 16:49:17.884377796 +0100 CET [manager] container [web-1] (675079eb9982697789ee5f8f) started
2024-12-04 16:49:18.215231204 +0100 CET [web-1] > @boris/[email protected] start
2024-12-04 16:49:18.215242974 +0100 CET [web-1] > node build
2024-12-04 16:49:18.366217588 +0100 CET [web-1] '/app/node_modules/@sentry/sveltekit/build/cjs/server/index.js',
2024-12-04 16:49:18.366051825 +0100 CET [web-1] Require stack:
2024-12-04 16:49:18.366114777 +0100 CET [web-1] code: 'MODULE_NOT_FOUND',
2024-12-04 16:49:18.366053117 +0100 CET [web-1] - /app/node_modules/@sentry/node/build/cjs/index.js
2024-12-04 16:49:18.366218832 +0100 CET [web-1] ]
2024-12-04 16:49:18.366049391 +0100 CET [web-1] const err = new Error(message);
2024-12-04 16:49:18.366114408 +0100 CET [web-1] at Module.require (node:internal/modules/cjs/loader:1311:19) {
2024-12-04 16:49:18.366147526 +0100 CET [web-1] requireStack: [
2024-12-04 16:49:18.366219523 +0100 CET [web-1] Node.js v20.18.0
2024-12-04 16:49:18.366100398 +0100 CET [web-1] at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)
2024-12-04 16:49:18.366098198 +0100 CET [web-1] at Module.require (node:internal/modules/cjs/loader:1311:19)
2024-12-04 16:49:18.366052342 +0100 CET [web-1] - /app/node_modules/@sentry/node/build/cjs/integrations/http/index.js
2024-12-04 16:49:18.366051268 +0100 CET [web-1] Error: Cannot find module '@opentelemetry/api'
2024-12-04 16:49:18.366099376 +0100 CET [web-1] at Object.<anonymous> (/app/node_modules/@sentry/node/build/cjs/integrations/http/index.js:7:13)
2024-12-04 16:49:18.366212131 +0100 CET [web-1] '/app/node_modules/@sentry/node/build/cjs/index.js',
2024-12-04 16:49:18.366053626 +0100 CET [web-1] - /app/node_modules/@sentry/sveltekit/build/cjs/server/index.js
2024-12-04 16:49:18.366101612 +0100 CET [web-1] at Module._load (node:internal/modules/cjs/loader:1104:12)
2024-12-04 16:49:18.366093833 +0100 CET [web-1] at Module._load (node:internal/modules/cjs/loader:1051:27)
2024-12-04 16:49:18.366035286 +0100 CET [web-1] node:internal/modules/cjs/loader:1225
2024-12-04 16:49:18.366218095 +0100 CET [web-1] '/app/node_modules/@sentry/sveltekit/build/cjs/index.server.js'
2024-12-04 16:49:18.366100761 +0100 CET [web-1] at Module.load (node:internal/modules/cjs/loader:1288:32)
2024-12-04 16:49:18.366050751 +0100 CET [web-1] ^
2024-12-04 16:49:18.366219150 +0100 CET [web-1] }
2024-12-04 16:49:18.366152151 +0100 CET [web-1] '/app/node_modules/@sentry/node/build/cjs/integrations/http/index.js',
2024-12-04 16:49:18.366054761 +0100 CET [web-1] at Module._resolveFilename (node:internal/modules/cjs/loader:1225:15)
2024-12-04 16:49:18.366100030 +0100 CET [web-1] at Module._compile (node:internal/modules/cjs/loader:1469:14)
2024-12-04 16:49:18.366098792 +0100 CET [web-1] at require (node:internal/modules/helpers:179:18)
2024-12-04 16:49:18.366054422 +0100 CET [web-1] - /app/node_modules/@sentry/sveltekit/build/cjs/index.server.js 

(You can still see the repo if needed here)

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Dec 4, 2024
@fuuuzz fuuuzz changed the title Cannot find package '@sentry/sveltekit' when deploying on production Cannot find package '@sentry/sveltekit' when deploying on production on a monorepo Dec 4, 2024
@Lms24
Copy link
Member

Lms24 commented Dec 5, 2024

So I went down this road after your suggestion 😅

Reads like a "fun" experiment, thanks for still doing it! 😅

So this makes me think that it is indeed related to the hoisting of the dependency. Once you added one of the deps specifically to the node_modules of the application, it was found but now the next one in the dependency graph was missing.

But this is weird again. Because @sentry/[email protected] should specifically require @sentry/[email protected] -> @sentry/[email protected] etc.... Why would it even look for non-matching versions in the top level node_modules?

I will check out your repo to see if it installs correctly on my system. Not sure if I can deploy to scalingo though.

@fuuuzz
Copy link
Author

fuuuzz commented Dec 5, 2024

Reads like a "fun" experiment, thanks for still doing it! 😅

I actually had fun 😆

I will check out your repo to see if it installs correctly on my system. Not sure if I can deploy to scalingo though.

Again, i have no issue locally, it builds smoothly, which points to a certain issue with scalingo hoisting imo. Don't hesitate to fork the repo if you want to deploy it to your own scalingo instance. I can also open one for you and add you as a collaborator.

What would be your strategy to dig in ?

Thanks again for taking time to investigate!

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Dec 5, 2024
@Lms24
Copy link
Member

Lms24 commented Dec 5, 2024

Just to confirm: You are using npm, not pnpm correct? Also on Scalingo?

@fuuuzz
Copy link
Author

fuuuzz commented Dec 5, 2024

Just to confirm: You are using npm, not pnpm correct? Also on Scalingo?

Yes! Good old npm

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Dec 5, 2024
@Lms24
Copy link
Member

Lms24 commented Dec 5, 2024

Okay, I did some digging and I can repro that if you set a specific version for the client/package.json and a specific lower version in the root package.json, the transitive dependencies are still hoisted into the top level node_modules. So based on your description, I still think that something is going wrong on Scalingo. Usually, dependency resolution should work with monorepo-structured node_modules.

I recommend raising this with Scalingo. My suspicion is that they don't handle npm workspace monorepo deploys correctly. Fwiw, this is the first time I'm hearing about this specific issue so I don't think it's related to the sveltekit SDK package specifically.

Also sorry that we can't help you more on our end.

@fuuuzz
Copy link
Author

fuuuzz commented Dec 6, 2024

Noted! I'll look into it with Scalingo. I'll reach back when I find a solution :)

Thanks a lot for your time, really appriciate it 😁

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Dec 6, 2024
@getsantry getsantry bot removed the status in GitHub Issues with 👀 3 Dec 6, 2024
@getsantry getsantry bot moved this to Waiting for: Community in GitHub Issues with 👀 3 Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: sveltekit Issues related to the Sentry SvelteKit SDK Waiting for: Community
Projects
Status: Waiting for: Community
Development

No branches or pull requests

2 participants