ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

ts-node cannot find module

Open tedcurrent opened this issue 9 years ago • 34 comments

I have two files:

  • src/app/index.ts
  • src/config.ts

Within index.ts:

import { getConfig } from "config";  
getConfig();

Within config.ts:

export const getConfig = () => console.log("hello world!");

I have a tsconfig.json file:

{
  "compilerOptions": {
    "outDir": "./ts-build/",
    "module": "commonjs",
    "moduleResolution": "node",
    "baseUrl": "src",
    "target": "es5"
  }
}

Running ts-node ./src/app/index.ts throws me: Error: Cannot find module 'config'. Setting moduleResolution to classic doesn't change anything.

Versions:

"ts-node": "^3.3.0",
"typescript": "^2.5.2"

Any ideas if this is a ts-node issue, typescript issue or did I do something wrong?

tedcurrent avatar Sep 11 '17 08:09 tedcurrent

Have you tried compiling with regular TypeScript? Otherwise, I'd guess you haven't written a definition file - this is the standard error from TypeScript when it can't find the definition for a module.

blakeembrey avatar Sep 11 '17 18:09 blakeembrey

Actually, it looks like you expect it to find config.ts from app/index.ts? You should use relative paths import {} from '../config'.

blakeembrey avatar Sep 11 '17 18:09 blakeembrey

Thanks for the reply @blakeembrey. I've run this with tsc --traceResolution and the resolution works fine as seen here:

======== Resolving module 'config' from 'C:/Dev/test-ts-node/src/app/index.ts'. ========
Explicitly specified module resolution kind: 'NodeJs'.
'baseUrl' option is set to 'C:/Dev/test-ts-node/src', using this value to resolve non-relative module name 'config'.
Resolving module name 'config' relative to base url 'C:/Dev/test-ts-node/src' - 'C:/Dev/test-ts-node/src/config'.
Loading module as file / folder, candidate module location 'C:/Dev/test-ts-node/src/config', target file type 'TypeScript'.
File 'C:/Dev/test-ts-node/src/config.ts' exist - use it as a name resolution result.
======== Module name 'config' was successfully resolved to 'C:/Dev/test-ts-node/src/config.ts'. ========

What I actually discovered was that I have to have the environment variable NODE_PATH set to match the baseUrl also. I ended up running the following: NODE_PATH=./src ts-node ./src/app/index.ts.

Now I know we can't set environment variables in tsconfig, this would be convenient and isn't a ts-node concern. However, do you have any thoughts if environment variables could be somehow settable on the ts-node side? I'm running nodemon in development and there it's easy to set within nodemon.json.

tedcurrent avatar Sep 12 '17 12:09 tedcurrent

Environment variables don't really have anything to do with ts-node. You can set it just like you are, or before nodemon if you wanted. You can also modify the require function in node.js using https://github.com/TypeStrong/ts-node#loading-tsconfigjson if you wanted to. I don't intend to have ts-node have the ability to alter the runtime require behaviour of node.js in any way directly.

blakeembrey avatar Sep 12 '17 16:09 blakeembrey

Alright, thanks for the comments!

tedcurrent avatar Sep 13 '17 07:09 tedcurrent

Does ts-node not handle non-relative paths? I'm trying to use this strategy to import modules: https://goenning.net/2017/07/21/how-to-avoid-relative-path-hell-javascript-typescript-projects/. Basically I don't want to use relative paths, but ts-node fails saying it cannot find the module. Is this not supported?

ashok-sc avatar Dec 05 '17 04:12 ashok-sc

@ashok-sc See the note in the README under https://github.com/TypeStrong/ts-node#loading-tsconfigjson. It's not provided directly by ts-node, but it's easily supported.

blakeembrey avatar Dec 07 '17 04:12 blakeembrey

@blakeembrey Before I open another issue I want to be sure I'm not going insane because I currently cannot get ts-node to do anything worthwhile. I've been beating my head bloody for days searching, reading and experimenting with every config I can think of.

If it's not one thing it's another. But most of my woes seem to be related to importing other modules. In the case of gulp for example, I'm loading other modules and registering tasks with gulp but it can't even get past the imports:

TSError: ⨯ Unable to compile TypeScript
_gulp\tests.ts (1,43): Cannot find module 'gulp-typescript-helper'. (2307)
_gulp\tests.ts (2,23): Cannot find module './constants/TaskNames'. (2307)
_gulp\tests.ts (3,23): Cannot find module 'gulp'. (2307)

This is the branch I'm currently working on: https://github.com/electricessence/TypeScript.NET/tree/next

I can't get mocha to work either. :(

electricessence avatar Mar 28 '18 04:03 electricessence

@electricessence Have you tried updating ts-node? If that fails I can look further into it, but the version you have listed in your package.json is over a year old.

