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

Cloudflare Worker adapter builds fail with sk-auth #42

Open
dcaslin opened this issue Jul 18, 2021 · 6 comments · May be fixed by #78
Open

Cloudflare Worker adapter builds fail with sk-auth #42

dcaslin opened this issue Jul 18, 2021 · 6 comments · May be fixed by #78
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@dcaslin
Copy link

dcaslin commented Jul 18, 2021

I have a simple sveltekit app that works with sk-auth locally, and works with the node adapter build, but fails with the logs below when I try to use the Cloudflare worker adapter. I've tried playing around w/ the Vite excludes settings but haven't had any luck yet.

Here's my svelite.config.js

import preprocess from 'svelte-preprocess';
import cloudflare from '@sveltejs/adapter-cloudflare-workers';
// import node from '@sveltejs/adapter-node';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	// Consult https://github.com/sveltejs/svelte-preprocess
	// for more information about preprocessors
	preprocess: [preprocess({
        "postcss": true
    })],

	kit: {
		adapter: cloudflare(),
		// adapter: node(),
		// hydrate the <div id="svelte"> element in src/app.html
		target: '#svelte'
	}
};

export default config;
// Workaround until SvelteKit uses Vite 2.3.8 (and it's confirmed to fix the Tailwind JIT problem)
const mode = process.env.NODE_ENV;
const dev = mode === "development";
process.env.TAILWIND_MODE = dev ? "watch" : "build";

And here's my package.json


{
	"name": "~TODO~",
	"version": "0.0.1",
	"scripts": {
		"dev": "svelte-kit dev",
		"build": "svelte-kit build",
		"preview": "svelte-kit preview",
		"check": "svelte-check --tsconfig ./tsconfig.json",
		"check:watch": "svelte-check --tsconfig ./tsconfig.json --watch",
		"lint": "prettier --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
		"format": "prettier --write --plugin-search-dir=. ."
	},
	"devDependencies": {
		"@sveltejs/adapter-cloudflare-workers": "^1.0.0-next.16",
		"@sveltejs/adapter-node": "^1.0.0-next.34",
		"@sveltejs/adapter-static": "^1.0.0-next.13",
		"@sveltejs/kit": "^1.0.0-next.131",
		"@tailwindcss/custom-forms": "^0.2.1",
		"@typescript-eslint/eslint-plugin": "^4.28.3",
		"@typescript-eslint/parser": "^4.28.3",
		"autoprefixer": "^10.3.1",
		"cssnano": "^5.0.6",
		"eslint": "^7.31.0",
		"eslint-config-prettier": "^8.1.0",
		"eslint-plugin-svelte3": "^3.2.0",
		"postcss": "^8.3.5",
		"postcss-load-config": "^3.1.0",
		"prettier": "~2.2.1",
		"prettier-plugin-svelte": "^2.2.0",
		"svelte": "^3.34.0",
		"svelte-check": "^2.2.3",
		"svelte-preprocess": "^4.7.4",
		"tailwindcss": "^2.2.4",
		"tslib": "^2.0.0",
		"typescript": "^4.3.5"
	},
	"type": "module",
	"dependencies": {
		"@tailwindcss/forms": "^0.3.3",
		"@tailwindcss/typography": "^0.4.1",
		"faker": "^5.5.3",
		"sk-auth": "^0.3.7"
	}
}

Error log below:

> Using @sveltejs/adapter-cloudflare-workers
 > node_modules/jws/lib/verify-stream.js:5:21: error: Could not resolve "stream" (use "platform: 'node'" when building for node)
    5 │ var Stream = require('stream');
      ╵                      ~~~~~~~~

 > node_modules/jws/lib/verify-stream.js:7:19: error: Could not resolve "util" (use "platform: 'node'" when building for node)
    7 │ var util = require('util');
      ╵                    ~~~~~~

 > node_modules/jws/lib/sign-stream.js:5:21: error: Could not resolve "stream" (use "platform: 'node'" when building for node)
    5 │ var Stream = require('stream');
      ╵                      ~~~~~~~~

 > node_modules/jws/lib/tostring.js:2:21: error: Could not resolve "buffer" (use "platform: 'node'" when building for node)
    2 │ var Buffer = require('buffer').Buffer;
      ╵                      ~~~~~~~~

 > node_modules/safe-buffer/index.js:3:21: error: Could not resolve "buffer" (use "platform: 'node'" when building for node)
    3 │ var buffer = require('buffer')
      ╵                      ~~~~~~~~

 > node_modules/jws/lib/data-stream.js:3:21: error: Could not resolve "stream" (use "platform: 'node'" when building for node)
    3 │ var Stream = require('stream');
      ╵                      ~~~~~~~~

 > node_modules/jws/lib/sign-stream.js:7:19: error: Could not resolve "util" (use "platform: 'node'" when building for node)
    7 │ var util = require('util');
      ╵                    ~~~~~~

 > node_modules/jwa/index.js:3:21: error: Could not resolve "crypto" (use "platform: 'node'" when building for node)
    3 │ var crypto = require('crypto');
      ╵                      ~~~~~~~~

 > node_modules/jws/lib/data-stream.js:4:19: error: Could not resolve "util" (use "platform: 'node'" when building for node)
    4 │ var util = require('util');
      ╵                    ~~~~~~

 > node_modules/jwa/index.js:5:19: error: Could not resolve "util" (use "platform: 'node'" when building for node)
    5 │ var util = require('util');
      ╵                    ~~~~~~

 > node_modules/buffer-equal-constant-time/index.js:3:21: error: Could not resolve "buffer" (use "platform: 'node'" when building for node)
    3 │ var Buffer = require('buffer').Buffer; // browserify
      ╵                      ~~~~~~~~

