vite-plugin-node icon indicating copy to clipboard operation
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.

Open juliushuck opened this issue 3 years ago • 1 comments
trafficstars

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

Screen Shot 2022-06-03 at 01 40 10

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,
    },
});

juliushuck avatar Jun 02 '22 23:06 juliushuck

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?

juliushuck avatar Jun 03 '22 19:06 juliushuck

try with vite@4 and latest version of this plugin. it should just work, since vite now by default build esm.

axe-me avatar Jan 12 '23 04:01 axe-me