Skip to content

error message disappeared between v21 and v22 #57655

Open
@allan-bonadio

Description

@allan-bonadio

================================ Problem 1: missing error message
I had a file named 'ff':

#!/usr/bin/env node
import fs from 'node:fs';
console.log(`starting ff cmd`)

I ran it with node v22.9.0, and there was no response. No errror message, no starting message. Debugger breakpoints I'd set didn't break, because the program never started. (this took me like an hour to figure out.) Now I switch it over to v21.7.3 and I got the message

(node:62342) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/opt/GLMR5/gscripts/ff:4
import fs from 'node:fs';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:128:18)
    at wrapSafe (node:internal/modules/cjs/loader:1279:20)
    at Module._compile (node:internal/modules/cjs/loader:1331:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
    at Module.load (node:internal/modules/cjs/loader:1205:32)
    at Module._load (node:internal/modules/cjs/loader:1021:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
    at node:internal/main/run_main_module:28:49

Node.js v21.7.3

The message is also missing with v23.10.0. So, the first problem is, the error message went away, leaving no clue as to what the problem was. I'm sure you guys can fix it!

================================ Problem 2: no way to make a command line command that's esm
Then I tried to fix it with a command line option:

node --experimental-default-type=module  ff

In v23, a message says to not use --experimental-default-type=module
I also tried node --import=fs ff because somewhere it said that an --import would implicitly set it to ESM module. Didn't work.
and node --input-type=module ff didn't work.

I know I can set the suffix to .mjs and it'll work, but that's not a way to make a tidy command for end users. It makes the command look like a hack. You can make shell scripts and php executables without suffixes with the #! convension, even JavaScript if it's written in cjs, but not JS modules. This is NOT in the middle of a node package, this is just a small program that should be runable anywhere. "ff.mjs" is six characters long, whereas "ff" is two.

I can't find any other way to make a command-line command that's an ESM module. There should be such a way.Thanks for reading this far!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions