jhnns / rewire

Easy monkey-patching for node.js unit tests
MIT License
3.08k stars 127 forks source link

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

Closed Ari-H closed 2 years ago

Ari-H commented 4 years ago

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: https://github.com/nodejs/node/commit/bcd27f7300 https://github.com/nodejs/node/commit/1c50714729

mbwhite commented 4 years ago

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 commented 3 years ago

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

sseide commented 3 years ago

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 commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

@mbwhite thanks for the info

is there an alternative to use?