Concerns with TypeScript 4.5's Node 12+ ESM Support
Problem
For TypeScript 4.5, we've added a new module mode called `node12` to better-support running ECMAScript modules in Node.js. Conceptually, the feature is simple - for whatever Node.js does, either match it or overlay something TypeScript-specific that mirrors thes same functionality. This is what TypeScript did for our initial Node.js support (i.e. `--moduleResolution node` and `--module commonjs`); however, the feature is much more expansive, and over the past few weeks, a few of us have grown a bit concerned about the complexity. I recently put together a list of user scenarios and possibly useful scripts for a few people on the team to run through, and we found a few sources of concerns. Bugs UX Ecosystem User Guidance Bugs Most complex software ships with a few bugs. Obviously, we want to avoid them, but the more complex a feature is, the harder it is to cover all the use-cases. As we get closer to our RC date, do we feel confident that what we're shipping has as few blocking bugs as possible? I would like to say we're close, but the truth is I have no idea. It feels like we'll have to keep trying the features for a bit until we don't run into anything - but we have less than 3 weeks before the RC ships. Here's a few surprising bugs that need to get fixed before I would feel comfortable shipping `node12` in stable. Code changes breaking module resolution https://github.com/microsoft/TypeScript/issues/46373 https://github.com/microsoft/TypeScript/issues
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix Module Resolution Bugs in TypeScript 4.5 Node12 ESM Support
The complexity of the new `node12` module mode in TypeScript 4.5 has introduced bugs related to module resolution, particularly when code changes affect how modules are imported and resolved. This is likely due to the interplay between TypeScript's handling of ECMAScript modules and Node.js's native module resolution, which can lead to unexpected behavior and errors.
Awaiting Verification
Be the first to verify this fix
- 1
Review and Update Module Resolution Logic
Examine the existing module resolution logic in TypeScript's compiler to identify areas where the new `node12` mode diverges from expected Node.js behavior. Update the logic to ensure compatibility with Node.js's ESM resolution rules.
typescriptconst moduleResolution = (moduleName) => { /* updated logic here */ }; - 2
Implement Comprehensive Unit Tests
Create a suite of unit tests that cover various scenarios of module resolution under the `node12` mode. Ensure tests include edge cases and complex module structures to validate that the resolution behaves as expected.
typescriptdescribe('Module Resolution Tests', () => { it('should resolve modules correctly', () => { /* test implementation */ }); }); - 3
Run Integration Tests with Sample Projects
Set up integration tests using sample projects that mimic real-world usage of TypeScript with Node.js. This will help identify any remaining bugs in module resolution and ensure that the new features work seamlessly.
bashnpm run test:integration - 4
Gather Feedback from Early Adopters
Reach out to users who have tested the `node12` mode in TypeScript 4.5 and collect feedback on any issues they encountered. Use this feedback to prioritize fixes and improvements before the RC release.
typescriptconst feedback = await getUserFeedback(); - 5
Document Known Issues and Workarounds
Create documentation that outlines known issues with the `node12` mode and provides workarounds for users. This will help manage user expectations and provide guidance on how to navigate any existing bugs.
typescriptconst documentation = generateDocumentation(knownIssues);
Validation
Confirm that all unit and integration tests pass without errors. Additionally, gather user feedback to ensure that the module resolution issues have been resolved and that users can successfully utilize the `node12` mode without encountering blocking bugs.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep