Skip to content

Run script from the console with UTF-8 with BOM #39111

Description

@arteze

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 directory

Seeing 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.

node script.js

Answer in LxTerminal:

���#!/bin/node

Answer 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

Activity

  1. 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
  2. targos commented on Jun 22, 2021

    @targos
    Member

    This is expected behavior. See #27768

  3. arteze commented on Jun 22, 2021

    @arteze
    Author

    I see that one of the /test/fixtures folder 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.js It works well.
    • The file utf8-bom-shebang-shebang.js it does not work.
    • The file utf8-shebang-bom.js also, it works perfect.

    Hexdump of utf8-shebang-bom.js

    hexdump -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.

  4. targos commented on Aug 9, 2021

    @targos
    Member

    I don't really know how to label this or who to ping, except @devsnek who worked on #27768

  5. added
    moduleIssues and PRs related to the module subsystem.
    on Sep 25, 2021
  6. beedell-roke commented on May 24, 2025

    @beedell-roke

    The shebang must be the first two bytes in the file to be interpreted by the kernel, which the BOM prevents. 1

    Footnotes

    1. stackoverflow.com/a/19065575 ↩

  7. bnoordhuis commented on May 24, 2025

    @bnoordhuis
    Member

    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.

  8. github-actions commented on Jun 27, 2026

    @github-actions
    Contributor

    This 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.

  9. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jun 27, 2026
  10. github-actions commented on Jul 28, 2026

    @github-actions
    Contributor

    This 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.

  11. beedell-roke commented on Jul 28, 2026

    @beedell-roke
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    moduleIssues and PRs related to the module subsystem.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions