Commit ed3278d
module: fix crash on multiline named cjs imports
The node process crashes when trying to parse a multiline import
statement for named exports of a CommonJS module:
TypeError: Cannot read property '0' of null
at ModuleJob._instantiate (internal/modules/esm/module_job.js:112:77)
at async ModuleJob.run (internal/modules/esm/module_job.js:137:5)
at async Loader.import (internal/modules/esm/loader.js:165:24)
at async rejects.name (file:///***/node/test/es-module/test-esm-cjs-named-error.mjs:56:3)
at async waitForActual (assert.js:721:5)
at async rejects (assert.js:830:25),
The reason is that the regexp that is currently used to decorate the
original error fails for multi line import statements.
Unfortunately the undecorated error stack only contains the single line
which causes the import to fail:
file:///***/node/test/fixtures/es-modules/package-cjs-named-error/multi-line.mjs:2
comeOn,
^^^^^^
SyntaxError: The requested module './fail.cjs' does not provide an export named 'comeOn'
at ModuleJob._instantiate (internal/modules/esm/module_job.js:98:21)
at async ModuleJob.run (internal/modules/esm/module_job.js:141:5)
at async Loader.import (internal/modules/esm/loader.js:165:24)
at async rejects.name (file:///***/node/test/es-module/test-esm-cjs-named-error.mjs:56:3)
at async waitForActual (assert.js:721:5)
at async rejects (assert.js:830:25)
Hence, for multiline import statements we cannot create an equivalent
piece of code that uses default import followed by an object
destructuring assignment.
In any case the node process should definitely not crash. So until we
have a more sophisticated way of extracting the entire problematic
multiline import statement, show the code example only for single-line
imports where the current regexp approach works well.
Refs: #35259
PR-URL: #35275
Backport-PR-URL: #35385
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>1 parent 89a58f6 commit ed3278dCopy full SHA for ed3278d
File tree
Expand file treeCollapse file tree
4 files changed
+31
-8
lines changedOpen diff view settings
Filter options
- lib/internal/modules/esm
- test
- es-module
- fixtures/es-modules/package-cjs-named-error
Expand file treeCollapse file tree
4 files changed
+31
-8
lines changedOpen diff view settings
Collapse file
lib/internal/modules/esm/module_job.js
Copy file name to clipboardExpand all lines: lib/internal/modules/esm/module_job.js+14-7Lines changed: 14 additions & 7 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
107 | 107 | |
108 | 108 | |
109 | 109 | |
110 | | - |
111 | | - |
112 | | - |
113 | 110 | |
114 | 111 | |
115 | 112 | |
116 | | - |
117 | | - |
118 | | - |
119 | | - |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
120 | 127 | |
121 | 128 | |
122 | 129 | |
|
Collapse file
test/es-module/test-esm-cjs-named-error.mjs
Copy file name to clipboardExpand all lines: test/es-module/test-esm-cjs-named-error.mjs+11Lines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
10 | 10 | |
11 | 11 | |
12 | 12 | |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
13 | 17 | |
14 | 18 | |
15 | 19 | |
| ||
52 | 56 | |
53 | 57 | |
54 | 58 | |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
55 | 66 | |
56 | 67 | |
57 | 68 | |
|
Collapse file
test/fixtures/es-modules/package-cjs-named-error/fail.cjs
Copy file name to clipboard+2-1Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
1 | 1 | |
2 | | - |
| 2 | + |
| 3 | + |
3 | 4 | |
Collapse file
test/fixtures/es-modules/package-cjs-named-error/multi-line.mjs
Copy file name to clipboard+4Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
0 commit comments