baseUrl has a side effect on the used dependency export
See original GitHub issueIs
If I use ts-node together with tsconfig-paths/register and a tsconfig baseUrl of ./node_modules
the runtime seems to “ignore” package.json exports of dependencies and uses the wrong file. As the bottom example shows it uses colorette/index.js
but it should use colorette/index.cjs
.
Should
It should use the correct dependency dist as defined by its package.json exports, so for colorette it should use colorette/index.cjs
.
Reproduction
- tsconfig-paths-exports-repro-master.zip
npm install && npm start
Trace
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: xyz/node_modules/colorette/index.js
require() of ES modules is not supported.
require() of xyz/node_modules/colorette/index.js from xyz/node_modules/autoprefixer/lib/autoprefixer.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from xyz/node_modules/colorette/package.json.
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1217:13)
at Module.load (internal/modules/cjs/loader.js:1050:32)
at Function.Module._load (internal/modules/cjs/loader.js:938:14)
at Module.require (internal/modules/cjs/loader.js:1090:19)
at require (internal/modules/cjs/helpers.js:75:18)
at Object.<anonymous> (xyz/node_modules/autoprefixer/lib/autoprefixer.js:5:17)
at Module._compile (internal/modules/cjs/loader.js:1201:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1221:10)
at Module.load (internal/modules/cjs/loader.js:1050:32)
at Function.Module._load (internal/modules/cjs/loader.js:938:14)
Background:
- At first I thought this is related to TypeStrong/ts-node#935 / TypeStrong/ts-node#1007 but after working out a reproduction case I figured out that the error only happens with tsconfig-paths and the mentioned baseUrl value 🙈.
- The reason why I used a baseUrl of “./node_modules” in the first place was because my root folder has some folders with the same name as some dependencies, a baseUrl of “.” resulted in typescript importing my /xyz instead of /node_modules/xyz 😉.
Issue Analytics
- State:
- Created 3 years ago
- Comments:36 (26 by maintainers)
Top Results From Across the Web
TSConfig Reference - Docs on every TSConfig option
A module file is a file that has imports and/or exports. ... Base Url - baseUrl ... are never used. This can cause...
Read more >Jest + Typescript + Absolute paths (baseUrl) gives error
Has any one been able to unit test his typescript project with Jest and absolute path? or is this a known bug? Since...
Read more >Effects - OVERMIND
A typical side effect would be an HTTP request or talking to localStorage. ... For example you want to introduce caching or a...
Read more >JavaScript modules - MDN Web Docs
Node.js has had this ability for a long time, and there are a ... Use of native JavaScript modules is dependent on the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@katywings It is already covered by this test:
https://github.com/dividab/tsconfig-paths/blob/2461cc9c0aa6f02f27c679d1344adaf35ff05cf7/test/data/match-path-data.ts#L128-L142
I did an experiment/repro here that does not involve
ts-node
or any bundling. It just usestsc
andnode
. It works withouttsconfig-paths
loaded but not with it.Looking into
colorette
I found it has"type": "module"
set in package.json. That means that every.js
file should be treated as a ESM module. So theindex.js
file is in ESM format. To be backwards compatible with the old node resolution it also has"main": "index.cjs"
. Since older versions of node only check themain
field they will requireindex.cjs
and it works. This means tsconfig-paths should also require this file but somehow it seems to requireindex.js
instead. I think this is a bug since if there is an explicit file extension for the file in themain
field it should be used as-is. (Note that themodule
field is not used at all bynode
as far as I know, it is only webapck that checks that field.)