question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Node shebang line results in error since Node.js 12.16.0

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:6

github_iconTop GitHub Comments

1reaction
jakedcommented, Nov 19, 2020

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.

0reactions
copecommented, Oct 9, 2021

@mbwhite thanks for the info

is there an alternative to use?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node v12.16.0 (LTS)
This handler routes the exception asynchronously to the Symbol.for('nodejs.rejection') method if there is one, or to the 'error' event ...
Read more >
Node.js shebang - Alex Ewerlöf
Shebang. Shebang or hashbang ( #! ) is the first line of the file which tells the OS which interpreter to use. It...
Read more >
Node.js application with Heroku Scheduler having a Shebang ...
Shebang line tells which interpreter to use to run the file. Error you receive tells that node is not installed in location shebang...
Read more >
How to Easily Update Node.js to the Latest Version
There are several easy & quick ways of upgrading Node.js. Learn how to update Node.js on different OS and improve your development ...
Read more >
Update Node.js solution stack versions in Elastic Beanstalk
I receive an error when I try a manual or managed update to a newer version of a Node.js solution stack in AWS...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found