--input-type 标志
【--input-type flag】
当设置了 --input-type=module 标志时,作为 --eval(或 -e)参数传入的字符串,或通过 STDIN 管道传入 node 的字符串,将被视为 ES 模块。
【Strings passed in as an argument to --eval (or -e), or piped to node via
STDIN, are treated as ES modules when the --input-type=module flag
is set.】
node --input-type=module --eval "import { sep } from 'node:path'; console.log(sep);"
echo "import { sep } from 'node:path'; console.log(sep);" | node --input-type=module 为了完整性,还有 --input-type=commonjs,用于将字符串输入明确地作为 CommonJS 运行。如果未指定 --input-type,这是默认行为。
【For completeness there is also --input-type=commonjs, for explicitly running
string input as CommonJS. This is the default behavior if --input-type is
unspecified.】