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

Error: "process is undefined" when using Cloudflare with Sveltekit (9.2.0) #15506

Open
3 tasks done
tarekfb opened this issue Feb 26, 2025 · 13 comments · Fixed by #15516
Open
3 tasks done

Error: "process is undefined" when using Cloudflare with Sveltekit (9.2.0) #15506

tarekfb opened this issue Feb 26, 2025 · 13 comments · Fixed by #15516
Labels
Package: sveltekit Issues related to the Sentry SvelteKit SDK

Comments

@tarekfb
Copy link

tarekfb commented Feb 26, 2025

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/sveltekit

SDK Version

9.2.0

Framework Version

No response

Link to Sentry event

No response

Reproduction Example/SDK Setup

export const handle: Handle = sequence(initCloudflareSentryHandle({
  dsn: "my-actual-dsn-here",
  tracesSampleRate: 1.0,
}), sentryHandle(), supabase, authGuard);

Steps to Reproduce

Follow instructions in new docs, and instructions in the pr for cloudflare & sveltekit support

Publish to cloudflare.
Error is printed to console, viewed in Cloudflare Real-time Logs under Functions for the build.

Expected Result

No error occurs.

Actual Result

Error occurs. I am not using environment anywhere in the sentry setup, so I'm not sure why this issue is occuring.

Logs:

{
  "truncated": false,
  "executionModel": "stateless",
  "outcome": "ok",
  "scriptVersion": {
    "id":redacted
  },
  "scriptName": "pages-worker--redacted-preview",
  "diagnosticsChannelEvents": [],
  "exceptions": [],
  "logs": [
    {
      "message": [
        {
         redacted
        }
      ],
      "level": "log",
      "timestamp": 1740572330174
    },
    {
      "message": [
        "ReferenceError: process is not defined"
      ],
      "level": "error",
      "timestamp": 1740572330174
    },
    {
      "message": [
        "ReferenceError: process is not defined"
      ],
      "level": "error",
      "timestamp": 1740572330174
    }
  ],
  "eventTimestamp": redacted
  "event": {
    "request": {
      "url": "https://redacted.pages.dev/",
      "method": "GET",
      "headers": {
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "accept-encoding": "gzip, br",
        "accept-language": "en,en-US;q=0.9,sv-SE;q=0.8,sv;q=0.7,zh-CN;q=0.6,zh;q=0.5,la;q=0.4",
        "cache-control": "max-age=0",
        "cf-connecting-ip": redacted
        "cf-ipcountry"redacted
        "cf-ray": redacted,
        "cf-visitor": "{\"scheme\":\"https\"}",
        "connection": "Keep-Alive",
        "host": "redacted.pages.dev",
        "priority": "u=0, i",
        "referer": "https://github.com/",
        "sec-ch-ua": "\"Not(A:Brand\";v=\"99\", \"Google Chrome\";v=\"133\", \"Chromium\";v=\"133\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "document",
        "sec-fetch-mode": "navigate",
        "sec-fetch-site": "cross-site",
        "sec-fetch-user": "?1",
        "upgrade-insecure-requests": "1",
        "user-agent":redacted
        "x-forwarded-proto": "https",
        "x-real-ip": redacted
      },
      "cf": {
        "requestHeaderNames": {},
        "httpProtocol": "HTTP/3",
        "tlsCipher": "AEAD-AES128-GCM-SHA256",
        "continent": "EU",
        "asn": 2119,
        "clientAcceptEncoding": "gzip, deflate, br, zstd",
        "country":redacted
        "isEUCountry": "1",
        "verifiedBotCategory": "",
        "requestPriority": "",
        "tlsClientAuth": redacted
         tSHA1": ""
        },
        "tlsClientExtensionsSha1": "redacted
        "tlsExportedAuthenticator":redacted,
        "tlsVersion": "TLSv1.3",
        "colo": "redacted
        "timezone": "redacted
        "region": redacted
        "longitude":redacted,
        "edgeRequestKeepAliveStatus": 1,
        "tlsClientRandom": redacted,
        "latitude": redacted",
        "postalCode": "redacted,
        "city": "Solna",
        "regionCode": redacted,
        "asOrganization": redacted,
        "tlsClientHelloLength": "1761",
        "pagesHostName": redacted,
        "botManagement": {
          "corporateProxy": false,
          "verifiedBot": false,
          "jsDetection": {
            "passed": false
          },
          "staticResource": false,
          "detectionIds": {},
          "score": 99
        }
      }
    },
    "response": {
      "status": 500
    }
  },
  "id": 0
}
@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Feb 26, 2025
@github-actions github-actions bot added the Package: sveltekit Issues related to the Sentry SvelteKit SDK label Feb 26, 2025
@tarekfb
Copy link
Author

tarekfb commented Feb 26, 2025

maybe @Lms24 knows anything about this? I read your new docs saying platform.env has to be used, however as stated above, I'm not using env vars whatsoever in the sentry config

@tarekfb
Copy link
Author

tarekfb commented Feb 26, 2025

Hi, did you add the compat flag to your wrangler config? https://github.com/getsentry/sentry-docs/blob/0dd74191b9e3c032a8506657908274a3eea46bef/docs/platforms/javascript/guides/cloudflare/frameworks/sveltekit.mdx#2-cloudflare-configuration

Yes, I've set it in the cloudflare dashboard. I've just downloaded (generated) a local wrangler.toml file and it had compatibility_flags = [ "nodejs_compat" ]. I'll push that too, although it shouldnt make a difference

