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

ts-node won't resolve unpostfixed file names

Open fresheneesz opened this issue 3 years ago • 4 comments

Search Terms

file extension resolve

Expected Behavior

npx ts-node --esm test should run the test.ts file.

Actual Behavior

Cannot find module '/<root>/test/test'

Steps to reproduce the problem

  1. Create a file test.ts and put anything in it. Or nothing.
  2. Run npx ts-node --esm test

Minimal reproduction

See above.

Specifications

  • ts-node version: v10.9.1
  • node version: v19.3.0
  • TypeScript version: tsc version 4.9.4
  • tsconfig.json, if you're using one:
{
	"compilerOptions": {
		"target": "es2021",
		"lib": [
			"es2021"
		] ,
		"jsx": "react" ,
		/* Modules */
		"module": "es2022" ,
		"moduleResolution": "node",
		"types": [
			"@cloudflare/workers-types",
			"jest"
		] ,
		"resolveJsonModule": true ,

		/* JavaScript Support */
		"allowJs": true ,
		"checkJs": false ,

		"noEmit": true ,

		/* Interop Constraints */
		"allowSyntheticDefaultImports": true ,
		"forceConsistentCasingInFileNames": true ,

		/* Type Checking */
		"strict": true,

		/* Completeness */
		"skipLibCheck": true
	}
}

  • package.json:
{"type": "module"}
  • Operating system and version: Windows 10
  • If Windows, are you using WSL or WSL2?: WSL2

fresheneesz avatar Jan 03 '23 03:01 fresheneesz

I'm seeing similar behavior for imports as well. I have a file wtf.ts with the contents:

import { test } from "./test";
console.log(test);

and test.ts contains

export const test = "???";

And running this with npx ts-node --esm -T wtf.ts gives me CustomError: Cannot find module '/<root>/test/test' imported from /<root>/test/wtf.ts.

However if I add a .ts extension to the import, it works (it prints out "???" as I would expect):

import { test } from "./test.ts";
console.log(test);

But then my IDE gives me typescript warnings that the .ts shouldn't be there. Well, I agree.

fresheneesz avatar Jan 03 '23 04:01 fresheneesz

I'm seeing similar behavior for imports as well. I have a file wtf.ts with the contents:

import { test } from "./test";
console.log(test);

and test.ts contains

export const test = "???";

And running this with npx ts-node --esm -T wtf.ts gives me CustomError: Cannot find module '/<root>/test/test' imported from /<root>/test/wtf.ts.

However if I add a .ts extension to the import, it works (it prints out "???" as I would expect):

import { test } from "./test.ts";
console.log(test);

But then my IDE gives me typescript warnings that the .ts shouldn't be there. Well, I agree.

same issue here

galaxynova1999 avatar Feb 08 '23 10:02 galaxynova1999

Same issue here

HUANGXUANKUN avatar Mar 27 '23 07:03 HUANGXUANKUN

I'm seeing similar behavior for imports as well. I have a file wtf.ts with the contents:

import { test } from "./test";
console.log(test);

and test.ts contains

export const test = "???";

And running this with npx ts-node --esm -T wtf.ts gives me CustomError: Cannot find module '/<root>/test/test' imported from /<root>/test/wtf.ts. However if I add a .ts extension to the import, it works (it prints out "???" as I would expect):

import { test } from "./test.ts";
console.log(test);

But then my IDE gives me typescript warnings that the .ts shouldn't be there. Well, I agree.

same issue here

setting "experimentalSpecifierResolution": "node" will solve the issue.

{
  "compilerOptions": {
    "module": "esnext",
    "moduleResolution": "node",
    "target": "ES2017",
    "resolveJsonModule": true,
    "forceConsistentCasingInFileNames": true
  },
  "ts-node": {
    "experimentalSpecifierResolution": "node",
    "pretty": true,
    "transpileOnly": true,
    "esm": true
  }
}

HUANGXUANKUN avatar Mar 27 '23 08:03 HUANGXUANKUN