Inconsistent module resolution with typescript under node/web
See original GitHub issuematch-sorter
version:2.2.1node
version:8.2.1npm
(oryarn
) version:yarn 1.0.2/npm 5.8.0
What you did: Basically trying to use match-sorter with typescript in browser and node.
What happened:
An error is thrown when trying to test a file that imports match-sorter.
Reproduction repository:
https://github.com/yakirn/matchsorter-ts
Problem description:
I used a very basic setup. create-react-app with typescript as described here.
I added basic match-sorter code to App.tsx
.
When running using yarn start
a browser pops up and everything works as expected.
But when trying to test the component using yarn test
an error is thrown:
Error: Uncaught [TypeError: match_sorter_1.default is not a function]
Suggested solution:
I’m no sure why, but import matchSorter from 'match-sorter';
works in the browser but not in node.
while import * as matchSorter from 'match-sorter';
works in node, but not in the browser.
I would love to help solve this issue, and later add types definition, but I guess there’s a problem with the bundler of this project, and I can use some guidance. Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:7 (5 by maintainers)
I’m guessing this is happening because node is resolving your require/import to the
package.json
main
field and webpack is resolving your require/import to thepackage.json
module
field. These are slightly different to facilitate an improved experience for people using CommonJS and UMD. I’m planning on changing this, and that will be a breaking change which should have fairly minimal impact so hopefully I can get to it soon.🎉 This issue has been resolved in version 4.0.0 🎉
The release is available on:
npm package (@latest dist-tag)
Your semantic-release bot 📦🚀