今天在開發專案時,重新整理了一下 package.json 中許多 libraries 的版本,結果在執行 Jest 測試時遇到了一個奇怪的錯誤:
TypeError: [BABEL] /src/test/jest.setup.js: Cannot use ‘in’ operator to search for ‘CallExpression’ in undefined
查了一下相關的資源後,記錄一下可以如何解決這個問題:
- 將 node_modules 資料夾刪除後,重新
yarn install或npm install來重新安裝所有 libraries。
# 刪除 node_modules 資料夾
rm -rf node_modules
# 如果使用 npm
npm install
# 如果使用 yarn
yarn install
- 如果做完 1 後仍然不行,請手動至 yarn.lock 或 package-lock.json 中,將
@babel/core鎖定版號的部分皆刪除,再次重新做第 1 步即可 👨🔧
# 刪除 yarn.lock / package-lock.json 中 @babel/core 部分的所有鎖定版本
# 例如:
# "@babel/core@^7.1.0", "@babel/core@^7.11.6", "@babel/core@^7.12.3", "@babel/core@^7.18.5", "@babel/core@^7.18.9", "@babel/core@^7.23.0", "@babel/core@^7.23.2", "@babel/core@^7.7.2", "@babel/core@^7.8.0":
# version "7.28.0"
# resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.28.0.tgz#55dad808d5bf3445a108eefc88ea3fdf034749a4"
# integrity sha512-UlLAnTPrFdNGoFtbSXwcGFQBtQZJCNjaN6hQNP3UPvuNXT1i82N26KL3dZeIpNalWywr9IuQuncaAfUaS1g6sQ==
# dependencies:
# "@ampproject/remapping" "^2.2.0"
# "@babel/code-frame" "^7.27.1"
# "@babel/generator" "^7.28.0"
# "@babel/helper-compilation-targets" "^7.27.2"
# "@babel/helper-module-transforms" "^7.27.3"
# "@babel/helpers" "^7.27.6"
# "@babel/parser" "^7.28.0"
# "@babel/template" "^7.27.2"
# "@babel/traverse" "^7.28.0"
# "@babel/types" "^7.28.0"
# convert-source-map "^2.0.0"
# debug "^4.1.0"
# gensync "^1.0.0-beta.2"
# json5 "^2.2.3"
# semver "^6.3.1"
# 刪除 node_modules 資料夾
rm -rf node_modules
# 如果使用 npm
npm install
# 如果使用 yarn
yarn install