Skip to content

ts-node-dev Cannot find module #953

Closed

Description

@thany

Suppose I have this kind of structure:

src
├── app.ts
├── config.ts
├── handlers
│   └── item.ts
└── types.ts

app.ts imports item.ts like so:

import itemHandler from './handlers/item';

item.ts on its turn imports types.ts like so:

import { ItemParams } from '/types';

This works absolutely fine. Exactly like I expect it. After all types.ts is at the root, and that's where I'm importing it from. So, I'll add another import:

import { ItemParams } from '/types';
import config from '/config';

Boom. Error: Cannot find module '/config'
That one cannot be found. How come? It's imported from the same file, from the same location, using the same absolute notation. How is it that it can find the one, but not the other?

I've checked to see if it matters what they export - it doesn't, as I suspected.

If nothing else, I'd like to know where it cannot find the module requested. As in, where did it look for it? But really this should just work, especially since running tsc works.

The only workaround I've found is to turn my imports relative:

import { ItemParams } from '../types';
import config from '../config';

But this is undesirable. What if I move the file with imports to another location? And also this is not as easily reusable.

For reference, here's my tsconfig.json:

{
  "include": ["src"],
  "compilerOptions": {
    "target": "ES2019",
    "module": "CommonJS",
    "lib": ["dom", "esnext", "es2017"],
    "importHelpers": true,
    "declaration": true,
    "downlevelIteration": true,
    "sourceMap": true,
    "rootDir": "./src",
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "baseUrl": "./",
    "outDir": "./dist",
    "paths": {
      "*": ["src/*", "node_modules/*"]
    },
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true
  }
}

Versions:
Node.js: 12.14.1
NPM: 6.13.7
Typescript: 3.7.5
ts-node-dev: 1.0.0-pre.44

Activity

  1. changed the title [-]Cannot find module[/-] [+]ts-noce-dev Cannot find module[/+] on Feb 2, 2020
  2. changed the title [-]ts-noce-dev Cannot find module[/-] [+]ts-node-dev Cannot find module[/+] on Feb 2, 2020
  3. cspotcode commented on Feb 3, 2020

    @cspotcode
    Collaborator

    @thany

    I see you're using "paths" in your tsconfig.json. Are you also using tsconfig-paths?

    https://github.com/TypeStrong/ts-node#loading-tsconfigjson

    You say that tsc succeeds. Are you able to run the code that has been emitted by tsc? Or do you get the same error when you try to run it?

  4. 3rd commented on Feb 4, 2020

    @3rd

    Running into the same issue, @thany are you using yarn 2 (berry) by any chance?

  5. thany commented on Feb 6, 2020

    @thany
    Author

    @cspotcode I'll have to verify that when I get home. I think I remember seeing it running outside of ts-node-dev, but I'm not 100% sure. I'm not aware of having installed/configured tsconfig-paths, so I'll check that as well.

    @3rd I'm using NPM, as you can see in my opening comment. Besides, I honestly can't imagine the package manager causing this issue.

  6. cspotcode commented on Feb 6, 2020

    @cspotcode
    Collaborator

    @thany Our README has a section that explains why you need to use tsconfig-paths in certain situations.
    https://github.com/TypeStrong/ts-node#loading-tsconfigjson

  7. thany commented on Feb 6, 2020

    @thany
    Author

    I noticed the stuff emitted from tsc doesn't work either and I couldn'tmake it work in any way other than by switching to webpack. Doing so reveals that both the aforementioned modules cannot be found, and I cannot figure out for the life of me how to tell the compiler/loader/config where to look...

  8. cspotcode commented on Feb 6, 2020

    @cspotcode
    Collaborator

    Have you tried my suggestions? Have you looked at the README section that I pointed you to?

  9. thany commented on Feb 6, 2020

    @thany
    Author

    I have, but I don't really see it explaining why I need that package... Shouldn't it just, you know, work?

  10. cspotcode commented on Feb 6, 2020

    @cspotcode
    Collaborator

    No, it doesn't really work that way. This is part of TypeScript's design, so it's not really a ts-node behavior.

    This section of the TypeScript handbook explains it:
    https://www.typescriptlang.org/docs/handbook/module-resolution.html#additional-module-resolution-flags

    It is important to note that the compiler will not perform any of these transformations; it just uses these pieces of information to guide the process of resolving a module import to its definition file.

    The tsconfig-paths README explains a bit more:
    https://www.npmjs.com/package/tsconfig-paths

    It also has some examples of usage with ts-node, which should set you on the right track.

    I'm going to close this since it's not a ts-node issue. The linked resources above should be everything you need to get your code working. Good luck!

  11. VadimZP commented on Dec 26, 2023

    @VadimZP
  12. thany commented on Dec 27, 2023

    @thany
    Author

    Thanks, @VadimZP, but honestly I don't agree with the whole paradigm of throwing more stuff towards my project, and then hope that doesn't also break at some point, even if it works today. Why can't things "just work"?

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions