首页 > 解决方案 > 运行 ts-mocha 时找不到模块“模块名称”

问题描述

当我运行npm testwhich 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部分,但这也不起作用。

知道为什么它无法加载依赖项吗?我错过了什么?

谢谢!

标签: typescriptunit-testingnpmtypesmocha.js

解决方案


推荐阅读