Edit: no difference

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Feb 26, 2025
@Lms24
Copy link
Member

Lms24 commented Feb 26, 2025

Hi, thanks for writing in! This sounds a bit strange to me as I wasn't aware that process isn't available in CF 🤔

We do access process.env when flushing out events on the server side:

export async function flushIfServerless(): Promise<void> {
const platformSupportsStreaming = !process.env.LAMBDA_TASK_ROOT && !process.env.VERCEL;

Without a stack trace it's hard to tell but maybe this is where things go wrong... I can add a guard for process but not sure if this actually fixes things or we fail further down in the chain.

@tarekfb
Copy link
Author

tarekfb commented Feb 26, 2025

Any idea when i can try changes from your pr? Thanks @Lms24

@Lms24
Copy link
Member

Lms24 commented Feb 26, 2025

Probably in the next days, since we wanted to cut another release this week anyway.

If you want to try it before the next release, you can use patch package and apply the changes from #15516 to the @sentry/sveltekit package in your node_modules.

@tarekfb
Copy link
Author

tarekfb commented Feb 27, 2025

Probably in the next days, since we wanted to cut another release this week anyway.

If you want to try it before the next release, you can use patch package and apply the changes from #15516 to the @sentry/sveltekit package in your node_modules.

I've used patch-package to apply your changes, no difference.

Is there a way I can verify the patch-package changes are published to cloudflare? I've added source maps to wrangler.toml but no stack trace in real time logs.

I did some more testing. The error was not resolved when i removed initCloudflareSentryHandle and sentryHandle. The error however did stop when I removed the sentrySvelteKit() call in plugins array from vite.config.ts.

And also some more notes, there is no error during build or publish of cloudflare preview env. However, when visiting the preview env I'm met with the error described above (and a 500 | Internal Error screen)
@Lms24

@lforst lforst reopened this Feb 27, 2025
@tarekfb
Copy link
Author

tarekfb commented Feb 28, 2025

I did some more testing.

I added the config for sourceMapsUploadOptions with my org/project/auth token. No difference

Previously i deleted sentry code in hooks.server.ts (issue remained), then deleted sentry config in vite.config.ts (no error).
I now tried to keep hooks.server.ts code, and remove sentry setup in vite.config.ts, and error remained.
@Lms24

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Feb 28, 2025
Copy link
Contributor

A PR closing this issue has just been released 🚀

This issue was referenced by PR #15516, which was included in the 9.3.0 release.

@tarekfb
Copy link
Author

tarekfb commented Feb 28, 2025

I updated to 9.3.0.

Issue sadly not resolved. Any ideas what I can do to circumvent this, in the meantime? @Lms24

@jasperrooduijn
Copy link

I updated to 9.3.0.

Issue sadly not resolved. Any ideas what I can do to circumvent this, in the meantime? @Lms24

Same here! I get this error process is not defined when running wrangler pages dev .svelte-kit/cloudflare and visiting the local wrangler URL. I have compatibility_flags = ["nodejs_compat"] defined in my wrangler.toml. Even tried passing it as a param wrangler pages dev .svelte-kit/cloudflare --compatibility-flag='nodejs_compat'.

⎔ Starting local server...
[wrangler:err] ReferenceError: process is not defined
    at flushIfServerless (*/node_modules/.pnpm/@sentry+sveltekit@9.3.0_@opentelemetry+api@1.9.0_@opentelemetry+context-async-hooks@1.30.1_@o_nllo4qeqnugsukxyzwgjjkzgli/node_modules/@sentry/sveltekit/src/server-common/utils.ts:22:3)
    at Object.handleError (*/node_modules/.pnpm/@sentry+sveltekit@9.3.0_@opentelemetry+api@1.9.0_@opentelemetry+context-async-hooks@1.30.1_@o_nllo4qeqnugsukxyzwgjjkzgli/node_modules/@sentry/sveltekit/src/server-common/handleError.ts:46:11)
    at handle_error_and_jsonify (*/.svelte-kit/output/server/index.js:153:31)
    at handle_fatal_error (*/.svelte-kit/output/server/index.js:135:23)
    at respond (*/.svelte-kit/output/server/index.js:3284:18)
    at async Object.fetch (*/.svelte-kit/cloudflare/_worker.js:72:13)
    at async jsonError (*/pnpm/global/5/.pnpm/wrangler@3.111.0/node_modules/wrangler/templates/middleware/middleware-miniflare3-json-error.ts:22:10)
    at async drainBody (*/pnpm/global/5/.pnpm/wrangler@3.111.0/node_modules/wrangler/templates/middleware/middleware-ensure-req-body-drained.ts:5:10)
    at async jsonError (*/pnpm/global/5/.pnpm/wrangler@3.111.0/node_modules/wrangler/templates/middleware/middleware-miniflare3-json-error.ts:22:10)
    at async drainBody (*/pnpm/global/5/.pnpm/wrangler@3.111.0/node_modules/wrangler/templates/middleware/middleware-ensure-req-body-drained.ts:5:10)```

@tarekfb
Copy link
Author

tarekfb commented Feb 28, 2025

It seems the error occurs where @Lms24 added a return if process undefined. Strange

@Lms24
Copy link
Member

Lms24 commented Feb 28, 2025

Hi, thanks for the additional information! I will take another look on Monday. I've been on PTO the last two days, so couldn't look at this after my last response.

Btw, I don't mind the occasional tag but there's really no need to tag me in every response. We have internal tracking tools that make sure that your replies are seen. Thanks :)

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
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

4 participants