描述
Error: Cannot find module 'G:/代码仓库/test-nest/src/api/test/dto/create-test.dto'
Require stack:
- G:\2.代码仓库\Private Project\template\template-server-nest\dist\api\test\test.controller.js
- G:\2.代码仓库\Private Project\template\template-server-nest\dist\api\test\test.module.js
- G:\2.代码仓库\Private Project\template\template-server-nest\dist\app.module.js
- G:\2.代码仓库\Private Project\template\template-server-nest\dist\main.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
at Function.Module._load (node:internal/modules/cjs/loader:922:27)
at Module.require (node:internal/modules/cjs/loader:1143:19)
at require (node:internal/modules/cjs/helpers:110:18)
at Object.<anonymous> (G:\2.代码仓库\Private Project\template\template-server-nest\dist\api\test\test.controller.js:49:46)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Function.Module._load (node:internal/modules/cjs/loader:960:12)
at Module.require (node:internal/modules/cjs/loader:1143:19)
解决
个人原因: 路径不能有中文
其他方案
-
删除 dist 目录并再次运行:
npm run start:dev -
tsconfig.json 中添加
"paths": { "src/*": ["src/*"] }
-
package.json文件中 jest 节点中添加
"moduleNameMapper": { "^src/(.*)$": "<rootDir>/$1" },
-
删除tsconfig.json 文件
noEmit
-
jest-e2e.json中transform节点添加
"^src/(.*)": "<rootDir>/../src/$1"
参考:
https://stackoverflow.com/questions/56703570/unable-to-run-tests-because-nest-cannot-find-a-module
https://stackoverflow.com/questions/63865678/nestjs-test-suite-failed-to-run-cannot-find-module-src-article-article-entity