vite-plugin-node
vite-plugin-node copied to clipboard
Using top level await: Module format cjs does not support top-level await. Use the "es" or "system" output formats rather.
I want to use top level await in my server. When I do so, I get the following error message:
Module format cjs does not support top-level await. Use the "es" or "system" output formats rather.
So I tried build: { target: "esnext" }, but it didn't help.
I also tried
lib: {
entry: "./source/app.js",
formats: ["es"],
},
This works, but then the outputed file still contains imports for the dependency and did not bundle everything in one file
Do I have to set another option for this to work? Or do I have to use a plugin like vite-plugin-top-level-await for that? I think vite-nodesays that they support top level wait out of the box, could this also be done in this package?
This is my vite.config:
import path from "path";
import * as vite from "vite";
import mkcertVitePlugin from "vite-plugin-mkcert";
import { VitePluginNode as nodeVitePlugin } from "vite-plugin-node";
const viteConfig = vite.defineConfig({
resolve: {
alias: [
{
find: "@",
replacement: path.resolve("source"),
},
],
},
build: { target: "esnext" },
plugins: [
mkcertVitePlugin(),
...nodeVitePlugin({
adapter: "koa",
appPath: "./source/app.js",
exportName: "app",
}),
],
server: {
https: { maxSessionMemory: 100 },
host: "0.0.0.0",
port: 4000,
},
});
rollupOptions: { output: { format: "es" } } did the trick for me. I do not have to do this in my react vite config, just in my "node" vite config. Did I do something else wrong?
try with vite@4 and latest version of this plugin. it should just work, since vite now by default build esm.