Solana Web3.jsで「[ERR_REQUIRE_ESM]: require() of ES Module」エラー

現象

package.jsonを最新にしてts-nodeしたら以下のエラー。

ターミナル

usr/local/lib/node_modules/ts-node/dist/index.js:851
            return old(m, filename);
                   ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/user/Documents/Programming/Blockchain/protoype/solana-script/node_modules/@solana/spl-token/lib/cjs/index.js from /Users/user/Documents/Programming/Blockchain/protoype/solana-script/node_modules/@metaplex-foundation/mpl-candy-machine/dist/src/generated/instructions/mintNft.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 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 /Users/user/Documents/Programming/Blockchain/protoype/solana-script/node_modules/@solana/spl-token/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.require.extensions.<computed> [as .js] (/usr/local/lib/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/Users/user/Documents/Programming/Blockchain/protoype/solana-script/node_modules/@metaplex-foundation/mpl-candy-machine/dist/src/generated/instructions/mintNft.js:27:31)
    at Object.require.extensions.<computed> [as .js] (/usr/local/lib/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/Users/user/Documents/Programming/Blockchain/protoype/solana-script/node_modules/@metaplex-foundation/mpl-candy-machine/dist/src/generated/instructions/index.js:19:14)
    at Object.require.extensions.<computed> [as .js] (/usr/local/lib/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/Users/user/Documents/Programming/Blockchain/protoype/solana-script/node_modules/@metaplex-foundation/mpl-candy-machine/dist/src/CandyMachineProgram.js:29:35)
    at Object.require.extensions.<computed> [as .js] (/usr/local/lib/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/Users/user/Documents/Programming/Blockchain/protoype/solana-script/node_modules/@metaplex-foundation/mpl-candy-machine/dist/src/mpl-candy-machine.js:17:14)
    at Object.require.extensions.<computed> [as .js] (/usr/local/lib/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/Users/user/Documents/Programming/Blockchain/protoype/solana-script/node_modules/@metaplex-foundation/js/dist/cjs/plugins/candyMachineModule/createCandyMachine.cjs:6:23)
    at Object.require.extensions.<computed> [as .js] (/usr/local/lib/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/Users/user/Documents/Programming/Blockchain/protoype/solana-script/node_modules/@metaplex-foundation/js/dist/cjs/plugins/candyMachineModule/CandyMachinesBuildersClient.cjs:5:26)
    at Object.require.extensions.<computed> [as .js] (/usr/local/lib/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/Users/user/Documents/Programming/Blockchain/protoype/solana-script/node_modules/@metaplex-foundation/js/dist/cjs/plugins/candyMachineModule/CandyMachinesClient.cjs:5:35)
    at Object.require.extensions.<computed> [as .js] (/usr/local/lib/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/Users/user/Documents/Programming/Blockchain/protoype/solana-script/node_modules/@metaplex-foundation/js/dist/cjs/plugins/candyMachineModule/plugin.cjs:5:27)
    at Object.require.extensions.<computed> [as .js] (/usr/local/lib/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/Users/user/Documents/Programming/Blockchain/protoype/solana-script/node_modules/@metaplex-foundation/js/dist/cjs/plugins/corePlugins/plugin.cjs:16:16)
    at Object.require.extensions.<computed> [as .js] (/usr/local/lib/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/Users/user/Documents/Programming/Blockchain/protoype/solana-script/node_modules/@metaplex-foundation/js/dist/cjs/Metaplex.cjs:6:14)
    at Object.require.extensions.<computed> [as .js] (/usr/local/lib/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/Users/user/Documents/Programming/Blockchain/protoype/solana-script/node_modules/@metaplex-foundation/js/dist/cjs/index.cjs:5:16)
    at Object.require.extensions.<computed> [as .js] (/usr/local/lib/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/Users/user/Documents/Programming/Blockchain/protoype/solana-script/src/app/main.ts:38:14)
    at Module.m._compile (/usr/local/lib/node_modules/ts-node/dist/index.js:857:29)
    at Object.require.extensions.<computed> [as .ts] (/usr/local/lib/node_modules/ts-node/dist/index.js:859:16)
    at phase4 (/usr/local/lib/node_modules/ts-node/dist/bin.js:466:20)
    at bootstrap (/usr/local/lib/node_modules/ts-node/dist/bin.js:54:12)
    at main (/usr/local/lib/node_modules/ts-node/dist/bin.js:33:12)
    at Object.<anonymous> (/usr/local/lib/node_modules/ts-node/dist/bin.js:579:5) {
  code: 'ERR_REQUIRE_ESM'
}

package.json

{
  "dependencies": {
    "@metaplex-foundation/js": "^0.13.3",
    "@solana/spl-token": "^0.3.2",
    "@solana/web3.js": "^1.50.1",
    "@types/node": "^18.7.12",
    "arweave": "^1.11.6",
    "fs": "^0.0.1-security",
    "sleep": "^6.3.0",
    "ts-node": "^10.9.1"
  },
  "devDependencies": {
    "@types/jest": "^28.1.7",
    "@types/sleep": "^0.0.8",
    "jest": "^28.1.3",
    "prettier": "2.7.1",
    "ts-jest": "^28.0.8",
    "typescript": "^4.7.4"
  },
  "scripts": {
    "build": "tsc",
    "test": "jest",
    "format": "prettier --write src test"
  }
}

原因

原因はわからなかったが、トリガーはわかった。
@solana/spl-token が原因で発生していた。

spl-tokenがcommonjsからECMAScriptに書き方が変わった?
でもエラーが出てるのはmetaplex jsのmintNft.jsになっているので謎。

Issuesはおそらくこれ?
debug: @solana/spl-token@0.3.2 breaks TypeScript imports #3522

対応

やむを得ず、一旦以下にダウングレードして対応した。

"@solana/spl-token": "^0.2.0",

npm関連フォルダを削除して npm i で解決。