freddy38510 / quasar-app-extension-ssg

Static Site Generator App Extension for Quasar.
MIT License
153 stars 16 forks source link

Error [ERR_REQUIRE_ESM]: require() of ES Module inside quasar-app-extension-ssg's server-entry.js #355

Closed tamysiby closed 1 year ago

tamysiby commented 1 year ago

I get the following error Error [ERR_REQUIRE_ESM]: require() of ES Module /home/tamy/DD_ECO/qfrontend/node_modules/query-string/index.js from /home/tamy/DD_ECO/qfrontend/node_modules/.cache/quasar-app-extension-ssg/server/server-entry.js not supported. Instead change the require of index.js in /home/tamy/DD_ECO/qfrontend/node_modules/.cache/quasar-app-extension-ssg/server/server-entry.js to a dynamic import() which is available in all CommonJS modules. at Module.<anonymous> (/home/tamy/DD_ECO/qfrontend/node_modules/.cache/quasar-app-extension-ssg/server/server-entry.js:16:19) at createRenderFn (/home/tamy/DD_ECO/qfrontend/node_modules/quasar-app-extension-ssg/src/vite/ssg-create-render-fn.js:30:7) at SsgBuilder.generatePages (/home/tamy/DD_ECO/qfrontend/node_modules/quasar-app-extension-ssg/src/vite/ssg-builder.js:88:64) at run (/home/tamy/DD_ECO/qfrontend/node_modules/quasar-app-extension-ssg/src/vite/cmd/generate.js:126:20) { code: 'ERR_REQUIRE_ESM' } and I'm not sure how to fix it since it seems like it's inside the quasar-app-extension's generated file? Is it a problem with the query-string library? How do I fix this?

freddy38510 commented 1 year ago

When running quasar build -m ssr or quasar ssg generate command, it compiles both client-side and server-side outputs. The server-side compiled code is in CommonJs format.

The query-string package is a pure ESM since its version 8.0.0.

So you need to transpile this library to commonJs. To do this, try adding this to your quasar.config.js file:

// quasar.config file

build: {
  extendViteConf (viteConf, { isServer }) {
    if(isServer) {
       // see https://v2.vitejs.dev/config/#ssr-noexternal

       viteConf.ssr = viteConf.ssr || { };
       viteConf.ssr.noExternal = viteConf.ssr.noExternal || [];
       viteConf.ssr.noExternal.push('query-string');

      // if it doesn't work, try this instead:
      // viteConf.ssr.noExternal.push(/query-string/);
    }
  }
}

Related to #309

tamysiby commented 1 year ago

Thank you for the explanation, and that solution worked for me! Thank you~