blakeembrey avatar Mar 28 '18 11:03 blakeembrey

@blakeembrey Wow ok. That was odd. (I had thought I updated everything.) My first attempt to update took it from 1.6 to 1.7. Then I updated global and it jumped to 5.0. Then I uninstalled and installed again and got the project up to 5. Now everything works. 😄 Thank you.

electricessence avatar Mar 28 '18 15:03 electricessence

Removing module and target fields from tsconfig.json worked for me.

hyphenwrex avatar Jun 27 '18 23:06 hyphenwrex

@tedcurrent solution did the trick for me. I also hade NODE_PATH=src in my .env file, so adding it before the ts-node call solved it. I'm using ts-node together with nodemon, so my code is:

nodemon.json

{
  "ignore": ["**/*.test.ts", "**/*.spec.ts", ".git", "node_modules"],
  "watch": ["./src"],
  "exec": "set NODE_PATH=./src node --inspect -r ./node_modules/.bin/ts-node/register ./src/index.ts",
  "ext": "ts"
}

Note that Windows users need to do set NODE_PATH.

MikaelEdebro avatar Aug 31 '18 08:08 MikaelEdebro

I'm stuck on this too. My tsconfig has this:

"paths": {
			"@middleware": ["../middleware/src"]
		},

And I'm able to pull interfaces/types from it. But nothing else.

SephReed avatar Dec 04 '19 20:12 SephReed

Stuck here, too.

% npx ts-node src/queue.bench.ts
⨯ Unable to compile TypeScript:
src/queue.bench.ts:5:20 - error TS2307: Cannot find module 'benny'.

5 import * as b from "benny";
                     ~~~~~~~

Loading the module use plain node works:

% node
> require('benny')
{ add: { [Function: add] only: [Function], skip: [Function] },
  complete: [Function: complete],
  cycle: [Function: cycle],
  save: [Function: save],
  suite: [Function: suite],
  default:
   { add: { [Function: add] only: [Function], skip: [Function] },
     complete: [Function: complete],
     cycle: [Function: cycle],
     save: [Function: save],
     suite: [Function: suite] } }

Adding --files doesn't help. Using node -t ts-node/register instead of npc ts-node gives same error. Not sure why ts-node doesn't want to load the module.

Types are available under node_modules:

% find node_modules -path '*benny*' -name '*.d.ts'
node_modules/benny/lib/internal/common-types.d.ts
node_modules/benny/lib/internal/format.d.ts
node_modules/benny/lib/internal/getCaseResult.d.ts
node_modules/benny/lib/internal/getSummary.d.ts
node_modules/benny/lib/internal/prepareFileContent.d.ts
node_modules/benny/lib/add.d.ts
node_modules/benny/lib/complete.d.ts
node_modules/benny/lib/cycle.d.ts
node_modules/benny/lib/index.d.ts
node_modules/benny/lib/save.d.ts
node_modules/benny/lib/suite.d.ts

stephen-smith avatar Dec 14 '19 03:12 stephen-smith

I also ran into this issue in my NestJS project when I tried enabling debugging in VSCode. I managed to solve this by changing my nodemon.json to:

{
  "watch": ["src"],
  "ext": "ts",
  "ignore": ["src/**/*.spec.ts"],
  "exec": "node -r tsconfig-paths/register -r ts-node/register src/main.ts"
}

It was the part -r tsconfig-paths/register that fixed it. This parameter will convert tsconfig defined paths into absolute paths.

abinici avatar Mar 09 '20 21:03 abinici

Thank you @abinici, you are my hero. No idea how you figured that out.

MitchTalmadge avatar Mar 12 '20 11:03 MitchTalmadge

tsconfig-paths is mentioned in our README.

On Thu, Mar 12, 2020, 7:12 AM Mitch Talmadge [email protected] wrote:

Thank you @abinici https://github.com/abinici, you are my hero. No idea how you figured that out.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/TypeStrong/ts-node/issues/422#issuecomment-598131204, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAC35OHRWRDIK2Y65SPBEITRHC7SJANCNFSM4D2LJA2Q .

cspotcode avatar Mar 12 '20 12:03 cspotcode

Ah you are correct, I didn't see that at all I guess because I just assumed it already followed the tsconfig paths. For the most part in fact it seemed like they were being obeyed without the tsconfig-paths register. Maybe I am not understanding things fully. It's working now though.

MitchTalmadge avatar Mar 12 '20 12:03 MitchTalmadge

A year later, dealing with this again. This thing needs to throw a more directive error.

Am I suppose to install tslib? Maybe put some typings somewhere... what was it again? Why isn't fs already there...

SephReed avatar Apr 18 '20 02:04 SephReed

@SephReed What is your comment in relation to?

blakeembrey avatar Apr 18 '20 05:04 blakeembrey