FIX: When using ESM, resolve Typescript imports using .js extension #345
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When using ESM imports and Typescript, the convention that the typescript team is pushing is to import typescript
.ts
files using.js
file extension when using ESM modules in Node16 or NodeNext.See this issue for more background: #TypeStrong/ts-loader/issues/111
However, older version of
webpack
(prior to v.5.74.0) could not resolve these typescript files with.js
extension.#Environment
ESM Modules, NodeNext
Observed Behaviour
ERROR in ../handler.ts 5:32-66
Module not found: Error: Can't resolve './balancesController.js' in '\sigma'
// balancesController file is named balancesController.ts
How to Fix
webpack
bundled withserverless-bundle
can resolve these filenames sincewebpack
version v5.74.0, which gets included in theserverless-bundle
repo. However, it requires some small modification to thewebpack.config.js
file, which is not exposed onserverless-bundle
. Namely, we need to add an extension alias tomodule.exports.resolve
I have to add these lines in manually to my node_modules for serverless-bundle each time. Think will be relevant to have in the base configuration as more people may be using ESM modules.