Skip to content

Commit

Permalink
fix(shared): crash while resolving esm-only package (#49)
Browse files Browse the repository at this point in the history
* fix(shared): crash while resolving esm-only package

* chore: changeset
  • Loading branch information
Anber authored Feb 6, 2024
1 parent 7aa81af commit c1a83e4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-poets-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@wyw-in-js/shared': patch
---

Fix crash while resolving esm-only package. Fixes #43
21 changes: 16 additions & 5 deletions packages/shared/src/findPackageJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,29 @@ export function findPackageJSON(

return cache.get(pkgPath);
} catch (er: unknown) {
if (
typeof er === 'object' &&
er !== null &&
(er as { code?: unknown }).code === 'MODULE_NOT_FOUND'
) {
const code =
typeof er === 'object' && er !== null && 'code' in er
? er.code
: undefined;

if (code === 'MODULE_NOT_FOUND') {
if (skipPathsOptions && filename) {
return findPackageJSON(pkgName, null);
}

return undefined;
}

if (code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') {
// See https://github.com/Anber/wyw-in-js/issues/43
// `require` can't resolve ESM-only packages. We can use the `resolve`
// package here, but it does not solve all cases because `pkgName`
// can be an alias and should be resolved by a bundler. However, we can't use
// `resolve` from a bundler because it is async. The good news is that in that
// specific case, we can just ignore those packages. For now.
return undefined;
}

throw er;
}
}

0 comments on commit c1a83e4

Please sign in to comment.