-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Issues with ESM module import syntax and endless loop during load in SSR #3273
Comments
Vite should treat In the meantime you can manually mark it as SSR external. export default {
ssr: {
external: ['@small-tech/jsdb']
},
plugins: [vue(), ssr()]
} I suspect the problem to be around the fact that @patak-js How about we treat all dependencies as SSR external? I don't see the benefit of processing Node.js dependencies. |
@brillout Thanks, that got me a little further. But now I have the following error (my project is the vite-plugin-ssr vue starter template but with
The JSDB index.js mentioned in the error contains: export { default } from './lib/JSDB.js' |
Doesn't |
@brillout No, it’s a pure ESM module optimised for ESM-only Node projects, it doesn’t support CJS. I can see the problem if Vite transpiles all code to CJS. (JSDB also saves its database tables in ESM format. There is the 1.x branch which is CJS and saves its tables in UMD/CJS format so there’s no reason to have a CJS build of it.) Do you know if ESM-only node modules will be supported at some time or is this a fact of life for the foreseeable future? PS. I did read the following on the link you provided earlier:
And also:
https://github.com/vitejs/vite/blob/main/packages/vite/src/node/ssr/ssrExternal.ts#L11 |
Yes and currently transpiled Node.js code is evaluated by using I guess we can close this ticket or replace it with a more precice one. |
OK, I’m going to close this. It might be worth having an issue to track support for pure ESM Node modules and using using |
that pr is for build and not dev so I guess it's not going to help |
Describe the bug
JSDB is a 100% JavaScript database. The latest version is written in ESM. Simply importing it in the SSR server script (in vite-plugin-ssr) causes errors that do not otherwise exist in other contexts.
(I am trying to use an ESM node module that otherwise works fine during SSR with Vite.)
Reproduction
module
in package.json:What should happen?
There should not be any errors.
What actually happens?
Initially, you get the following error:
This is because the JSTable.js file in JSDB has the following import:
I don’t know if this is an issue with vite-plugin-ssr or vite itself but I’m leaning towards the latter. The workaround is to change the import to:
Needless to say, while I can do this in a module I control, this would mean I’d have to update all third-party modules that might use these sorts of imports in the future to make them compatible.
Once that error is out of the way, you get several related errors:
This time it’s clearly an esbuild error from the stack trace. The previous error did not have the same stack trace but it might also have to do with vite’s use of esbuild.
I don’t think the problem is with esbuild itself as I bundle Place using esbuild and it imports JSDB without issues. And I also just tried it with esbuild 0.9.3 – the same version used here – and it worked without issues.
Again, the fix is to rewrite the imports:
So
import { performance } from 'perf_hooks'
becomes:etc.
Once those changes have been made, I no longer get errors but loading the index page causes the browser to enter an infinite loading loop with no warnings or errors either in the browser or on the console.
There is a chance that this is an issue with vite-plugin-ssr, so I’ve opened an issue there also also but it feels like it is more a general Vite issue to me.
System Info
Output of
npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers
:Additional system info
Used package manager: npm
Before submitting the issue, please make sure you do the following
The text was updated successfully, but these errors were encountered: