Skip to content

Commit

Permalink
fix: GH-178 import error on shebang modules
Browse files Browse the repository at this point in the history
As of NodeJS 12.16.0, importing modules with shebang declarations failed.
This commit checks the first line for shebang declarations and removes them
before trying to load the module.
  • Loading branch information
breautek committed Mar 31, 2020
1 parent dea7b22 commit 0244069
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/moduleEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ var moduleWrapper0 = Module.wrapper[0],
// errors anyway, it's probably still a reasonable trade-off.
// Test the regular expresssion at https://regex101.com/r/dvnZPv/2 and also check out testLib/constModule.js.
matchConst = /(^|\s|\}|;)const(\/\*|\s|{)/gm,
// Required for importing modules with shebang declarations, since NodeJS 12.16.0
shebang = /^(#!).+/,
nodeRequire,
currentModule;

Expand Down Expand Up @@ -123,7 +125,9 @@ function jsExtension(module, filename) {

_compile.call(
module,
content.replace(matchConst, "$1let $2"), // replace const with let, while maintaining the column width
content
.replace(shebang, '') // Remove shebang declarations
.replace(matchConst, "$1let $2"), // replace const with let, while maintaining the column width
filename
);
};
Expand Down

0 comments on commit 0244069

Please sign in to comment.