N
Nuxt10mo ago
David

Nuxt 2 and Directus SDK, not loading CommonJS module

Hello ! I'm trying to use the Directus SDK with the latest version of Nuxt 2. I am getting this error :
require() of ES Module /node_modules/@directus/sdk/dist/index.js from /node_modules/vue-server-renderer/build.dev.js not supported.
Instead change the require of index.js in /node_modules/vue-server-renderer/build.dev.js to a dynamic import() which is available in all CommonJS modules.
require() of ES Module /node_modules/@directus/sdk/dist/index.js from /node_modules/vue-server-renderer/build.dev.js not supported.
Instead change the require of index.js in /node_modules/vue-server-renderer/build.dev.js to a dynamic import() which is available in all CommonJS modules.
The Directus SDK has an index.cjs file and I don't understand why vue-server-renderer tries to load the ES module instead of the CommonJS file. I don't have "type": "module" in my package.json. I even tried to set "type": "commonjs", but it still behaves the same. Has anyone had that same kind of issue and would know about a trick to get it to work?
7 Replies
danielroe
danielroe10mo ago
Would you try adding it to build.transpile in your nuxt config?
David
DavidOP10mo ago
@danielroe Thanks for the suggestion! This helped me go further. Then I got a similar error with @directus/system-data, which I added to build.transpile as well. Now I'm getting another error. From the look of it, I'd say it's because it tries to do something that doesn't work server-side.
TypeError
o.URL is not a constructor
TypeError
o.URL is not a constructor
https://raw.githubusercontent.com/directus/directus/v10.10.3/sdk/src/client.ts
No description
danielroe
danielroe10mo ago
likely original issue is caused by directus sdk exporting something ending in .js but actually being in ES syntax. it should probably be reported to them as a bug
David
DavidOP10mo ago
That's right. Their index.js file is an ES module. Then, the package.json file of the Directus SDK has "type": "module", and the CommonJS file is named index.cjs. Is that really incorrect? Shouldn't the "type": "module" be a hint that index.js is an ES module and that the index.cjs file should be looked for to get the CommonJS version?
David
DavidOP10mo ago
No description
danielroe
danielroe10mo ago
I think it might be fine... The problem is likely webpack v4. which unfortunately is just ... buggy on this, because it predates all of this.
David
DavidOP10mo ago
Okay I see. Thanks again for your help! Regarding the error
o.URL is not a constructor
o.URL is not a constructor
I was able to get rid of it by doing
globalThis.URL = URL;
globalThis.URL = URL;
before calling createDirectus.
Want results from more Discord servers?
Add your server