--input-type 标志
作为参数传给 --eval(或 -e),或通过 STDIN 管道传输到 node 的字符串,在设置 --input-type=module 标志时被视为 ES 模块。
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,这是默认行为。
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=moduleFor completeness there is also --input-type=commonjs, for explicitly running
string input as CommonJS. This is the default behavior if --input-type is
unspecified.