LoginSignup
3
2

More than 3 years have passed since last update.

jestでCannot find module 'spdx-license-ids'というエラーになった時の対処

Posted at

どんなエラーが出たのか

jestでテストを実行した時に以下のエラーが出ました

Test suite failed to run

Cannot find module 'spdx-license-ids' from 'node_modules/spdx-expression-parse/scan.js'

Require stack:
  node_modules/spdx-expression-parse/scan.js
  node_modules/spdx-expression-parse/index.js
  node_modules/validate-npm-package-license/index.js
  node_modules/normalize-package-data/lib/fixer.js
  node_modules/normalize-package-data/lib/normalize.js
  node_modules/meow/index.js
  src/bin.ts
  src/tests/bin.test.ts

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:306:11)
  at Object.<anonymous> (node_modules/spdx-expression-parse/scan.js:4:11)

どうやって治したか

jestのconfigの moduleFileExtensionsjson を追加したら直りました

  • 変更前
package.jsonから一部
"jest": {
    "moduleFileExtensions": ["ts", "js"]
}
  • 変更後
package.jsonから一部
"jest": {
    "moduleFileExtensions": ["ts", "js", "json"]
}

なぜ治ったのか

今回エラーしたパッケージはspdx-license-ids(https://github.com/jslicense/spdx-license-ids) なのですが、内部でjsonファイルが使われています。
jestはconfigのmoduleFileExtensionsに指定された拡張子のファイルしか読み込みません。(https://jestjs.io/docs/en/configuration#modulefileextensions-arraystring)
なので、spdx-license-idsで使われていたjsonファイルを読み込むことができずエラーしました。

ちなみに、jestのmoduleFileExtensionsの初期値は

["js", "json", "jsx", "ts", "tsx", "node"]

なので、私が余計な設定をしていなければこのエラーは発生しませんでしたね😇

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2