Skip to content

codemod: ERR_REQUIRE_ESM when migrating from v4 to v5 #7154

Description

@wangwalton

Describe the bug

ERR_REQUIRE_ESM when migrating from v4 to v5 with codegen

Your minimal, reproducible example

NA

Steps to reproduce

Running this

 npx jscodeshift@latest ./src/ \                                                                                                                                                                                      main *
  --extensions=ts,tsx \
  --parser=tsx \
  --transform=./node_modules/@tanstack/react-query/build/codemods/src/v5/remove-overloads/remove-overloads.js

gives this error:

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/dev/node_modules/@tanstack/react-query/build/codemods/src/v5/remove-overloads/remove-overloads.js from /Users/.npm/_npx/a3dac9416cb5dcf1/node_modules/jscodeshift/src/Worker.js not supported.
remove-overloads.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 remove-overloads.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 /Users/./node_modules/@tanstack/react-query/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.newLoader [as .js] (/Users//.npm/_npx/a3dac9416cb5dcf1/node_modules/pirates/lib/index.js:121:7)
    at setup (./_npx/a3dac9416cb5dcf1/node_modules/jscodeshift/src/Worker.js:93:18)
    at Object.<anonymous> (./_npx/a3dac9416cb5dcf1/node_modules/jscodeshift/src/Worker.js:45:3) {
  code: 'ERR_REQUIRE_ESM'
}

Expected behavior

No error

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

macos

Tanstack Query adapter

None

TanStack Query version

5.28.6

TypeScript version

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions