pixelmund / svelte-kit-cookie-session

⚒️ Encrypted "stateless" cookie sessions for SvelteKit
MIT License
182 stars 11 forks source link

Vite error in make-crypter.js - Vite seems to think "crypto" is a local module #15

Closed rmunn closed 3 years ago

rmunn commented 3 years ago

Just updated svelte-kit-cookie-session from 1.2.4 to 1.3.0, and now I'm getting the following Vite error whenever I run pnpm run dev:

1:10:31 PM [vite] new dependencies found: svelte-kit-cookie-session, updating...
 > node_modules/svelte-kit-cookie-session/dist/utils/crypto/make-crypter.js:9:75: error: Could not read from file: (REPOROOT)/crypto
    9 │ import { pbkdf2Sync, createDecipheriv, createCipheriv, randomBytes, } from "crypto";
      ╵                                                                            ~~~~~~~~

(I replaced my repo root with the text (REPOROOT) above since I'm running this from a directory nested about five deep in my folder hierarchy, and without that replacement the error message was even more annoyingly long than it already is.)

Running with npm instead of pnpm I get the same error. However, if I run pnpm run build followed by pnpm run preview then everything works.

I know I need to somehow tell Vite that svelte-kit-cookie-session is only Node-based and should therefore be allowed to import Node packages like crypto, but I'm not yet familiar enough with Svelte-Kit to know how to do that off the top of my head. Which leads me to report it here, because you might want to add something to the svelte-kit-cookie-session README about how to work around this Vite behavior. (And I'll update this issue if I figure it out.)

pixelmund commented 3 years ago

See #14

rmunn commented 3 years ago

If I move svelte-kit-cookie-session into devDependencies then the error message changes:

1:22:41 PM [vite] Error when evaluating SSR module (REPOROOT)/src/hooks:
Error: Cannot find module 'svelte-kit-cookie-session' from '(REPOROOT)/src'

And then pnpm run build works but pnpm run preview does not:

> (PROJECTNAME)@0.0.1 preview (REPOROOT)
> svelte-kit preview

> Cannot find package '(REPOROOT)/node_modules/svelte-kit-cookie-session/' imported from (REPOROOT)/.svelte-kit/output/server/app.js

This is all with the latest Svelte-Kit and Vite, BTW.

pixelmund commented 3 years ago

I haven't quite figured out which vite config is helpful here. Would be happy to include something in the README.

rmunn commented 3 years ago

Saw your reference to https://github.com/vitejs/vite/issues/728 which does look like exactly the issue... but that issue is closed and locked, so I bet the Vite developers aren't tracking it. I wonder if I should open a new issue in Vite to report this? I bet we could boil this down to a VERY simple reproduction with no Svelte-Kit in the mix, so that the Vite devs don't end up thinking the problem is in Svelte-Kit.

Hmm. I'll hold off on opening a Vite issue until I have a Vite-only repro, I think. But it's definitely something VIte-side that's causing this.

pixelmund commented 3 years ago

This seems definetly like a Vite issue, i'm trying around with the vite config to see if i can solve this issue that way.

pixelmund commented 3 years ago

@rmunn @chbert I found 2 different solution, this is the easiest one which should work..

const config = {
    kit: {
        vite: {
            optimizeDeps: {
                exclude: ['svelte-kit-cookie-session'],
            },
        },
    },
};
rmunn commented 3 years ago

@pixelmund That doesn't work for me. I also tried optimizeDeps: { esbuildOptions: { platform: 'neutral' } } without checking dev and I'm still getting "error: Could not read from file: (REPOROOT)/crypto". The app does run and correctly decrypts and encrypts session cookies, but esbuildOptions: { platform: 'neutral' } isn't working.

I also tried this:

optimizeDeps: {
    allowNodeBuiltins: ['crypto'],
    esbuildOptions: { platform: 'neutral' }
}

But that didn't change anything; I'm still getting the same "Could not read from file" error.

pixelmund commented 3 years ago

So did you try the exclude one? I can't reproduce it anymore, even after deleting node_modules the exclude did the trick..

rmunn commented 3 years ago

Just tried exclude and it worked -- before seeing your comment edit above. :-)

optimizeDeps: {
    exclude: ['svelte-kit-cookie-session'],
}

That was enough to get rid of the Vite error.

pixelmund commented 3 years ago

Perfect, i'm adding it to the readme. Thanks for verifying :-)

rmunn commented 3 years ago

Note that svelte-kit-cookie-session does need to be in dependencies, not devDependencies, or I still get "Error: Cannot find module 'svelte-kit-cookie-session' from '(REPOROOT)/src'".

rmunn commented 3 years ago

Perfect, i'm adding it to the readme. Thanks for verifying :-)

Great; I'll close the issue once the README is updated, or you can close it if I don't get to it. (I'm in a far eastern timezone, UTC+lots, so I go to sleep while most of America is just waking up).

pixelmund commented 3 years ago

I have updated the README and going to close this issue now. I'm on UTC+1