0

I'm developing an app in Nodejs and building it with vite. In my code I'm using "AsyncLocalStorage", but I get this error during build: "AsyncLocalStorage" is not exported by "__vite-browser-external", imported by [path name].

This is my code:

import { AsyncLocalStorage } from "node:async_hooks";

const asyncLocalStorage = new AsyncLocalStorage();
asyncLocalStorage.run(new Map(), () => {
  asyncLocalStorage.getStore()?.set("requestid", "45");
});

This is my vite.config.ts:

import { defineConfig } from "vite";

export default defineConfig({
  plugins: [],

  build: {
    rollupOptions: {
      input: "./index.js",
    },
  },
});


Everything is ok in develop mode and my app works properly, but not during the building. I appreciate it if you could help me. Thank you enter image description here

I also added rollup-plugin-node-polyfills and alias for 'async-hook' in vite config, but it seems it's not been added to vite yet.

  • Well, node specific functionality won't be available in the browser. It should be fairly straightforward to implement your own abstraction which wraps whatever is available. – Aluan Haddad Aug 03 '23 at 20:53
  • 1
    Thanks Aluan Haddad for the answer, but actually it's not related to browser. AsyncLocalStorage is a node feature that leads to share the context through the request lifetime. Actually, I asked the issue in vite github and they pointed out that vite is not a node bundller. After more research I came to know that bundler is not needed for server side and I removed vite totally from the app. Now, it's ok. – sara khosropour Aug 04 '23 at 14:09
  • Glad you got it figured out. I had the wrong idea... – Aluan Haddad Aug 04 '23 at 14:26

0 Answers0