withastro / adapters

Home for Astro's core maintained adapters
47 stars 26 forks source link

Error: Could not resolve "node:stream" when deploying to Cloudflare Pages #203

Closed faizananwerali closed 3 months ago

faizananwerali commented 3 months ago

Astro Info

Astro                    v4.5.9
Node                     v18.19.1
System                   macOS (arm64)
Package Manager          npm
Output                   server
Adapter                  @astrojs/cloudflare
Integrations             @astrojs/react
                         @astrojs/tailwind
                         @astrojs/sitemap
                         astro-robots-txt
                         astro-imagetools
                         astro-compress

Describe the Bug

00:54:35.653    Cloning repository...
00:54:36.878    From https://github.com/faizananwerali/portfolio-astro
00:54:36.878     * branch            0c88957d454eeacd9cb759b55342e14865cebab9 -> FETCH_HEAD
00:54:36.878    
00:54:36.961    HEAD is now at 0c88957 fixing cloudflare astro adaptor code.
00:54:36.962    
00:54:37.046    
00:54:37.047    Using v2 root directory strategy
00:54:37.071    Success: Finished cloning repository files
00:54:38.976    Detected the following tools from environment: nodejs@18.19.1, npm@9.6.7
00:54:38.976    Installing nodejs 18.19.1
00:54:39.554    Trying to update node-build... ok
00:54:39.759    Downloading node-v18.19.1-linux-x64.tar.gz...
00:54:39.759    -> https://nodejs.org/dist/v18.19.1/node-v18.19.1-linux-x64.tar.gz
00:54:41.341    
00:54:41.341    WARNING: node-v18.19.1-linux-x64 is in LTS Maintenance mode and nearing its end of life.
00:54:41.341    It only receives *critical* security updates, *critical* bug fixes and documentation updates.
00:54:41.341    
00:54:41.341    Installing node-v18.19.1-linux-x64...
00:54:41.721    Installed node-v18.19.1-linux-x64 to /opt/buildhome/.asdf/installs/nodejs/18.19.1
00:54:41.721    
00:54:42.808    Installing project dependencies: npm clean-install --progress=false
00:54:50.853    npm WARN deprecated phin@2.9.3: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.
00:54:50.938    npm WARN deprecated sourcemap-codec@1.4.8: Please use @jridgewell/sourcemap-codec instead
00:54:51.102    npm WARN deprecated rollup-plugin-inject@3.0.2: This package has been deprecated and is no longer maintained. Please use @rollup/plugin-inject.
00:55:10.795    
00:55:10.795    added 822 packages, and audited 823 packages in 27s
00:55:10.795    
00:55:10.796    235 packages are looking for funding
00:55:10.796      run `npm fund` for details
00:55:10.800    
00:55:10.800    4 high severity vulnerabilities
00:55:10.800    
00:55:10.800    To address all issues (including breaking changes), run:
00:55:10.800      npm audit fix --force
00:55:10.800    
00:55:10.801    Run `npm audit` for details.
00:55:10.834    Executing user command: npm run build
00:55:11.448    
00:55:11.448    > portfolio-astro@1.0.0 build
00:55:11.449    > astro build
00:55:11.449    
00:55:13.048    19:55:13 [WARN] [@astrojs/cloudflare] The current configuration does not support image optimization. To allow your project to build with the original, unoptimized images, the image service has been automatically switched to the 'noop' option. See https://docs.astro.build/en/reference/configuration-reference/#imageservice
00:55:13.241    19:55:13 [build] output: "server"
00:55:13.241    19:55:13 [build] directory: /opt/buildhome/repo/dist/
00:55:13.241    19:55:13 [build] adapter: @astrojs/cloudflare
00:55:13.241    19:55:13 [build] Collecting build info...
00:55:13.241    19:55:13 [build] ✓ Completed in 225ms.
00:55:13.243    19:55:13 [build] Building server entrypoints...
00:55:14.785    19:55:14 [vite] ✓ built in 1.51s
00:55:14.785    19:55:14 [build] ✓ Completed in 1.54s.
00:55:14.786    
00:55:14.786     building client (vite) 
00:55:14.793    19:55:14 [vite] transforming...
00:55:15.441    19:55:15 [vite] ✓ 97 modules transformed.
00:55:15.506    19:55:15 [vite] rendering chunks...
00:55:15.542    19:55:15 [vite] computing gzip size...
00:55:15.548    19:55:15 [vite] dist/_astro/index.NEDEFKed.js         6.71 kB │ gzip:  2.67 kB
00:55:15.549    19:55:15 [vite] dist/_astro/components.DSeWAh9S.js   54.34 kB │ gzip: 18.10 kB
00:55:15.549    19:55:15 [vite] dist/_astro/client.DbokQZWz.js      135.49 kB │ gzip: 43.80 kB
00:55:15.549    19:55:15 [vite] ✓ built in 759ms
00:55:15.569    19:55:15 
00:55:15.569     finalizing server assets 
00:55:15.569    
00:55:15.570    19:55:15 [build] Rearranging server assets...
00:55:15.576    19:55:15 [@astrojs/sitemap] `sitemap-index.xml` created at `dist`
00:55:15.578    astro-robots-txt: `robots.txt` is created.
00:55:15.578    
00:55:15.579    
00:55:15.579     AstroCompress processing 
00:55:15.686    (-236 Bytes)    1.45% reduction in //dist/_astro/index.Cy_7o8lB.css
00:55:15.687    ✓ Successfully compressed a total of 1 CSS file for 236 Bytes.
00:55:15.714    (-5.68 KB)  53.09% reduction in //dist/assets/images/better-stack-dark.png
00:55:15.732    (-6.66 KB)  49.64% reduction in //dist/assets/images/better-stack-white.png
00:55:15.753    (-3.11 KB)  53.68% reduction in //dist/assets/images/checkly-logo-light.png
00:55:15.791    (-10.3 KB)  54.81% reduction in //dist/assets/images/clerk.png
00:55:15.811    (-5.44 KB)  54.81% reduction in //dist/assets/images/crowdin-dark.png
00:55:15.823    (-3.99 KB)  49.84% reduction in //dist/assets/images/crowdin-white.png
00:55:15.921    (-14.55 KB) 55.24% reduction in //dist/assets/images/facebook-icon.png
00:55:18.581    19:55:18 [build] Waiting for integration "astro-compress", hook "astro:build:done"...
00:55:32.412    (-17.88 KB) 57.44% reduction in //dist/assets/images/linkedin-icon.png
00:55:32.761    (-196.45 KB)    68.86% reduction in //dist/assets/images/project-fire.png
00:55:34.100    (-222.28 KB)    71.63% reduction in //dist/assets/images/project-maps.png
00:55:35.066    (-6.86 KB)  59.79% reduction in //dist/assets/images/sentry-dark.png
00:55:35.103    (-4.72 KB)  51.78% reduction in //dist/assets/images/sentry-white.png
00:55:35.191    (-13.17 KB) 55.55% reduction in //dist/assets/images/turso.png
00:55:35.276    (-15.88 KB) 56.79% reduction in //dist/assets/images/twitter-icon.png
00:55:35.356    (-14.82 KB) 58.35% reduction in //dist/assets/images/youtube-icon.png
00:55:35.356    ✓ Successfully compressed a total of 15 Image files for 541.79 KB.
00:55:35.421    (-17 Bytes) 27.42% reduction in //dist/$server_build/_noop-middleware.mjs
00:55:35.698    (-30 KB)    50.43% reduction in //dist/$server_build/_worker.mjs
00:55:35.745    (-2.69 KB)  26.60% reduction in //dist/$server_build/manifest_DdhpKru9.mjs
00:55:35.767    (-3.29 KB)  47.12% reduction in //dist/$server_build/renderers.mjs
00:55:36.574    (-1.5 KB)   1.14% reduction in //dist/_astro/client.DbokQZWz.js
00:55:36.823    (-242 Bytes)    3.61% reduction in //dist/_astro/index.NEDEFKed.js
00:55:36.976    (-28.35 KB) 36.10% reduction in //dist/$server_build/chunks/astro_BYAv4N7Z.mjs
00:55:36.979    (-17 Bytes) 13.71% reduction in //dist/$server_build/chunks/generic_MXM9nE68.mjs
00:55:36.982    (-17 Bytes) 13.93% reduction in //dist/$server_build/chunks/index_CRFI37MD.mjs
00:55:36.987    (-404 Bytes)    39.92% reduction in //dist/$server_build/chunks/vnode-children_BkR_XoPb.mjs
00:55:37.005    (-4.32 KB)  46.91% reduction in //dist/$server_build/chunks/astro/assets-service_CA8_NaZ9.mjs
00:55:37.092    (-14.11 KB) 45.11% reduction in //dist/$server_build/chunks/pages/generic_C0Yg18_-.mjs
00:55:37.124    (-5.48 KB)  26.24% reduction in //dist/$server_build/chunks/pages/index_Dmx7aaFF.mjs
00:55:37.124    ✓ Successfully compressed a total of 13 JavaScript files for 90.43 KB.
00:55:37.413    (-2.13 KB)  8.42% reduction in //dist/assets/images/avatar.svg
00:55:37.436    (-3.83 KB)  43.45% reduction in //dist/assets/images/codecov-dark.svg
00:55:37.470    (-3.88 KB)  42.77% reduction in //dist/assets/images/codecov-white.svg
00:55:37.470    ✓ Successfully compressed a total of 3 SVG files for 9.84 KB.
00:55:37.539    19:55:37 [build] Server built in 24.52s
00:55:37.539    19:55:37 [build] Complete!
00:55:37.658    Finished
00:55:37.659    Found _worker.js in output directory. Uploading.
00:55:38.869    ✘ [ERROR] 1 error(s) and 0 warning(s) when compiling Worker.
00:55:38.869    
00:55:38.869    
00:55:38.872    
00:55:38.874    ✘ [ERROR] Could not resolve "node:stream"
00:55:38.874    
00:55:38.875        _worker.js:15:26945:
00:55:38.875          15 │ ...Writable:e}=await import("node:stream");return e}function pc(e)...
00:55:38.875             ╵                             ~~~~~~~~~~~~~
00:55:38.875    
00:55:38.875      The package "node:stream" wasn't found on the file system but is built into node.
00:55:38.875      Add the "nodejs_compat" compatibility flag to your Pages project to enable Node.js compatibility.
00:55:38.875    
00:55:38.875    
00:55:38.876    ✘ [ERROR] Build failed with 1 error:
00:55:38.877    
00:55:38.877      _worker.js:15:26945: ERROR: Could not resolve "node:stream"
00:55:38.877    
00:55:38.877    
00:55:38.887    Failed building Pages Functions.
00:55:39.861    Failed: generating Pages Functions failed. Check the logs above for more information. If this continues for an unknown reason, contact support: https://cfl.re/3WgEyrH

