Repository navigation
shebang: #!/usr/bin/env node — a BOM issue #464
Description
Activity
I assume the same thing happens if the file is called
blah.mjs(removing the specifics of thepackage.jsonfield from the repro)?One quick meta note is that V8 should support the shebang natively and IIRC our module implementation depends on that. So it feels really weird that it would be platform-dependent..?
Tried on my mac (version installed via
nvm, so official release build) with both.jsand.mjs. In both cases it seemed to work. Are there any other details I might have missed?$ node test.js (node:12797) ExperimentalWarning: The ESM module loader is experimental. file:///tmp/shebang/test.js darwin $ node -v v13.6.0 $ cat test.js #!/usr/bin/env node import {platform} from 'os'; console.log(import.meta.url, platform());
Ah, missed
{ type: 'commonjs' }(!). Wait - if type is CommonJS, why is it trying to run the source as a module?P.S.:
internal/modules/esm/translators.js:83on v13.6.0 is the handler for files in module format which shouldn't apply to CJS source code.Still, same result after running as CJS (removing my bad type field from package.json):
$ cat test.js #!/usr/bin/env node const {platform} = require('os'); console.log(platform()); $ node test.js darwin $ node -v v13.6.0
I haven't tried debugging this, but I remember that earlier versions of our loader included Node stripping out the shebang line; I think when V8 added support for it, Node's code was removed. So there might be a bug in V8 shebang handling.
@SMotaal does your file start with a utf8 byte order mark?
echo \uFEFF#!test | v8 V8 version 8.1.117 d8> (d8):1: SyntaxError: Invalid or unexpected token #!test ^ SyntaxError: Invalid or unexpected tokenReacted by Jan Olaf MartinIs it intended that a starting BOM would not be removed as part of the shebang?
it shouldn't be removed as part of the hashbang, but i'm surprised that node doesn't strip utf8 bom when reading source files anymore.
I am inclined to say it is an OS problem:
bom.sh#!/usr/bin/env sh echo "executed!";-
with BOM:
./bom.sh: line 1: #!/usr/bin/env: No such file or directory executed! -
without BOM:
executed!
-
which is more disturbing honestly :)
- Reacted by Saleh Abdel Motaal and Jordan Harband
I guess I used UTF-16 at some point and decided it was still to much hassle and went to UTF-8 which technically should not need BOM — ah living in the 🙃
But that issue comes up a lot apparently elsewhere too.
Okay, while we are here let's ask, folks, what is the recommended encode setting for ESM files?
- changed the title
[-]esm: `#!/usr/bin/env node` (macOS)[/-][+]esm: `#!/usr/bin/env node` — a `BOM` issue[/+]on Jan 9, 2020 - changed the title
[-]esm: `#!/usr/bin/env node` — a `BOM` issue[/-][+]shebang: `#!/usr/bin/env node` — a `BOM` issue[/+]on Jan 9, 2020 Thanks folks!
I assume everything should be utf-8 everywhere always, but it still seems like a BOM should be stripped.
Reacted by Saleh Abdel Motaal@ljharb I think the problem is the OS is not stripping it, hence never called node — https://en.wikipedia.org/wiki/Shebang_(Unix)#Magic_number
+1 on UTF-8 — just can't recall why I was inclined to add BOMs exactly (assuming it was latent from UTF-16 bugging in Windows, but since moved to UTF-8)
This issues related to the issue seen with rewire (jhnns/rewire#179)
It started occurring on node 12.16.0.Would somebody be able to clarify how this issue relates please?
@mbwhite seems like that is due to this refactoring PR being backported - https://github.com/nodejs/node/pull/27768/files.


Folks, quick question about shebang on macOS!
Package:
~/blah/blah/package.jsonModule:
~/blah/blah/blah.jsCommand:
node blah.js # in ~/blah/blahChecks:
Module:
~/blah/blah/blah.jsCommand:
node blah.js # in ~/blah/blah