typescript - 运行 ts-mocha 时找不到模块“模块名称”
问题描述
当我运行npm test
which runsts-mocha -p ./tsconfig.json tests/**/*.ts
时,我收到以下错误。
Error: Cannot find module 'random-name'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
at Function.Module._load (internal/modules/cjs/loader.js:508:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (code-location\src\utility.ts:5:1)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Module.m._compile (code-location\node_modules\ts-mocha\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Object.require.extensions.(anonymous function) [as .ts] (code-location\node_modules\ts-mocha\node_modules\ts-node\src\index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (code-location\src\give-me-an-avatar.ts:2:1)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Module.m._compile (code-location\node_modules\ts-mocha\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Object.require.extensions.(anonymous function) [as .ts] (code-location\node_modules\ts-mocha\node_modules\ts-node\src\index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (code-location\tests\give-me-an-avatar.test.ts:1:1)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Module.m._compile (code-location\node_modules\ts-mocha\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Object.require.extensions.(anonymous function) [as .ts] (code-location\node_modules\ts-mocha\node_modules\ts-node\src\index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.exports.requireOrImport (code-location\node_modules\mocha\lib\esm-utils.js:20:12)
at Object.exports.loadFilesAsync (code-location\node_modules\mocha\lib\esm-utils.js:33:34)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:757:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm ERR! Test failed. See above for more details.
我正在对一个名为的文件进行单元测试,该文件utility.test.ts
使用import { Utility } from "../src/utility";
此Utility.ts
文件导入节点模块import * as RandomNameGenerator from "random-name";
。该模块确实存在于node_modules
文件夹内的@types
文件夹中。
注意:我没有测试随机名称功能。我正在实用程序文件中测试其他实用程序方法。
tsconfig.json 看起来像这样
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": true,
"declaration": true,
"outDir": "./lib"
},
"exclude": [
"tests",
"lib",
"node_modules"
]
}
我尝试添加"moduleResolution": "node"
到上面的配置,但没有奏效。
package.json 文件中的依赖部分。
"devDependencies": {
"@types/chai": "^4.2.14",
"@types/mocha": "^8.2.0",
"@types/random-name": "^0.1.0",
"chai": "^4.2.0",
"mocha": "^8.2.1",
"mocha-junit-reporter": "^1.22.0",
"nyc": "^14.1.1",
"ts-mocha": "^8.0.0",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
}
我尝试移动"@types/random-name": "^0.1.0",
到dependencies
而不是devDependencies
部分,但这也不起作用。
知道为什么它无法加载依赖项吗?我错过了什么?
谢谢!
解决方案
推荐阅读
- php - 不推荐使用的 ping() 的替代方案
- c++ - Why isn't glDrawElements drawing my circle?
- linux - cron 任务结束 php 会话
- javascript - 使用 object.keys 在两个数字之间迭代
- r - 更改存储在列表中的 df 中最后一列的名称
- azure - 无法从 Dynamics 365 Customer Insights 平台接收正确的预测数据
- postgresql - postgres/timescaledb 中的连续聚合需要 time_bucket-function?
- css - LESS 类本身声明
- monaco-editor - Monaco-editor :有没有办法防止光标/编辑器的焦点在单击编辑器外部时丢失(模糊)
- symfony - SYMFONY Serializer - 解码数组中的 XML 文件,强制子元素的类型