What's the expected result?

Don't know what happend but it is not working

Link to Minimal Reproducible Example

https://github.com/withastro/astro

Participation

faizananwerali commented 3 months ago

Just for information, I removed cloudflare and used node server adaptor for deployment testing.

import node from '@astrojs/node';
  output: "server",
  adapter: node({
    mode: 'standalone',
  }),

deployment was fully successful but obviously it didn't launch the website on cloudflare because i didn't use correct Adaptor. So there must be an issue with @astrojs/cloudflare Adaptor.

Got 404 error.

Screenshot 2024-03-26 at 1 09 54 AM

Looking for the fix in Adaptor or guide how to resolve this issue.

alexanderniebuhr commented 3 months ago

Cloudflare doesn't run on a Node runtime, that's why not everything is compatible.. But streams are compatible if you enable the correct flag, it even does tell you this in the error message: Add the "nodejs_compat" compatibility flag to your Pages project to enable Node.js compatibility.

We do have documentation about this here: https://docs.astro.build/en/guides/integrations-guide/cloudflare/#nodejs-compatibility And Cloudflare also has documentation about it here: https://developers.cloudflare.com/workers/runtime-apis/nodejs#enable-nodejs-with-pages-functions

faizananwerali commented 3 months ago

Appreciate it. For others facing the same issue, follow the documentation. Essentially, you have two options: either create a wrangler.toml file in your project or enable Node.js directly from the Dashboard.

Enable Node.js with Workers

Add the nodejs_compat compatibility flag to your wrangler.toml:

wrangler.toml compatibility_flags = [ "nodejs_compat" ]

Enable Node.js from the Cloudflare dashboard

To enable Node.js for your Pages Function from the Cloudflare dashboard:

  1. Log in to the Cloudflare dashboard and select your account.
  2. Select Workers & Pages and in Overview, select your Pages project.
  3. Select Settings > Functions > Compatibility Flags.
  4. Add the nodejs_compat compatibility flag to your Preview and Production deployments.
irg1008 commented 3 months ago

This still does not work for me. I receive "Buffer is not defined" error evene with compatibility turned on

alexanderniebuhr commented 3 months ago

This still does not work for me. I receive "Buffer is not defined" error evene with compatibility turned on

Please open a new issue with a minimal reproduction.