问题描述
我试图在我的 ubuntu 操作系统上运行这个例子。
https://github.com/google/zx/blob/main/examples/basics.mjs
在我从 npm 安装 zx 之后
通过npm i -g zx
并运行以下cmd:
sudo zx ./script.mjs
我收到以下错误:
/usr/local/lib/node_modules/zx/zx.mjs:17
import {basename,dirname,extname,join,parse,resolve} from 'path'
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJscore (internal/bootstrap/node.js:623:3)
我的 npm 版本是:7.14.0
解决方法
如以上评论所述:
该问题源于无法识别 import
功能的旧版本节点。仅适用于:v13+(参见 https://stackoverflow.com/questions/39436322/node-js-syntaxerror-unexpected-token-import)