首页 > 解决方案 > 试图在 ZX 命令行上运行 .mjs 脚本

问题描述

我试图在我的 ubuntu 操作系统上运行这个示例。

https://github.com/google/zx/blob/main/examples/basics.mjs

在我从 npm 安装 zx 之后

通过npm i -g zx

并运行以下命令:

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

标签: javascriptshellscripting

解决方案


正如上面的评论所解释的:

该问题源于无法识别该import功能的旧版本节点。仅从以下版本可用:v13+(请参阅https://stackoverflow.com/questions/39436322/node-js-syntaxerror-unexpected-token-import)


推荐阅读