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

Unable to deploy to Netlify SSR #7920

Closed
1 task
vkbansal opened this issue Aug 2, 2023 · 4 comments
Closed
1 task

Unable to deploy to Netlify SSR #7920

vkbansal opened this issue Aug 2, 2023 · 4 comments
Labels
ecosystem: upstream Upstream package has issue feat: ssr Related to SSR (scope)

Comments

@vkbansal
Copy link

vkbansal commented Aug 2, 2023

What version of astro are you using?

2.9.7

Are you using an SSR adapter? If so, which one?

Netlify

What package manager are you using?

pnpm

What operating system are you using?

MacOS

What browser are you using?

Chrome

Describe the Bug

I have a following configs

// package.json
{
  "dependecies": {
   "@astrojs/netlify": "^2.5.2",
    "@astrojs/preact": "^2.2.1",
    "astro": "^2.9.7",
    "jsonwebtoken": "^9.0.1",
    "preact": "^10.16.0",
    "sass": "^1.64.2",
    "typescript": "^5.1.6"
  }
}
// astro.config.ts
import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify/functions';
import preact from '@astrojs/preact';

export default defineConfig({
	output: 'server',
	adapter: netlify({}),
	integrations: [preact()],
});
# netlify.toml
[build]
  command = "pnpm run build"
  publish = "dist/"

[build.environment]
  NODE_VERSION="18"

I have a middleware, in which I try to validate JWT using jsonwebtoken package.

This is working fine with astro dev.

But when I try deploying this to netlify, I am getting the following error:

