使用Bash heredoc在Bash终端中运行ES6代码 [英] Run ES6 code in Bash terminal with Bash heredoc

查看:71
本文介绍了使用Bash heredoc在Bash终端中运行ES6代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ES5代码可以在终端中使用Bash heredoc轻松运行:

ES5 code can be run easily with Bash heredoc in terminal:

node <<HEREDOC
  var fs = require("fs");
  ...
HEREDOC

但是,即使使用正确的--experimental-modules标志,ES6代码也无法运行:

But ES6 code doesn't run, even with the correct --experimental-modules flag:

node --experimental-modules <<HEREDOC
  import fs from "fs";
  ...
HEREDOC

显示的错误是:

(node:4130) ExperimentalWarning: The ESM module loader is experimental.
[stdin]:1
import fs from "fs";
       ^^

SyntaxError: Unexpected identifier
    at new Script (vm.js:83:7)
    at createScript (vm.js:267:10)
    at Proxy.runInThisContext (vm.js:319:10)
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (internal/modules/cjs/loader.js:722:30)
    at evalScript (internal/bootstrap/node.js:670:27)
    at ReadStream.<anonymous> (internal/bootstrap/node.js:340:15)
    at ReadStream.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1098:12)
    at process.internalTickCallback (internal/process/next_tick.js:72:19)

它确实显示了此信息"ExperimentalWarning:ESM模块加载器是实验性的."这意味着Node.js可以使用ES6模块功能正常运行,但是 import关键字不起作用.

It does show this info "ExperimentalWarning: The ESM module loader is experimental." which means Node.js is running correctly with ES6 module feature, however, the import keyword is just not working.

如何使用Bash heredoc在终端中内联运行ES6代码?我知道我可以将代码编写为文件以正常加载为ES6模块,但这是一个简短的临时代码,应该在heredoc中更好.

How to run ES6 code inline in terminal with Bash heredoc? I know I can write the code to file to load as ES6 module normally, but it's a short temporary code, should it be in heredoc better.

推荐答案

更新:
从Node.js 14开始,不再需要标志--experimental-modules,因此默认情况下已经在Node.js中为.mjs文件以及heredoc添加了标志--experimental-modules,但是在CLI coz ES5或ES6上仍然需要--input-type仍应为Heredoc指定

Update:
Since Node.js 14, the flag --experimental-modules is no longer needed, it's already by default in Node.js for .mjs files, and also for heredoc, but the --input-type is still needed on CLI coz ES5 or ES6 is still supposed to be specified for the heredoc:

node --input-type module <<HEREDOC
  //example:
  import fs from "fs";
HEREDOC

旧指南:
研究完之后,Node 11根本不支持stdin中的ES模块,如果您想在Node 11中使用模块,则需要将它们放在文件中.

Older guide:
After looking into it, Node 11 does not support ES modules from stdin at all, if you want to use modules in Node 11 you need to put them in a file.

对于节点12(当前未发布,但您可以使用npm i -g node-nightly进行尝试),可以使用标志--entry-type=module将stdin用作模块.

With Node 12 (currently unreleased but you can try it using npm i -g node-nightly), you can use the flag --entry-type=module to use stdin as a module.

使用node-nightly可以正常工作:

node-nightly --experimental-modules --entry-type=module <<HEREDOC
  import fs from 'fs'
  console.log(fs);
HEREDOC

如@Jamesernator在评论中指出的那样,对于v13的节点每晚,请使用"--input-type"而不是"--entry-type".

As noted by @Jamesernator in comments, for node-nightly from v13, use "--input-type" instead of "--entry-type".

并且仅支持内置模块,即. 导入"将无法在本地目录中找到模块,也将无法找到带有"-g"标志安装的全局模块.相关问题: https://github.com/nodejs/node/issues/19570

And only built-in modules are supported, ie. 'import' won't be able to find modules in local dir, and also won't be able to find global modules installed with '-g' flag. Related issue: https://github.com/nodejs/node/issues/19570

这篇关于使用Bash heredoc在Bash终端中运行ES6代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