Skip to content
This repository was archived by the owner on Jan 19, 2024. It is now read-only.

Fix ERR_REQUIRE_ESM when running ssr.js#53

Open
pyksid wants to merge 1 commit intorompetomp:masterfrom
pyksid:bugfix-ssr-nodejs-err-require-esm
Open

Fix ERR_REQUIRE_ESM when running ssr.js#53
pyksid wants to merge 1 commit intorompetomp:masterfrom
pyksid:bugfix-ssr-nodejs-err-require-esm

Conversation

@pyksid
Copy link

@pyksid pyksid commented Dec 15, 2023

With Inertia v1.0, an error occured while running ssr.js:

$ node public/build-ssr/ssr.js 

/public/build-ssr/ssr.js:29
module.exports = require("@inertiajs/core");
                 ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /node_modules/@inertiajs/core/dist/index.js from /public/build-ssr/ssr.js not supported.
index.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /node_modules/@inertiajs/core/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at @inertiajs/core (/public/build-ssr/ssr.js:29:18)
    at __webpack_require__ (/public/build-ssr/ssr.js:139:41)
    at ./node_modules/@inertiajs/vue3/dist/index.esm.js (/public/build-ssr/ssr.js:105:73)
    at __webpack_require__ (/public/build-ssr/ssr.js:139:41)
    at /public/build-ssr/ssr.js:195:73 {
  code: 'ERR_REQUIRE_ESM'
}

The problem is solved by adding @inertiajs/vue3 package to webpack-node-externals allowlist.

I took the opportunity to use the addExternals function of Webpack Encore (instead of config.externals) and externalsPresets option of Webpack.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant