anse icon indicating copy to clipboard operation
anse copied to clipboard

Cloudflare Pages Deployment Issue - Uncaught ReferenceError: window is not defined

Open jwjoel opened this issue 2 years ago • 3 comments

How is Anse deployed?

Others (Specify in description)

Describe the bug

Issue: I'm trying to deploy the project using Cloudflare Pages, and I have installed @astrojs/cloudflare ^6.2.4 and made the following changes to the astro.config.mjs file:

const envAdapter = () => {
  if (process.env.OUTPUT === 'vercel') {
    return vercel()
  } else if (process.env.OUTPUT === 'netlify') {
    return netlify()
  } else if (process.env.OUTPUT === 'cloudflare') {
    return cloudflare()
  } else {
    return node({
      mode: 'standalone',
    })
  }
}

However, the deployment is failing with the following error message: Error: Failed to publish your Function. Got error: Uncaught ReferenceError: window is not defined at functionsWorker-0.8414097395681288.js:2597:8.

It seems that the issue is caused by the use of a window hook in the server-side context, and I've tried a few ways to fix it but failed. Any ideas?

Console Logs

2023-05-12T02:16:22.614053Z Cloning repository... 2023-05-12T02:16:23.439496Z From https://github.com/jwjoel/anse 2023-05-12T02:16:23.44015Z * branch 3f8e873060e2be528334044a32b75a708e06ad68 -> FETCH_HEAD 2023-05-12T02:16:23.440343Z 2023-05-12T02:16:23.517256Z HEAD is now at 3f8e873 fix: issues 2023-05-12T02:16:23.517877Z 2023-05-12T02:16:23.62398Z 2023-05-12T02:16:23.652002Z Success: Finished cloning repository files 2023-05-12T02:16:24.231949Z Installing dependencies 2023-05-12T02:16:24.239642Z Python version set to 2.7 2023-05-12T02:16:27.179096Z Downloading and installing node v20.1.0... 2023-05-12T02:16:27.43792Z Downloading https://nodejs.org/dist/v20.1.0/node-v20.1.0-linux-x64.tar.xz... 2023-05-12T02:16:27.769552Z Computing checksum with sha256sum 2023-05-12T02:16:27.907887Z Checksums matched! 2023-05-12T02:16:33.070072Z Now using node v20.1.0 (npm v9.6.4) 2023-05-12T02:16:33.308799Z Started restoring cached build plugins 2023-05-12T02:16:33.317739Z Finished restoring cached build plugins 2023-05-12T02:16:33.6865Z Attempting ruby version 2.7.1, read from environment 2023-05-12T02:16:35.09395Z Using /opt/buildhome/.rvm/gems/ruby-2.7.1 2023-05-12T02:16:36.017782Z Using PHP version 5.6 2023-05-12T02:16:36.103867Z 5.2.5 is already installed. 2023-05-12T02:16:36.119458Z Using Swift version 5.2.5 2023-05-12T02:16:36.119871Z Started restoring cached node modules 2023-05-12T02:16:36.130823Z Finished restoring cached node modules 2023-05-12T02:16:36.76132Z Installing NPM modules using NPM version 9.6.4 2023-05-12T02:16:43.066541Z npm WARN deprecated [email protected]: Please use @jridgewell/sourcemap-codec instead 2023-05-12T02:16:47.11671Z npm WARN deprecated [email protected]: This package has been deprecated and is no longer maintained. Please use @rollup/plugin-terser 2023-05-12T02:16:53.511306Z 2023-05-12T02:16:53.511834Z > [email protected] postinstall 2023-05-12T02:16:53.512012Z > npx simple-git-hooks 2023-05-12T02:16:53.512168Z 2023-05-12T02:16:54.275686Z [INFO] Successfully set the pre-commit with command: pnpm lint-staged 2023-05-12T02:16:54.278024Z [INFO] Successfully set all git hooks 2023-05-12T02:16:54.34621Z 2023-05-12T02:16:54.346508Z added 1479 packages, and audited 1480 packages in 16s 2023-05-12T02:16:54.346711Z 2023-05-12T02:16:54.346964Z 378 packages are looking for funding 2023-05-12T02:16:54.347116Z run npm fund for details 2023-05-12T02:16:54.355073Z 2023-05-12T02:16:54.355297Z 7 moderate severity vulnerabilities 2023-05-12T02:16:54.355487Z 2023-05-12T02:16:54.355666Z To address all issues (including breaking changes), run: 2023-05-12T02:16:54.355825Z npm audit fix --force 2023-05-12T02:16:54.355981Z 2023-05-12T02:16:54.356132Z Run npm audit for details. 2023-05-12T02:16:54.374587Z NPM modules installed 2023-05-12T02:16:54.436024Z Installing Hugo 0.54.0 2023-05-12T02:16:55.096087Z Hugo Static Site Generator v0.54.0-B1A82C61A/extended linux/amd64 BuildDate: 2019-02-01T10:04:38Z 2023-05-12T02:16:55.098589Z Started restoring cached go cache 2023-05-12T02:16:55.111859Z Finished restoring cached go cache 2023-05-12T02:16:55.196544Z go version go1.14.4 linux/amd64 2023-05-12T02:16:55.209089Z go version go1.14.4 linux/amd64 2023-05-12T02:16:55.212555Z Installing missing commands 2023-05-12T02:16:55.212762Z Verify run directory 2023-05-12T02:16:55.212901Z Executing user command: npm run build 2023-05-12T02:16:55.853778Z 2023-05-12T02:16:55.854176Z > [email protected] build 2023-05-12T02:16:55.854742Z > astro build 2023-05-12T02:16:55.855233Z 2023-05-12T02:16:58.132196Z 02:16:58 AM [content] No content directory found. Skipping type generation. 2023-05-12T02:16:58.132985Z 02:16:58 AM [build] output target: server 2023-05-12T02:16:58.133353Z 02:16:58 AM [build] deploy adapter: @astrojs/cloudflare 2023-05-12T02:16:58.133674Z 02:16:58 AM [build] Collecting build info... 2023-05-12T02:16:58.133921Z 02:16:58 AM [build] Completed in 495ms. 2023-05-12T02:16:58.134749Z 02:16:58 AM [build] Building server entrypoints... 2023-05-12T02:17:01.173419Z 02:17:01 AM [build] Completed in 3.04s. 2023-05-12T02:17:01.174281Z 2023-05-12T02:17:01.174492Z building client 2023-05-12T02:17:10.523885Z 2023-05-12T02:17:10.524362Z (!) Some chunks are larger than 500 kBs after minification. Consider: 2023-05-12T02:17:10.524555Z - Using dynamic import() to code-split the application 2023-05-12T02:17:10.524718Z - Use build.rollupOptions.output.manualChunks to improve chunking: https://rollupjs.org/configuration-options/#output-manualchunks 2023-05-12T02:17:10.524838Z - Adjust chunk size limit for this warning via build.chunkSizeWarningLimit. 2023-05-12T02:17:12.472355Z 2023-05-12T02:17:12.472904Z PWA v0.14.7 2023-05-12T02:17:12.473082Z mode generateSW 2023-05-12T02:17:12.47323Z precache 28 entries (1339.00 KiB) 2023-05-12T02:17:12.473375Z files generated 2023-05-12T02:17:12.47352Z dist/sw.js 2023-05-12T02:17:12.473671Z dist/workbox-3625d7b0.js 2023-05-12T02:17:12.473816Z Completed in 11.29s. 2023-05-12T02:17:12.473946Z 2023-05-12T02:17:12.482333Z 2023-05-12T02:17:12.482573Z finalizing server assets 2023-05-12T02:17:12.48274Z 2023-05-12T02:17:12.482883Z 02:17:12 AM [build] Rearranging server assets... 2023-05-12T02:17:12.571858Z 02:17:12 AM [build] Server built in 14.93s 2023-05-12T02:17:12.572146Z 02:17:12 AM [build] Complete! 2023-05-12T02:17:12.611285Z Finished 2023-05-12T02:17:12.611797Z Found _worker.js in output directory. Uploading. 2023-05-12T02:17:13.275882Z ✨ Compiled Worker successfully 2023-05-12T02:17:13.350176Z Found _routes.json in output directory. Uploading. 2023-05-12T02:17:13.366708Z Validating asset output directory 2023-05-12T02:17:13.954359Z Deploying your site to Cloudflare's global network... 2023-05-12T02:17:16.734752Z Uploading... (89/89) 2023-05-12T02:17:16.735493Z ✨ Success! Uploaded 0 files (89 already uploaded) (0.39 sec) 2023-05-12T02:17:16.735699Z 2023-05-12T02:17:17.220644Z ✨ Upload complete! 2023-05-12T02:17:18.850346Z Success: Assets published! 2023-05-12T02:17:19.791748Z Error: Failed to publish your Function. Got error: Uncaught ReferenceError: window is not defined at functionsWorker-0.8414097395681288.js:2597:8

Participation

  • [ ] I am willing to submit a pull request for this issue.

jwjoel avatar May 12 '23 02:05 jwjoel

Cloudflare Pages seem not to support Node18.

yzh990918 avatar May 12 '23 04:05 yzh990918

Cloudflare Pages seem not to support Node18.

Thanks for getting back. In order to use a specific version of Node, you'll need to set the environment variable NODE_VERSION to the desired version number.

jwjoel avatar May 12 '23 11:05 jwjoel

I have managed to successfully deploy Anse on Cloudflare Pages. I modified the astro.config.mjs file accordingly as you did. I also ensured that the "OUTPUT" environment variable is set to "cloudflare" and the "NODE_VERSION" is set to "18.16.0".

It seems necessary to use the version 1 build system. I attempted to use version 2, which has a higher default node version, but it resulted in failure. The errors I encountered were different from the ones you experienced.

conql avatar Jul 07 '23 19:07 conql