ts-loader: Module not found for TypeScript 4.7

Expected Behaviour

Able to bundle.

Actual Behaviour

errors such as:

Module not found: Error: Can't resolve './tersible.js'

Steps to Reproduce the Problem

In TypeScript 4.7, the NodeNext module resolution requires the source code to specify the extension as .js. e.g.:

// index.ts
export * from './tersible.js' // for `tersible.ts`

For ts-jest on the jest side, it solves that by doing a transformation:

  moduleNameMapper: {
    '^(\\.{1,2}/.*)\\.js$': '$1',
  },

Maybe something similar is needed? Would be great to add a section in the readme specifically for that.

Location of a Minimal Repository that Demonstrates the Issue.

Not a minimal repo, but here is an example: https://github.com/unional/tersify. Run yarn bundle in the repo

šŸŒ·

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 54 (51 by maintainers)

Commits related to this issue

Most upvoted comments

https://github.com/justland/just-web/pull/58

One update. After I add the cjs/package.json workaround, at least the CommonJS side resolution is working fine. Itā€™s not directly related to this issue, but it shows that having due-export (CJS + ESM/NodeNext) able to work at the end application.

I have published color-map with a few variations to test this:

upgrade to 4.7.3 (including test pack) complete

Iā€™m open to adjusting this - I mostly care that code is formatted consistently. If thereā€™s DX tweaks you have in mind we can look at it.

#1477 is the PR for that.

https://github.com/TypeStrong/ts-loader/issues/1464

It looks like the npm that ships with node 18 had some breaking changes. We use yarn 1 for ts-loader anyway, so Iā€™d advise sticking with that

image

šŸ¤£ thatā€™s why. Only running comparison tests on Windows. I was running yarn test, i.e. also running execution-tests

I see. I can take a look at it. šŸ˜ƒ

Sounds good - when you get to it, feel free to stick up a work in progress PR with broken tests if needs be. Will be happy to pair on this

Apologies if I only reply occasionally - Iā€™m on holiday

NP. That explains all those nice pics. šŸŒ·

Yeah, I can give it a shot. But need to handle a few things first. Will see if I can get to it. šŸ˜ƒ

Roughly the three major bits of NodeNext / mts / cts support in ts-node were:


Replace nodeā€™s resolver with one that checks what I call ā€œalternative file extensions.ā€ Every time nodeā€™s resolver would check for foo.js, also check for the alternatives: foo.ts, foo.tsx, foo.jsx


Teach transpiler codepaths to emit Typescriptā€™s 2x new flavors of module:

  • nodenext-flavored CommonJs, like commonjs emit except that now dynamic imports are preserved
  • nodenext-flavored ESM, like EsNext emit except that now import foo = require('foo') statements automatically use createRequire to gin up a require function

ts.transpileModule does not expose this functionality; cannot be used


Teach transpiler codepaths to classify files as either CJS or ESM using the same rules as NodeNext

  • So, use file extension and contextual package.json to decide which of the 2x flavors to emit
  • TypeScript relies on its own resolver to do this classification; transpile-only codepaths need to reimplement it