Cannot find module 'module-name' while running ts-mocha

Viewed 481

When I run npm test which runs ts-mocha -p ./tsconfig.json tests/**/*.ts, I get the following error.

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.

I'm unit testing a file called utility.test.ts which uses import { Utility } from "../src/utility"; This Utility.ts file imports a node module import * as RandomNameGenerator from "random-name";. This module does exist in the node_modules folder inside the @types folder.

Note: I'm not testing the random-name functionality. I'm testing other util methods in the Utility file.

tsconfig.json looks like this

{
    "compilerOptions": { 
      "target": "es6", 
      "module": "commonjs", 
      "strict": true, 
      "declaration": true,
      "outDir": "./lib"
    },  
    "exclude": [
        "tests",
        "lib",
        "node_modules"
    ]
  }

I tried adding "moduleResolution": "node" to the config above but that didn't work.

Dependency section in package.json file.

"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"
  }

I tried moving "@types/random-name": "^0.1.0", to the dependencies instead of devDependencies section but that doesn't work either.

Any idea why it's not able to load the dependencies? What am I missing?

Thanks!

0 Answers
Related