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.

ESM should require file extensions

See original GitHub issue

🐛 Bug Report

import t from './file';
import d from './directory/'

These should both fail to resolve, but since we use the same resolver as for CJS, it doesn’t, and the import will work while it would have failed at runtime. This is also the case for dynamic imports.

This might need some support in resolve whenever they add support for ESM? https://github.com/browserify/resolve/issues/222

Place where we call resolve in jest: https://github.com/facebook/jest/blob/c024dec130d9914dcc3418ea74c26f667db3dbfa/packages/jest-runtime/src/index.ts#L395-L398 https://github.com/facebook/jest/blob/c024dec130d9914dcc3418ea74c26f667db3dbfa/packages/jest-runtime/src/index.ts#L423

Node docs: https://nodejs.org/api/esm.html#esm_mandatory_file_extensions Node issue for better errors: https://github.com/nodejs/node/issues/30603

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
SimenBcommented, May 29, 2020

--experimental-specifier-resolution=node is the flag. And yeah, we’d need to keep the logic around for that case… Not sure how to best detect if it has been passed. It’s not present in process.argv so we might need to have our own option for this 🙁

1reaction
cspotcodecommented, Oct 5, 2021

Bundlers are effectively a runtime target. I simply mean that, in terms of the TS team’s decision-making, it is technically valid ESM, which is why they’re supporting it. It’s not merely a backwards-compat issue. A lot of the ecosystem’s problems tend to get blamed on TS, when it’s actually TS playing along with the ecosystem’s desire to be custom at every turn.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Explicit ESM in Node.js with .mjs - DEV Community ‍ ‍
The Quick Answer. The straightforward answer to this is that having different file extensions allows you to be explicit in how you want...
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
Mandatory file extensions#​​ A file extension must be provided when using the import keyword to resolve relative or absolute specifiers. Directory indexes (e.g. ......
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
In turn, TypeScript supports two new source file extensions: .mts and .cts . ... You can read more about ESM/CommonJS interop in Node.js...
Read more >
Why is Javascript module with ".js" file extension not found on ...
In ES Modules, file extension is mandatory, so you cannot omit the .js file extension like you can in CommonJS.
Read more >
tsc-esm-fix - npm
Moreover, if understand TS/49271 correctly, nodenext + pkg.json type: module requires the .js extension in .d.ts files of external ESM ...
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