fastify / help

Need help with Fastify? File an Issue here.
https://www.fastify.io/
64 stars 8 forks source link

getting cjs error with "index.cjs not supported" when I run tests #1008

Closed DanielRuf closed 2 months ago

DanielRuf commented 6 months ago

💬 how to resolve the cjs error

When I run jest in my project, I get an error:

$ USE_MOCKS=true && jest --detectOpenHandles
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/druf/Schreibtisch/projects/project-name/01-Backend/node_modules/string-width/index.js from /home/druf/Schreibtisch/projects/project-name/01-Backend/node_modules/cliui/build/index.cjs not supported.
Instead change the require of index.js in /home/druf/Schreibtisch/projects/project-name/01-Backend/node_modules/cliui/build/index.cjs to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/druf/Schreibtisch/projects/project-name/01-Backend/node_modules/cliui/build/index.cjs:291:21)
    at Object.<anonymous> (/home/druf/Schreibtisch/projects/project-name/01-Backend/node_modules/yargs/build/index.cjs:1:60678)
    at Object.<anonymous> (/home/druf/Schreibtisch/projects/project-name/01-Backend/node_modules/yargs/index.cjs:5:30)
    at _yargs (/home/druf/Schreibtisch/projects/project-name/01-Backend/node_modules/jest-cli/build/run.js:30:39)
    at buildArgv (/home/druf/Schreibtisch/projects/project-name/01-Backend/node_modules/jest-cli/build/run.js:149:26)
    at Object.run (/home/druf/Schreibtisch/projects/project-name/01-Backend/node_modules/jest-cli/build/run.js:124:24)
    at Object.<anonymous> (/home/druf/Schreibtisch/projects/project-name/01-Backend/node_modules/jest-cli/bin/jest.js:16:17)
    at Object.<anonymous> (/home/druf/Schreibtisch/projects/project-name/01-Backend/node_modules/jest/bin/jest.js:12:3)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

yarn why string-width tells me, that it probably comes from @fastify/static. forceESM is not set by us. Not sure why this happens.

yarn why string-width  
yarn why v1.22.19
[1/4] Why do we have the module "string-width"...?
[2/4] Initialising dependency graph...
[3/4] Finding dependency...
[4/4] Calculating file sizes...
=> Found "string-width@5.1.2"
info Reasons this module exists
   - "@fastify#static#glob#jackspeak#@isaacs#cliui" depends on it
   - Hoisted from "@fastify#static#glob#jackspeak#@isaacs#cliui#string-width"
   - Hoisted from "@fastify#static#glob#jackspeak#@isaacs#cliui#wrap-ansi#string-width"
info Disk size without dependencies: "172KB"
info Disk size with unique dependencies: "308KB"
info Disk size with transitive dependencies: "328KB"
info Number of shared dependencies: 4
Done in 0.30s.

Your Environment

mcollina commented 6 months ago

Thanks for reporting!

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

DanielRuf commented 6 months ago

Thanks for your swift reply @mcollina. I will have to invest some time for that, since this is a project that was created by another company and we are currently just maintaining it (doing upgrades). So I will have to dive a bit deeper and check which test file causes this.

In general the whole setup is a bit more complex since it involves multiple plugins and a distributed file structure.

DanielRuf commented 2 months ago

I'm not working on this project anymore, so I can not check or bisect that in detail. Closing.