Skip to content
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

Vite, ssr => Error [ERR_REQUIRE_ESM] #31

Closed
aleksanderd opened this issue Jul 31, 2021 · 3 comments
Closed

Vite, ssr => Error [ERR_REQUIRE_ESM] #31

aleksanderd opened this issue Jul 31, 2021 · 3 comments

Comments

@aleksanderd
Copy link

aleksanderd commented Jul 31, 2021

Trying to setup ssr with vite. Created server.ts as described in solid/vite docs/examples but at

render = (await vite.ssrLoadModule('/src/entry-server.tsx')).render; // renderToStringAsync inside

got error:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/rootfs/var/www/thesite/thesite/node_modules/solid-app-router/dist/index.js
require() of ES modules is not supported.
require() of /home/rootfs/var/www/thesite/thesite/node_modules/solid-app-router/dist/index.js from /home/rootfs/var/www/thesite/thesite/node_modules/vite/dist/node/chunks/dep-11db14da.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/rootfs/var/www/thesite/thesite/node_modules/solid-app-router/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at nodeRequire (/home/rootfs/var/www/thesite/thesite/node_modules/vite/dist/node/chunks/dep-11db14da.js:73337:17)
    at ssrImport (/home/rootfs/var/www/thesite/thesite/node_modules/vite/dist/node/chunks/dep-11db14da.js:73290:20)
    at eval (/src/AppDocument.tsx:18:31)
    at instantiateModule (/home/rootfs/var/www/thesite/thesite/node_modules/vite/dist/node/chunks/dep-11db14da.js:73323:166)

Could you help with it?

Thank you!

@rturnq
Copy link
Member

rturnq commented Jul 31, 2021

Are you using Windows? If so, I would point you to this Vite issue: vitejs/vite#2393

The workaround here is to tell Vite not to externalize the router from the SSR build (eg. use the commonjs version shipped in the package) by adding the following in your Vite config file:

// vite.config.js
export default defineConfig({
  //...
  ssr: {
    noExternal: ['solid-app-router']
  },
});

Even if you are not using Windows, Vite uses a heuristic to make the decision which may fail, so the workaround it should work in other cases too. Here is Vite's documentation about SSR externals: https://vitejs.dev/guide/ssr.html#ssr-externals

@rturnq
Copy link
Member

rturnq commented Jul 31, 2021

Just noticed the paths in your error 🤦 Try adding the config option anyway - hopefully that will resolve the issue.

@aleksanderd
Copy link
Author

Yes, it helped.

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants