-
Notifications
You must be signed in to change notification settings - Fork 156
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
Unable to import isolated-vm
in SSR of web applications
#423
Comments
This is just your bundler configuration. You'd run into the same problem with other native modules as well, for example import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
const ivm = require('isolated-vm'); There are other ways around it but unless you feel strongly about your module graph integrity and static analyzability then it's probably not worth bothering. |
Thank you for the prompt response! I will look into the solution you offered, it does feel a little less hacky 😄. For full context, I am building several SDKs that export components: for React, React Native, Qwik, Vue, SolidJS, Svelte, NextJS-specific React. Those SDKs are importing Therefore I have zero control over the web applications that end up importing my SDKs, and I want whatever solution I come up with to work out-of-the-box without needing any change in my users' web app configuration. |
I am going to close this seeing as it is normal behavior. Thanks for the improved workaround. |
I facing this issue and cannot get any of the workarounds to work once I deploy on vercel . Is there a solution that worked for anyone ? |
@djibomar one solution that partially works (for Nextjs specifically) is to:
NOTE: for Nextjs, you cannot use @laverdet 's suggestion:
because Nextjs does not allow |
@samijaber what would you expect from isolated-vm in a client-rendered React component? |
@laverdet I want it to work in the SSR only. Obviously it would break the client render, but I have guarantees in place to make sure the code only executes during SSR. A very minimal repro of what I'm trying to do is: import React from "react";
const getBrowserEval = () => new Function("return 1+3")();
const getServerEval = () => {
const ivm = eval("require")("isolated-vm");
const isolate = new ivm.Isolate({ memoryLimit: 128 });
const context = isolate.createContextSync();
const result = context.evalSync("1+3");
return result;
};
const isBrowser = typeof window !== "undefined";
const getEvalResult = isBrowser ? getBrowserEval : getServerEval;
export const MyComponent = () => {
return <div>1 + 3 = {getEvalResult()}</div>;
}; use |
I see. You can do this in Webpack. Not sure how you would do it in Vite but I am sure there is a way. In Webpack you just add I wouldn't recommend using If you want to avoid the direct |
Here is a repro using latest NextJS: https://github.com/samijaber/isolated-vm-import-bug
I have seen this issue in a variety of build tooling and web frameworks, including Webpack/Vite and Qwik/Vue/Nuxt. So it is not restricted to NextJS/React.
running
npm run build
(or dev) gives me this issue:For some reason, using this
require
hack works just fine:https://github.com/samijaber/isolated-vm-import-bug/blob/0b23a981f64c814e837412ce05c1353294438ff7/src/app/page.tsx#L1-L5
I am out of my depth here, and not sure why the
import
doesn't work, but the other approach does. I tried finding other issues reporting this but couldn't. Would appreciate any clarity!The text was updated successfully, but these errors were encountered: