Skip to content

fix(dev): make dependency install registry-agnostic and npm 12 compatible - #12

Merged
MalachiteN merged 1 commit into
NERDSORG:mainfrom
guilimao:main
Aug 2, 2026
Merged

fix(dev): make dependency install registry-agnostic and npm 12 compatible#12
MalachiteN merged 1 commit into
NERDSORG:mainfrom
guilimao:main

Conversation

@guilimao

@guilimao guilimao commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

问题背景

在全新环境中执行 npm install 时,安装过程会失败并报错:

npm error code EALLOWREMOTE
npm error Fetching packages of type "remote" have been disabled
npm error Refusing to fetch "zwitch@https://registry.npmmirror.com/zwitch/-/zwitch-2.0.4.tgz"
npm error A complete log of this run can be found in:
npm error /home/guilimao/.npm/_logs/2026-08-02T13_13_19_378Z-debug-0.log

原因是 npm 12 引入了新的远程依赖安全策略,allow-remote 默认值调整为 none

Upcoming breaking changes for npm v12: --allow-remote defaults to none

当前 package-lock.json 是在使用镜像 registry 的环境中生成的,其中记录了指向 registry.npmmirror.com 的 tarball URL。

当开发者使用 npm 12 或更高版本,并将 registry 配置为官方源时,lockfile 中的 tarball 主机与当前 registry 不一致。npm 会将该地址识别为未经允许的远程依赖,并以 EALLOWREMOTE 拒绝下载。

修复内容

1. 避免 lockfile 绑定特定 registry

新增项目级 .npmrc

omit-lockfile-registry-resolved=true

启用该配置后,package-lock.json 不再记录普通 registry 依赖的 tarball URL。安装时,npm 会根据开发者当前配置的 registry 解析实际下载地址。

这样可以避免 lockfile 绑定生成者使用的官方源、镜像源或企业代理,提高其在不同开发环境中的可移植性。

现有 package-lock.json 已按照该配置重新规范化。

2. 显式批准必要的安装脚本

package.json 中新增:

{
  "allowScripts": {
    "better-sqlite3": true,
    "electron": true,
    "esbuild": true,
    "lzma-native": true
  }
}

npm 12 默认限制未经批准的依赖安装脚本。上述依赖需要在安装阶段下载平台二进制文件、加载预编译原生模块或执行本地构建,因此需要显式加入允许列表。

提交该配置后,开发环境和 CI 可以在不依赖个人 npm 配置的情况下完成安装。

验证结果

  • 使用官方 registry 执行 npm ci,安装成功

  • 使用镜像 registry 执行npm ci --registry=https://registry.npmmirror.com,安装成功

  • 执行 npm run compile,TypeScript 类型检查和 esbuild 打包均通过

后续注意事项

omit-lockfile-registry-resolved 是 npm 12 引入的配置。

如果使用旧版本 npm 添加或更新依赖,npm 可能重新向 package-lock.json 写入 resolved 字段。提交前应使用项目规定的 npm 版本执行:

npm install --package-lock-only --ignore-scripts

然后检查 package-lock.json 的变更,确保其中没有重新写入与个人环境相关的 registry 地址。

Summary by Sourcery

提升在不同注册表和 npm 版本之间安装依赖的兼容性。

改进内容:

  • 添加项目级 .npmrc 配置,避免将锁文件绑定到特定注册表,从而提升可移植性。
  • 规范化 package-lock.json,使其符合与注册表无关的锁文件行为。
  • 声明原生和二进制依赖包允许执行的安装脚本,以支持 npm 12 的脚本限制。
Original summary in English

Summary by Sourcery

Improve npm dependency installation compatibility across different registries and npm versions.

Enhancements:

  • Add project-level .npmrc configuration to avoid binding the lockfile to a specific registry and improve portability.
  • Normalize package-lock.json to align with registry-agnostic lockfile behavior.
  • Declare allowed install scripts for native and binary-dependent packages to support npm 12's script restrictions.

…ible

- Add .npmrc with omit-lockfile-registry-resolved=true: the lockfile no
  longer records registry hosts, so installs work with any registry or
  mirror and no longer hit npm 12's EALLOWREMOTE when the lockfile was
  generated against a mirror registry.
- Rewrite package-lock.json via npm install --package-lock-only: strip
  692 'resolved' URLs (all integrity hashes preserved, no version drift).
- Declare allowScripts for native/build deps (better-sqlite3, electron,
  esbuild, lzma-native): npm 12 blocks install scripts by default, this
  keeps npm ci working out of the box for all contributors.

Verified: npm ci passes with the official registry and with
registry.npmmirror.com.
@sourcery-ai

sourcery-ai Bot commented Aug 2, 2026

Copy link
Copy Markdown
审阅者指南(在小型 PR 上折叠)

审阅者指南

此 PR 更新了 npm 配置和元数据,使依赖安装在不同的 registry 上都能可靠运行,并与 npm 12 更严格的远程和脚本策略兼容。

适配 npm 12、与 registry 解耦的依赖安装流程图

