Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node shebang line results in error since Node.js 12.16.0 #178

Closed
Ari-H opened this issue Mar 19, 2020 · 6 comments · Fixed by #179
Closed

Node shebang line results in error since Node.js 12.16.0 #178

Ari-H opened this issue Mar 19, 2020 · 6 comments · Fixed by #179

Comments

@Ari-H
Copy link

Ari-H commented Mar 19, 2020

Using rewire@5.0.0. Modules including shebang line can no longer be loaded with rewire.

Files to test the issue:

$ cat test.js 
const rewire = require('rewire');
rewire('./dependency.js');

$ cat dependency.js 
#!/usr/bin/env node
console.log('Hello world!');

With Node.js 12.15.0 it still works:

$ node --version
v12.15.0

$ node test.js 
Hello world!

Starting with Node.js 12.16.0 it fails:

$ node --version
v12.16.0

$ node test.js 
/tmp/node/dependency.js:1
(function (exports, require, module, __filename, __dirname) { var clearInterval = global.clearInterval; var clearTimeout = global.clearTimeout; var setInterval = global.setInterval; var setTimeout = global.setTimeout; var queueMicrotask = global.queueMicrotask; var clearImmediate = global.clearImmediate; var setImmediate = global.setImmediate; (function () { #!/usr/bin/env node
                                                                                                                                                                                                                                                                                                                                                                         ^

SyntaxError: Invalid or unexpected token
    at new Script (vm.js:88:7)
    at createScript (vm.js:263:10)
    at Object.runInThisContext (vm.js:311:10)
    at wrapSafe (internal/modules/cjs/loader.js:1058:15)
    at Module._compile (internal/modules/cjs/loader.js:1121:27)
    at Module.module._compile (/tmp/node/node_modules/rewire/lib/moduleEnv.js:124:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
    at Object.jsExtension (/tmp/node/node_modules/rewire/lib/moduleEnv.js:132:24)
    at Module.load (internal/modules/cjs/loader.js:1001:32)
    at Object.load (/tmp/node/node_modules/rewire/lib/moduleEnv.js:55:18)

There has been some changes on shebang handling in Node.js 12.16.0:
nodejs/node@bcd27f7300
nodejs/node@1c50714729

breautek added a commit to breautek/rewire that referenced this issue Mar 31, 2020
breautek added a commit to breautek/rewire that referenced this issue Mar 31, 2020
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.
@mbwhite
Copy link

mbwhite commented Apr 2, 2020

To add that I've encountered this as well, and that module that is being rewired doesn't have to be the one with the shebang.

Node 12.15.0 is ok, but 12.16.x has the failure.
Rewire 4.0.1 also will fail.

Great to see a fix yesterday @jhnns - do you have an expected re-release date?

@jaked
Copy link

jaked commented Nov 19, 2020

hi, came across this bug when I was debugging a similar issue with Electron. I filed this Node bug nodejs/node#36173. I suspect you are running into the same thing because rewire monkey-patches Module.wrapper.

@sseide
Copy link

sseide commented Jun 10, 2021

for us it breaks loading plant-uml package to auto-generate uml images while running jsdoc...
No nice images anymore in docs, just a sea of gray :-(

@cope
Copy link

cope commented Oct 8, 2021

Has this been fixed?
'Cause it just happened with node 16.10.0, typescript ^4.0.3 and rewire ^5.0.0 😞

; (function () { #!/usr/bin/env node
                 ^
SyntaxError: Invalid or unexpected token

@mbwhite
Copy link

mbwhite commented Oct 9, 2021

@cope there is a PR for this, but it's never been merged.

We abandoned using this module. It doesn't appear to be maintained any longer

@cope
Copy link

cope commented Oct 9, 2021

@mbwhite thanks for the info

is there an alternative to use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants