Repository navigation
Run script from the console with UTF-8 with BOM #39111
Description
Activity
- changed the title
[-]Run Script from the console with UTF-8 with BOM[/-][+]Run script from the console with UTF-8 with BOM[/+]on Jun 22, 2021 This is expected behavior. See #27768
I see that one of the
/test/fixturesfolder works well.curl -O https://raw.githubusercontent.com/nodejs/node/master/test/fixtures/utf8-bom.js
Hexdump of
utf8-bom.js:hexdump -C utf8-bom.js 00000000 ef bb bf 6d 6f 64 75 6c 65 2e 65 78 70 6f 72 74 |...module.export| 00000010 73 20 3d 20 34 32 3b 0a |s = 42;.| 00000018
- The file
utf8-bom.jsIt works well. - The file
utf8-bom-shebang-shebang.jsit does not work. - The file
utf8-shebang-bom.jsalso, it works perfect.
Hexdump of
utf8-shebang-bom.jshexdump -C utf8-shebang-bom.js 00000000 23 21 73 68 65 62 61 6e 67 0a ef bb bf 6d 6f 64 |#!shebang....mod| 00000010 75 6c 65 2e 65 78 70 6f 72 74 73 20 3d 20 34 32 |ule.exports = 42| 00000020 3b |;| 00000021
Hexdump replicating my script:
hexdump -C ./test_she.js 00000000 ef bb bf 23 21 73 68 65 62 61 6e 67 0a 6d 6f 64 |...#!shebang.mod| 00000010 75 6c 65 2e 65 78 70 6f 72 74 73 20 3d 20 34 32 |ule.exports = 42| 00000020 3b 0a |;.|
This fails while the one in fixtures works well.
- The file
- addedmoduleIssues and PRs related to the module subsystem.Issues and PRs related to the module subsystem.
on Sep 25, 2021 The shebang must be the first two bytes in the file to be interpreted by the kernel, which the BOM prevents. 1
Footnotes
I don't think node's behavior is unreasonable - you put in a sheband so you can run a script from the command line; your OS doesn't accept a BOM + shebang, therefore it doesn't make sense for node to do so - but the error message is somewhat inscrutable and could be improved. Someone want to send a PR?
You could argue node could strip the BOM to no ill effect but then you get an inscrutable error message when invoking the script from the command line. That's not an improvement.
Reacted by Mr Beedell, Roke Julian Lockhartgithub-actions commented
on Jun 27, 2026 on Jun 27, 2026 – with GitHub ActionsContributorMore actionsThis issue has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.Reacted by Mr Beedell, Roke Julian Lockhart- addedstaleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.
on Jun 27, 2026 github-actions commented
on Jul 28, 2026 on Jul 28, 2026 – with GitHub ActionsContributorMore actionsThis issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 120 days).
If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.Reacted by Mr Beedell, Roke Julian Lockhart
From Linux I make a script with shebang:
#!/bin/node // Code...But when I transformed it to UTF-8 with BOM it does not run.
line 1: ���#!/bin/node: No such file or directorySeeing this I just realized that it is a problem of the terminal and not of Node, but if I run it with Node, the problem is still.
Answer in LxTerminal:
���#!/bin/nodeAnswer on Sakura:
#!/bin/node ^ SyntaxError: Invalid or unexpected token at Object.compileFunction (node:vm:353:18) at wrapSafe (node:internal/modules/cjs/loader:1039:15) at Module._compile (node:internal/modules/cjs/loader:1073:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10) at Module.load (node:internal/modules/cjs/loader:989:32) at Function.Module._load (node:internal/modules/cjs/loader:829:14) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12) at node:internal/main/run_main_module:17:47