Skip to content

Error: Cannot find module with ts-node  #1285

Description

@daition

Expected Behavior

"ts-node test.ts" should print "hello"

Actual Behavior

Error: Cannot find module

I'm not sure where is wrong. tsc works well but ts-node can't resolve the module "testModule" which is declared with "declare module" in a d.ts.

Steps to reproduce the problem

  1. ts-node 9.1.1; node 14.15.5;tsc 4.2.3
  2. at same directory, I have three files test.ts, testModule.d.ts, testModule.js
  • testModule.js

function testMethod(){
console.log("hello")
}
module.exports={testMethod};`

  • testModule.d.ts

declare module "testModule" {
export function testMethod():void;
};

  • test.ts

import * as t from "testModule"; t.testMethod();

  • tsconfig.json

{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"allowJs": true,
"strict": true,
"noImplicitAny": false,
"esModuleInterop": true,
"outDir": "./out",
"moduleResolution": "node",
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"paths": {"testModule":["testModule.js"]},
"typeRoots": ["./node_modules/@types"],
"traceResolution":true
},
}

  1. then, "ts-node test.ts" will give error

Error: Cannot find module 'testModule'
Require stack:

  • /Users/daixuan/mine/mycode/learnTS/tryts/test.ts
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object. (/Users/daixuan/mine/mycode/learnTS/tryts/test.ts:1:1)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Module.m._compile (/Users/daixuan/mine/mycode/opencvjs/emsdk/node/14.15.5_64bit/lib/node_modules/ts-node/src/index.ts:1056:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Object.require.extensions. [as .ts] (/Users/daixuan/mine/mycode/opencvjs/emsdk/node/14.15.5_64bit/lib/node_modules/ts-node/src/index.ts:1059:12)
    at Module.load (internal/modules/cjs/loader.js:928:32)

Minimal reproduction

Specifications

  • ts-node version:
  • node version:
  • TypeScript version:
  • tsconfig.json, if you're using one:
{}
  • Operating system and version:
  • If Windows, are you using WSL or WSL2?:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions