Skip to content

npx @larksuite/cli@latest install 安装错误,抛出 fails with ERR_REQUIRE_ESM #1636

Description

@oneisall8955

环境

项目 详情
OS Windows 11 Pro (10.0.26100)
Node.js v22.11.0
命令 npx @larksuite/cli@latest install
@larksuite/cli 版本 latest
@clack/prompts 解析版本 ≥1.0.0(ESM-only)

现象

按照 README.zh.md 中的安装指引执行:

npx @larksuite/cli@latest install

抛出以下异常:

node:internal/modules/cjs/loader:1309
    throw new ERR_REQUIRE_ESM(filename, true);
    ^

Error [ERR_REQUIRE_ESM]: require() of ES Module D:\nodejs\node_cache\_npx\8f08eae71a6e4041\node_modules\@clack\prompts\dist\index.mjs not supported.
Instead change the require of D:\nodejs\node_cache\_npx\8f08eae71a6e4041\node_modules\@clack\prompts\dist\index.mjs to a dynamic import() which is available in all CommonJS modules.
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at Object.<anonymous> (D:\nodejs\node_cache\_npx\8f08eae71a6e4041\node_modules\@larksuite\cli\scripts\install-wizard.js:8:11) {
  code: 'ERR_REQUIRE_ESM'
}

根因分析

错误发生在 scripts/install-wizard.js:8:11,该文件使用 CommonJS 的 require('@clack/prompts') 来加载依赖。但 @clack/prompts 从 v1.0.0 起已变为 ESM-only 分发,不再提供 CJS 入口。

参考 @clack/prompts v1.0.0 Release Notes

The package is now distributed as ESM-only. In v0 releases, the package was dual-published as CJS and ESM.

版本兼容对照

@clack/prompts 版本 CJS require() ESM import
≤0.11.0(v0 最新) ✅ 支持 ✅ 支持
≥1.0.0 ❌ 不支持 ✅ 支持

@larksuite/clipackage.json 中对 @clack/prompts 的版本约束(推测为 ^0.7.0 或类似范围)未锁死上限,导致 npm 解析到了不兼容的 1.x 版本。

修复建议(三选一)

方案 A:锁死 @clack/prompts 版本(改动最小)

package.json 中将 @clack/prompts 固定到最后一个 CJS 兼容版本:

{
  "dependencies": {
    "@clack/prompts": "0.11.0"
  }
}

方案 B:将 install-wizard.js 改为 ESM

  • 重命名为 install-wizard.mjs
  • require('@clack/prompts') 替换为 import prompts from '@clack/prompts'
  • 同样处理其他 require() 调用
  • 更新 package.json 中对该脚本的引用路径

方案 C:使用动态 import()(兼容 CJS 环境)

install-wizard.js 中,将:

const prompts = require('@clack/prompts');

替换为:

const prompts = await import('@clack/prompts');

注意:这要求调用处是 async 函数,或使用顶层 await(Node.js ≥14.8)。

用户侧临时 Workaround

在等待上游修复期间,用户可通过以下方式绕过:

# PowerShell(Windows)
# 1. 清除 npx 缓存
Remove-Item -Recurse -Force "$env:LOCALAPPDATA\npm-cache\_npx" -ErrorAction SilentlyContinue

# 2. 启用 Node.js 实验性 ESM require 支持(需要 Node ≥20.19 或 ≥22.12)
$env:NODE_OPTIONS = "--experimental-require-module"
npx @larksuite/cli@latest install

或者手动安装并降级依赖:

mkdir larkcli-fix && cd larkcli-fix
npm init -y
npm install @larksuite/cli @clack/prompts@0.11.0
node node_modules/@larksuite/cli/scripts/install-wizard.js

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdomain/coreCLI framework and core libraries

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions