Commit 06fde1d
committed
esm: fix relative require in non-file CJS source
When a load hook provides the source of a CommonJS module, the require
calls in that source are meant to go through the ESM loader so that the
registered hooks apply to them, as documented for the load hook.
That did not happen when the module had a URL that is not a file:. The
require function built for those modules called
Module._resolveFilename() before handing the specifier to the ESM
resolver, only to look at the extension of the resulting path and decide
whether to treat the request as JSON or as a native addon. Because the
CJS resolver works on file paths, it cannot resolve a relative specifier
against a referrer such as custom:cjs, so it threw MODULE_NOT_FOUND and
the ESM resolution below it was never reached. The resolve hook was not
called at all.
Keep that lookup, since the JSON and .node branches depend on it, but
let it fail when the referrer is not a file and leave the specifier for
the ESM resolver to handle.
A specifier that the hooks do not claim now fails with
ERR_UNSUPPORTED_RESOLVE_REQUEST, which reports that the referrer scheme
is not hierarchical, instead of MODULE_NOT_FOUND coming from the CJS
resolver.
Signed-off-by: Avocado <ujubongbong@gmail.com>1 parent 7b6b21a commit 06fde1d
3 files changed
Lines changed: 67 additions & 12 deletions
File tree
- lib/internal/modules/esm
- test
- es-module
- fixtures/es-module-loaders
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
143 | 157 | | |
144 | | - | |
145 | 158 | | |
146 | 159 | | |
147 | 160 | | |
| |||
Lines changed: 17 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
0 commit comments