question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

baseUrl has a side effect on the used dependency export

See original GitHub issue

Is

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

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:open
  • Created 3 years ago
  • Comments:36 (26 by maintainers)

github_iconTop GitHub Comments

1reaction
jonaskellocommented, Jul 23, 2020

I did an experiment/repro here that does not involve ts-node or any bundling. It just uses tsc and node. It works without tsconfig-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 the index.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 the main field they will require index.cjs and it works. This means tsconfig-paths should also require this file but somehow it seems to require index.js instead. I think this is a bug since if there is an explicit file extension for the file in the main field it should be used as-is. (Note that the module field is not used at all by node as far as I know, it is only webapck that checks that field.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bountysource
baseUrl has a side effect on the used dependency export.
Read more >
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found