sheerlox / import-from-esm

Almost drop-in "import-from" replacement that supports loading both ESM & CJS modules
MIT License
2 stars 2 forks source link

Module subpath doesn't resolve #107

Closed lukiffer closed 5 months ago

lukiffer commented 5 months ago

Ran into an issue when using semantic-release with a shared config (CommonJS) using subpaths.

With a destination package @example/package/subpath, assuming the target file in the package @example/package exists in ./subpath/index.js, I get something like the following:

import-from-esm Trying to resolve '@example/package/subpath' from 'file:///Users/lukiffer/.npm/_npx/db57d9b03a028fc5/node_modules/semantic-release/lib/noop.js' +0ms
  import-from-esm Failed to resolve '@example/package/subpath' from 'file:///Users/lukiffer/.npm/_npx/db57d9b03a028fc5/node_modules/semantic-release/lib/noop.js': Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@example/package' imported from /Users/lukiffer/.npm/_npx/db57d9b03a028fc5/node_modules/semantic-release/lib/noop.js +0ms

If I instead move and rename the target file from ./subpath/index.js to ./subpath.cjs this works as expected.

I'm not sure if this is something that import-from previously masked or if ./subpath/index.js should actually be checked, and if so why it's failing to find the root package.

sheerlox commented 5 months ago

Hi @lukiffer, sorry you're encountering an issue with this package!

Could you please run the failing command with the DEBUG=import-from-esm env variable? This will log every path it has tried to resolve.

lukiffer commented 5 months ago

Here's the actual output: https://gist.github.com/lukiffer/fd3f0b38960555b7bb808fdb5edc42f7

(I think) the issue is with the first resolve attempt:

Trying to resolve '@bdrk/semantic-release-config/npm'

In this case, a file does in fact exist at node_modules/@bdrk/semantic-release-config/npm/index.js but is throwing ERR_MODULE_NOT_FOUND.

Moving the file to ./npm.js (with type="commonjs" in package.json) or moving it to ./npm.cjs both work fine. So it seems like it's either not trying the index.js default, or having some trouble locating it.

sheerlox commented 5 months ago

Ok, I found the issue. I'm working on a fix and should be releasing that in the next few hours, will keep you updated. Thanks for the heads up!

sheerlox commented 5 months ago

:tada: This issue has been resolved in version 1.3.4 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

sheerlox commented 5 months ago

Renovate should update the dependency on the various @semantic-release repos in the next few hours!

Feel free to reopen if you're still getting an issue after upgrading :smile:

EDIT: forgot @semantic-release repositories didn't use pinned versions, so you just need to update your lockfile with npm update import-from-esm, but it seems you're using npx to run semantic-release so none of the above should be needed :)