loader: fix package resolution for edge case
this commit solves a regression introduced with PR-40980.
if a resolve
call results in a script with .mjs extension the format
is automatically set to module
. This avoids the case where an additional
package.json
in the same directory as the .mjs
file would declare the
type
to commonjs
the PR also contains a new test that covers this functionality. It fixes the issue observed with https://github.com/nodejs/node/pull/41167 during backport of https://github.com/nodejs/node/pull/40980
relevant comment for this situation: https://github.com/nodejs/node/pull/41167#issuecomment-996251066
the issue was that for the case of yargs
the type was solved wrongly to commonjs
because of: this when importing yargs/helpers
.
That happened because package.json
had priority over the extension. With this PR the package.json
information only applies if the extension of the script is .js
Refs: https://github.com/nodejs/node/pull/40980 Refs: https://github.com/yargs/yargs/issues/2068