> NODE_ENV=production astro build
9:10:16 PM: 03:40:16 PM [content] No content directory found. Skipping type generation.
9:10:16 PM: 03:40:16 PM [build] output target: server
9:10:16 PM: 03:40:16 PM [build] deploy adapter: @astrojs/netlify/functions
9:10:16 PM: 03:40:16 PM [build] Collecting build info...
9:10:16 PM: 03:40:16 PM [build] Completed in 72ms.
9:10:16 PM: 03:40:16 PM [build] Building server entrypoints...
9:10:17 PM: 03:40:17 PM [build] Completed in 1.77s.
9:10:17 PM: 
9:10:17 PM:  building client 
9:10:18 PM: Completed in 0.96s.
9:10:18 PM: 
9:10:18 PM: 
9:10:18 PM:  finalizing server assets 
9:10:18 PM: 03:40:18 PM [build] Rearranging server assets...
9:10:18 PM: ✘ [ERROR] Could not resolve crypto
9:10:18 PM:     node_modules/.pnpm/jsonwebtoken@9.0.1/node_modules/jsonwebtoken/sign.js:6:65:
9:10:18 PM:       6 │ ... KeyObject, createSecretKey, createPrivateKey } = require('crypto')
9:10:18 PM:         ╵                                                              ~~~~~~~~
9:10:18 PM:   The package crypto wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use platform: 'node' to do that, which will remove this error.
9:10:18 PM: ✘ [ERROR] Could not resolve crypto
9:10:18 PM:     node_modules/.pnpm/jsonwebtoken@9.0.1/node_modules/jsonwebtoken/verify.js:9:62:
9:10:18 PM:       9 │ ... {KeyObject, createSecretKey, createPublicKey} = require(crypto);
9:10:18 PM:         ╵                                                             ~~~~~~~~
9:10:18 PM:   The package crypto wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use platform: 'node' to do that, which will remove this error.
9:10:18 PM: ✘ [ERROR] Could not resolve stream
9:10:18 PM:     node_modules/.pnpm/jws@3.2.2/node_modules/jws/lib/verify-stream.js:5:21:
9:10:18 PM:       5 │ var Stream = require('stream');
9:10:18 PM:         ╵                      ~~~~~~~~
9:10:18 PM:   The package stream wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use platform: 'node' to do that, which will remove this error.
9:10:18 PM: ✘ [ERROR] Could not resolve util
9:10:18 PM:     node_modules/.pnpm/jws@3.2.2/node_modules/jws/lib/verify-stream.js:7:19:
9:10:18 PM:       7 │ var util = require('util');
9:10:18 PM:         ╵                    ~~~~~~
9:10:18 PM:   The package util wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use platform: 'node' to do that, which will remove this error.
9:10:18 PM: ✘ [ERROR] Could not resolve stream
9:10:18 PM:     node_modules/.pnpm/jws@3.2.2/node_modules/jws/lib/data-stream.js:3:21:
9:10:18 PM:       3 │ var Stream = require('stream');
9:10:18 PM:         ╵                      ~~~~~~~~
9:10:18 PM:   The package stream wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use platform: 'node' to do that, which will remove this error.
9:10:18 PM: ✘ [ERROR] Could not resolve util
9:10:18 PM:     node_modules/.pnpm/jws@3.2.2/node_modules/jws/lib/data-stream.js:4:19:
9:10:18 PM:       4 │ var util = require('util');
9:10:18 PM:         ╵                    ~~~~~~
9:10:18 PM:   The package util wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use platform: 'node' to do that, which will remove this error.
9:10:18 PM: ✘ [ERROR] Could not resolve crypto
9:10:18 PM:     node_modules/.pnpm/jwa@1.4.1/node_modules/jwa/index.js:3:21:
9:10:18 PM:       3 │ var crypto = require('crypto');
9:10:18 PM:         ╵                      ~~~~~~~~
9:10:18 PM:   The package crypto wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use platform: 'node' to do that, which will remove this error.
9:10:18 PM: ✘ [ERROR] Could not resolve util
9:10:18 PM:     node_modules/.pnpm/jwa@1.4.1/node_modules/jwa/index.js:5:19:
9:10:18 PM:       5 │ var util = require('util');
9:10:18 PM:         ╵                    ~~~~~~
9:10:18 PM:   The package util wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use platform: 'node' to do that, which will remove this error.
9:10:18 PM: ✘ [ERROR] Could not resolve stream
9:10:18 PM:     node_modules/.pnpm/jws@3.2.2/node_modules/jws/lib/sign-stream.js:5:21:
9:10:18 PM:       5 │ var Stream = require('stream');
9:10:18 PM:         ╵                      ~~~~~~~~
9:10:18 PM:   The package stream wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use platform: 'node' to do that, which will remove this error.
9:10:18 PM: ✘ [ERROR] Could not resolve util
9:10:18 PM:     node_modules/.pnpm/jws@3.2.2/node_modules/jws/lib/sign-stream.js:7:19:
9:10:18 PM:       7 │ var util = require('util');
9:10:18 PM:         ╵                    ~~~~~~
9:10:18 PM:   The package util wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use platform: 'node' to do that, which will remove this error.
9:10:19 PM:  error   Could not resolve crypto
9:10:19 PM:   File:
9:10:19 PM:     node_modules/.pnpm/jsonwebtoken@9.0.1/node_modules/jsonwebtoken/sign.js:6:65
9:10:19 PM:   Code:
9:10:19 PM:     5 | const {includes, isBoolean, isInteger, isNumber, isPlainObject, isString, once} = require('lodash')
9:10:19 PM:     > 6 | const { KeyObject, createSecretKey, createPrivateKey } = require('crypto')
9:10:19 PM:         |                                                                 ^
9:10:19 PM:       8 | const SUPPORTED_ALGS = ['RS256', 'RS384', 'RS512', 'ES256', 'ES384', 'ES512', 'HS256', 'HS384', 'HS512', 'none'];
9:10:19 PM:       9 | if (PS_SUPPORTED) {
9:10:19 PM:   Stacktrace:
9:10:19 PM: Error: Build failed with 10 errors:
9:10:19 PM: node_modules/.pnpm/jsonwebtoken@9.0.1/node_modules/jsonwebtoken/sign.js:6:65: ERROR: Could not resolve crypto
9:10:19 PM: node_modules/.pnpm/jsonwebtoken@9.0.1/node_modules/jsonwebtoken/verify.js:9:62: ERROR: Could not resolve crypto
9:10:19 PM: node_modules/.pnpm/jwa@1.4.1/node_modules/jwa/index.js:3:21: ERROR: Could not resolve crypto
9:10:19 PM: node_modules/.pnpm/jwa@1.4.1/node_modules/jwa/index.js:5:19: ERROR: Could not resolve util
9:10:19 PM: node_modules/.pnpm/jws@3.2.2/node_modules/jws/lib/data-stream.js:3:21: ERROR: Could not resolve stream
9:10:19 PM: ...
9:10:19 PM:     at failureErrorWithLog (/opt/build/repo/node_modules/.pnpm/esbuild@0.15.18/node_modules/esbuild/lib/main.js:1575:15)
9:10:19 PM:     at /opt/build/repo/node_modules/.pnpm/esbuild@0.15.18/node_modules/esbuild/lib/main.js:1033:28
9:10:19 PM:     at /opt/build/repo/node_modules/.pnpm/esbuild@0.15.18/node_modules/esbuild/lib/main.js:978:67
9:10:19 PM:     at buildResponseToResult (/opt/build/repo/node_modules/.pnpm/esbuild@0.15.18/node_modules/esbuild/lib/main.js:1031:7)
9:10:19 PM:     at /opt/build/repo/node_modules/.pnpm/esbuild@0.15.18/node_modules/esbuild/lib/main.js:1143:14
9:10:19 PM:     at responseCallbacks.<computed> (/opt/build/repo/node_modules/.pnpm/esbuild@0.15.18/node_modules/esbuild/lib/main.js:680:9)
9:10:19 PM:     at handleIncomingPacket (/opt/build/repo/node_modules/.pnpm/esbuild@0.15.18/node_modules/esbuild/lib/main.js:735:9)
9:10:19 PM:     at Socket.readFromStdout (/opt/build/repo/node_modules/.pnpm/esbuild@0.15.18/node_modules/esbuild/lib/main.js:656:7)
9:10:19 PM:     at Socket.emit (node:events:514:28)
9:10:19 PM:     at addChunk (node:internal/streams/readable:324:12)
9:10:19 PM:     at readableAddChunk (node:internal/streams/readable:297:9)
9:10:19 PM:     at Readable.push (node:internal/streams/readable:234:10)
9:10:19 PM:     at Pipe.onStreamRead (node:internal/stream_base_commons:190:23)
9:10:19 PM:  ELIFECYCLE  Command failed with exit code 1. (https://ntl.fyi/exit-code-1)
9:10:19 PM: ​
9:10:19 PM: build.command failed                                        
9:10:19 PM: ────────────────────────────────────────────────────────────────
9:10:19 PM: ​
9:10:19 PM:   Error message
9:10:19 PM:   Command failed with exit code 1: pnpm run build (https://ntl.fyi/exit-code-1)
9:10:19 PM: ​
9:10:19 PM:   Error location
9:10:19 PM:   In build.command from netlify.toml:
9:10:19 PM:   pnpm run build

What's the expected result?

I should be able to deploy to Netlify.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-rawcyg?file=astro.config.mjs&on=stackblitz

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Aug 2, 2023
@natemoo-re natemoo-re added feat: ssr Related to SSR (scope) pkg: netlify labels Aug 2, 2023
@natemoo-re
Copy link
Member

This is a bundling issue. jsonwebtoken seems to rely on the node built-in crypto package, so it can't be bundled. Try the following:

// astro.config.mjs
defineConfig({
  vite: {
    ssr: {
      external: ['jsonwebtoken']
    }
  }
})

You may need some additional configuration to exclude jsonwebtoken from the Netlify bundle as well? But it should work automatically for a Serverless deployment.

@natemoo-re natemoo-re added needs response Issue needs response from OP and removed needs triage Issue needs to be triaged labels Aug 3, 2023
@vkbansal
Copy link
Author

vkbansal commented Aug 4, 2023

Tried it with

// astro.config.mjs
defineConfig({
  vite: {
    ssr: {
      external: ['jsonwebtoken', 'bcrypt'],
    }
  }
})

Still facing same issue

Also tried with

#netlify.toml
[functions]
	external_node_modules = ["jsonwebtoken", "bcrypt"]

still no luck.

@reaganchisholm
Copy link

TL;DR: I recommend swapping out jsonwebtoken to the jose package to avoid headaches


"Could not resolve crypto" error seems to be during the Astro build step, by adding this to package.json, it seemed to resolve the error for me.

"browser": {
    "crypto": false,
    "stream": false,
    "util": false
},

At this point, you can technically npm run build and netlify deploy --prod and it seems to work. But if you try to build via Netlify cli or platform, you'll run into the following error:

Bundling of edge function failed
TypeError: util.inherits is not a function
at node_modules/jws/lib/data-stream.js (file:///opt/build/repo/.netlify/edge-functions/edgeMiddleware.js:2029:10)

It seems we may have discovered a bug with the Netlify adapter that by default when you have a middleware.js file in Astro, it creates an additional edge function middleware file with all the bundled code even though you may not have opted into using edge functions. I've opened an issue to see if the behavior is a bug or intended (#7971). The docs make it seem like its not intended.

And because jsonwebtoken doesn't seem to work in edge environments, we get the error above, so I tried swapping to the jose package instead. The APIs are a bit different but with jose you don't need any of the extra stuff in astro.config.mjs or package.json, it works on both edge and non-edge environments so it should give you less headaches and more flexibility.

But as mentioned above, you can also manually build and remove the edge functions folder and deploy to Netlify and it should work as expected with jsonwebtoken

Hope that helps!

@natemoo-re natemoo-re added ecosystem: upstream Upstream package has issue and removed needs response Issue needs response from OP labels Aug 7, 2023
@natemoo-re
Copy link
Member

Thanks for the detailed response @reaganchisholm! Seems like jsonwebtoken is a generally incompatible package and jose has better support.

Unfortunately, Astro can't support every kind of package in every kind of environment, so I'm going to close this issue and recommend opening an issue with jsonwebtoken directly. I definitely think #7971 is a valid issue, so we can continue that convo there.

@natemoo-re natemoo-re closed this as not planned Won't fix, can't repro, duplicate, stale Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ecosystem: upstream Upstream package has issue feat: ssr Related to SSR (scope)
Projects
None yet
Development

No branches or pull requests

3 participants