flowchart TD
    A[开发者运行 npm ci 或 npm install] --> B[npm 读取项目 .npmrc]
    B --> C[omit-lockfile-registry-resolved=true]
    C --> D[npm 在解析 package-lock.json 时不使用绑定到特定 registry 的 resolved URL]
    D --> E[npm 从当前配置的 registry 下载 tarball]

    A --> F[npm 读取 package.json allowScripts]
    F --> G[允许 better-sqlite3、electron、esbuild、lzma-native 的安装脚本]
    F --> H[其他依赖的安装脚本仍然遵循 npm 12 的默认阻止策略]

    E --> I[在不同 registry 上安装均可成功完成]
    G --> I
    H --> I
Loading

文件级变更

变更 详情 文件
配置 npm,避免在 lockfile 中嵌入与 registry 绑定的 tarball URL,并基于该配置重新生成 lockfile。
  • 添加项目级 npm 配置,启用 omit-lockfile-registry-resolved,使 lockfile 不再存储普通 registry 的 tarball URL。
  • 在新设置下重新生成并规范化 package-lock.json,使依赖不再引用特定的 registry 主机。
.npmrc
package-lock.json
显式允许原生/二进制依赖在安装时所需的脚本,以满足 npm 12 的脚本批准要求。
  • 在包清单中引入 allowScripts 配置段。
  • 批准 better-sqlite3、electron、esbuild 和 lzma-native 的安装脚本,使其二进制下载/构建步骤在开发和 CI 中无需按用户单独配置即可运行。
package.json

技巧与命令

与 Sourcery 交互

  • 触发新的审阅: 在 pull request 中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub issue: 通过回复审阅评论,请求 Sourcery 以该评论创建一个 issue。你也可以在审阅评论下回复 @sourcery-ai issue 来从该评论创建 issue。
  • 生成 pull request 标题: 在 pull request 标题中的任意位置写上 @sourcery-ai,即可在任何时候生成标题。你也可以在 pull request 中评论 @sourcery-ai title 来(重新)生成标题。
  • 生成 pull request 摘要: 在 pull request 正文中的任意位置写上 @sourcery-ai summary,即可在你希望的位置生成 PR 摘要。你也可以在 pull request 中评论 @sourcery-ai summary 来在任何时候(重新)生成摘要。
  • 生成审阅者指南: 在 pull request 中评论 @sourcery-ai guide,即可在任何时候(重新)生成审阅者指南。
  • 解决所有 Sourcery 评论: 在 pull request 中评论 @sourcery-ai resolve,即可解决所有 Sourcery 评论。如果你已经处理了所有评论且不希望再看到它们,这会非常有用。
  • 忽略所有 Sourcery 审阅: 在 pull request 中评论 @sourcery-ai dismiss,即可忽略所有现有的 Sourcery 审阅。如果你希望从一个新的审阅重新开始,这尤其有用——别忘了再评论 @sourcery-ai review 来触发新的审阅!

自定义你的体验

访问你的 控制面板 以:

  • 启用或禁用审阅功能,例如 Sourcery 生成的 pull request 摘要、审阅者指南等。
  • 更改审阅语言。
  • 添加、移除或编辑自定义审阅说明。
  • 调整其他审阅设置。

获取帮助

Original review guide in English
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR updates npm configuration and metadata to make dependency installation work reliably across different registries and with npm 12’s stricter remote and script policies.

Flow diagram for npm 12-compatible, registry-agnostic dependency installation

flowchart TD
    A[Developer runs npm ci or npm install] --> B[npm reads project .npmrc]
    B --> C[omit-lockfile-registry-resolved=true]
    C --> D[npm interprets package-lock.json without registry-bound resolved URLs]
    D --> E[npm downloads tarballs from current configured registry]

    A --> F[npm reads package.json allowScripts]
    F --> G[Scripts for better-sqlite3, electron, esbuild, lzma-native are allowed]
    F --> H[Other dependency install scripts remain blocked by npm 12 defaults]

    E --> I[Installation completes successfully across different registries]
    G --> I
    H --> I
Loading

File-Level Changes

Change Details Files
Configure npm to avoid embedding registry-specific tarball URLs in the lockfile and regenerate the lockfile accordingly.
  • Add a project-level npm configuration enabling omit-lockfile-registry-resolved so lockfiles no longer store ordinary registry tarball URLs.
  • Regenerate and normalize package-lock.json under the new setting so dependencies no longer reference a specific registry host.
.npmrc
package-lock.json
Explicitly allow required install-time scripts for native/binary dependencies to satisfy npm 12’s script approval requirements.
  • Introduce an allowScripts section in the package manifest.
  • Approve install scripts for better-sqlite3, electron, esbuild, and lzma-native so their binary downloads/build steps can run in dev and CI without per-user configuration.
package.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗨——我已经审查了你的修改,看起来非常棒!


Sourcery 对开源项目是免费的——如果你觉得我们的审查有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的审查。
Original comment in English

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@MalachiteN
MalachiteN merged commit e36a714 into NERDSORG:main Aug 2, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants