Closed NikolayMakhonin closed 2 years ago
I added the .js
extension to the import path and it worked.
import {test} from './test.js'
this is confused me, because js file is not exists, but tsc
is worked and IDE code inspection is worked
I'm facing the same issue as well.
I try to use ts-node --esm
with a large existing codebase, and I don't want to add .js
to each import.
Does anyone think of a possible solution ?
I don't want to add .js to each import.
Check out: https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#mandatory-file-extensions https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#customizing-esm-specifier-resolution-algorithm https://typestrong.org/ts-node/docs/options#experimentalspecifierresolution https://github.com/TypeStrong/ts-node/pull/1813/files#diff-8e9a64b8e2bd566ae76b953a77e069f6ca2ac0aa74af439287107aecfb42bb79R406
Many people will tell you to strongly consider adding .js
to each import, but ultimately, the decision is yours to make.
Thank you all for this workaround!
I just experienced the same problem (CustomError: Cannot find module
) with ts-node v10.9.1 and ts-node-esm
execution.
Changing my TypeScript import from import { getDistanceReport } from "./getDistanceReport";
to import { getDistanceReport } from "./getDistanceReport.js";
helped.
Because I found it weird to use a .js
extension when importing a TS file, I decided to enable the allowImportingTsExtensions compiler flag. This at least allowed me to add a .ts
extension.
Example:
import { getDistanceReport } from "./getDistanceReport.ts";
I ran into this issue today as well. The .js
solution works, but it's clearly not ideal. I am not sure if this is a bug with ts-node
, or an issue with my repo. It's a large components repo that has a small node scripts section that I'm converting to TypeScript.
I also thought that the .js
extension solution isn't ideal but it is actually officially recommended:
relative import paths need full extensions (e.g we have to write import "./foo.js" instead of import "./foo") [...] bar.ts will instead have to import from ./foo.js. [...] This might feel a bit cumbersome at first, but TypeScript tooling like auto-imports and path completion will typically just do this for you.
Source: https://www.typescriptlang.org/docs/handbook/esm-node.html#type-in-packagejson-and-new-extensions
My .ts
solution was not ideal as it only works with allowImportingTsExtensions which is only allowed when --noEmit
or --emitDeclarationOnly
is set. That being said, .js
it is. 🌻
I added the
.js
extension to the import path and it worked.
import {test} from './test.js'
this is confused me, because js file is not exists, but
tsc
is worked and IDE code inspection is worked
Thanks for the workaround! Non-obvious, and saves me from much uglier workaround.
There is new moduleResolution
now: "bundler".
( https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#moduleresolution-bundler )
this might help some of you
i personally no more use ts-node but use vite-node currently tho..
Search Terms
esm with package type module. CustomError: Cannot find module
TS2835, TS2691
Expected Behavior
It should work with es modules without errors.
Actual Behavior
Steps to reproduce the problem
Clone my repo and run
npm test
Minimal reproduction
My repo is very minimal
Specifications