> Build failed with 11 errors:
node_modules/buffer-equal-constant-time/index.js:3:21: error: Could not resolve "buffer" (use "platform: 'node'" when building for node)
node_modules/jwa/index.js:3:21: error: Could not resolve "crypto" (use "platform: 'node'" when building for node)
node_modules/jwa/index.js:5:19: error: Could not resolve "util" (use "platform: 'node'" when building for node)
node_modules/jws/lib/data-stream.js:3:21: error: Could not resolve "stream" (use "platform: 'node'" when building for node)
node_modules/jws/lib/data-stream.js:4:19: error: Could not resolve "util" (use "platform: 'node'" when building for node)
...
Error: Build failed with 11 errors:
node_modules/buffer-equal-constant-time/index.js:3:21: error: Could not resolve "buffer" (use "platform: 'node'" when building for node)
node_modules/jwa/index.js:3:21: error: Could not resolve "crypto" (use "platform: 'node'" when building for node)
node_modules/jwa/index.js:5:19: error: Could not resolve "util" (use "platform: 'node'" when building for node)
node_modules/jws/lib/data-stream.js:3:21: error: Could not resolve "stream" (use "platform: 'node'" when building for node)
node_modules/jws/lib/data-stream.js:4:19: error: Could not resolve "util" (use "platform: 'node'" when building for node)
...
    at failureErrorWithLog (/Users/dcaslin/projects/531-calc-2/node_modules/esbuild/lib/main.js:1449:15)
    at /Users/dcaslin/projects/531-calc-2/node_modules/esbuild/lib/main.js:1131:28
    at runOnEndCallbacks (/Users/dcaslin/projects/531-calc-2/node_modules/esbuild/lib/main.js:1049:65)
    at buildResponseToResult (/Users/dcaslin/projects/531-calc-2/node_modules/esbuild/lib/main.js:1129:7)
    at /Users/dcaslin/projects/531-calc-2/node_modules/esbuild/lib/main.js:1236:14
    at /Users/dcaslin/projects/531-calc-2/node_modules/esbuild/lib/main.js:609:9
    at handleIncomingPacket (/Users/dcaslin/projects/531-calc-2/node_modules/esbuild/lib/main.js:706:9)
    at Socket.readFromStdout (/Users/dcaslin/projects/531-calc-2/node_modules/esbuild/lib/main.js:576:7)
    at Socket.emit (events.js:315:20)
    at Socket.EventEmitter.emit (domain.js:467:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `svelte-kit build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

@dcaslin
Copy link
Author

dcaslin commented Jul 18, 2021

Well this is interesting. I had an older project I setup to test all this and I jumped back to it and, good news, it builds, but it then fails to publish to CF.

package.json of building (but not publishing) project:

{
	"name": "~TODO~",
	"version": "0.0.1",
	"scripts": {
		"dev": "svelte-kit dev",
		"build": "svelte-kit build",
		"preview": "svelte-kit preview",
		"publish": "svelte-kit build && wrangler publish",
		"check": "svelte-check --tsconfig ./tsconfig.json",
		"check:watch": "svelte-check --tsconfig ./tsconfig.json --watch",
		"lint": "prettier --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
		"format": "prettier --write --plugin-search-dir=. ."
	},
	"devDependencies": {
		"@sveltejs/adapter-cloudflare-workers": "^1.0.0-next.12",
		"@sveltejs/kit": "next",
		"@typescript-eslint/eslint-plugin": "^4.19.0",
		"@typescript-eslint/parser": "^4.19.0",
		"eslint": "^7.22.0",
		"eslint-config-prettier": "^8.1.0",
		"eslint-plugin-svelte3": "^3.2.0",
		"prettier": "~2.2.1",
		"prettier-plugin-svelte": "^2.2.0",
		"svelte": "^3.38.2",
		"svelte-check": "^2.0.0",
		"svelte-preprocess": "^4.7.3",
		"tslib": "^2.0.0",
		"typescript": "^4.0.0",
		"postcss": "^8.3.5",
		"postcss-load-config": "^3.1.0",
		"autoprefixer": "^10.2.6",
		"cssnano": "^5.0.6",
		"tailwindcss": "^2.2.4"
	},
	"type": "module",
	"dependencies": {
		"apexcharts": "^3.27.1",
		"faker": "^5.5.3",
		"sk-auth": "^0.3.7",
		"sveltestrap": "^5.2.1"
	}
}

Wrangler publish error:

Error: Something went wrong! Status: 400 Bad Request, Details {
  "result": null,
  "success": false,
  "errors": [
    {
      "code": 10021,
      "message": "Uncaught ReferenceError: require is not defined\n  at line 140572 in node_modules/safe-buffer/index.js\n  at line 9 in __require\n  at line 141103 in node_modules/jws/lib/sign-stream.js\n  at line 9 in __require\n  at line 141283 in node_modules/jws/index.js\n  at line 
9 in __require\n  at line 141316 in node_modules/jsonwebtoken/decode.js\n  at line 9 in __require\n  at line 143373 in node_modules/jsonwebtoken/index.js\n  at line 9 in __require\n"
    }
  ],
  "messages": []
}



@dcaslin
Copy link
Author

dcaslin commented Jul 19, 2021

Btw, if this means sk-auth + CF workers isn't a good fit yet, that's fair. (And if so, What adapter would you recommend as the most stable choice instead?)

@Dan6erbond
Copy link
Owner

Hey there! Thanks for opening this issue!

That's interesting. I'm not really well versed in CF Workers and how they work, but it seems they're a serverless platform so the way entrypoints work might be different from having a constantly running Node app, and assets might get optimized differently by Vite, which would explain why it's complaining about require().

The goal is for SKAuth to work on all deploy targets, except in SPA mode or static builds of course, and I can imagine this being an issue for Vercel, Netlify, etc, too, so I'll be looking into it once I get a chance! First I'll have to figure out how to isolate and reproduce the issue, though.

Given that, I think there is a chance, that at the moment only the Node adapter will work properly. I'll look into whether this is an issue with certain dependencies, and if we can get away with switching them out for alternatives, or if a more robust solution is needed.

@Dan6erbond Dan6erbond self-assigned this Jul 19, 2021
@Dan6erbond Dan6erbond added bug Something isn't working help wanted Extra attention is needed labels Jul 19, 2021
@dcaslin
Copy link
Author

dcaslin commented Jul 24, 2021

Thanks! Based on your note here and advice in the SvelteKit discord I've moved my little hobby project over to the node adapter. I haven't gotten a chance to deploy yet, but dev wise this plugin is working great, thx for your work!

@ohmree
Copy link
Contributor

ohmree commented Feb 27, 2022

This seems to be due to a single package - jsonwebtoken.
Is my observation correct?

If so, can it not be replaced with jose, which explicitly supports cloudflare workers along with a bunch of other runtimes?

@hmnd hmnd linked a pull request Mar 17, 2022 that will close this issue
@imawaki
Copy link

imawaki commented Jul 28, 2022

I'm getting this build error on vercel. (Local environment is working.)

[commonjs--resolver] Failed to resolve entry for package "sk-auth". The package may have incorrect main/module/exports specified in its package.json.
--
16:14:37.092 | [vite-plugin-svelte-kit] Failed to resolve entry for package "sk-auth". The package may have incorrect main/module/exports specified in its package.json.
16:14:37.096 | error during build:
16:14:37.096 | Error: Failed to resolve entry for package "sk-auth". The package may have incorrect main/module/exports specified in its package.json.
16:14:37.096 | at packageEntryFailure (file:https:///vercel/path0/node_modules/vite/dist/node/chunks/dep-c6273c7a.js:34410:11)
16:14:37.096 | at resolvePackageEntry (file:https:///vercel/path0/node_modules/vite/dist/node/chunks/dep-c6273c7a.js:34407:5)
16:14:37.096 | at tryNodeResolve (file:https:///vercel/path0/node_modules/vite/dist/node/chunks/dep-c6273c7a.js:34161:20)
16:14:37.097 | at Object.resolveId (file:https:///vercel/path0/node_modules/vite/dist/node/chunks/dep-c6273c7a.js:33963:28)
16:14:37.097 | at Object.<anonymous> (file:https:///vercel/path0/node_modules/vite/dist/node/chunks/dep-c6273c7a.js:43670:19)
16:14:37.097 | at file:https:///vercel/path0/node_modules/rollup/dist/es/shared/rollup.js:22695:37
16:14:37.123 | error Command failed with exit code 1.

Is the root cause same as this?
here's my svelte.conf.js and vite.conf.js.

// svelte.conf.js
import adapter from '@sveltejs/adapter-auto';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	kit: {
		adapter: adapter()
	}
};

export default config;
//vite.conf.js
import { sveltekit } from '@sveltejs/kit/vite';
import { resolve } from 'path';

/** @type {import('vite').UserConfig} */
const config = {
  plugins: [sveltekit()],
  ssr: {noExternal: ['chart.js']},
  resolve: {alias: {
    $src: resolve('./src'),
    $components: resolve('./src/components'),
  }}
};

export default config;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants