Package a Chrome Extension Without Store Upload Errors

Originally published on ExtensionLaunch. If you want to package a Chrome extension for the Chrome Web Store, the file you need is a ZIP, not the CRX that Chrome’s “Pack extension” button gives you. Below is a short, practical walkthrough: which file format serves which purpose, the commands that build a clean archive, and a quick lookup table for the upload errors that stop most first submissions.

本文最初发布于 ExtensionLaunch。如果你想为 Chrome 应用商店打包一个 Chrome 扩展程序,你需要的文件格式是 ZIP,而不是 Chrome 的“打包扩展程序”按钮生成的 CRX 文件。以下是一份简短实用的指南:介绍不同文件格式的用途、构建干净归档文件的命令,以及一份用于排查大多数首次提交时常见上传错误的速查表。

ZIP, CRX or PEM: pick the right file. Three files show up when people talk about packaging, and they do different jobs:

  • .zip: Plain archive of your extension files. Use it for: Every Chrome Web Store upload (also Edge Add-ons and Opera).
  • .crx: ZIP plus a signed header, created by Chrome. Use it for: Testing a packed build, enterprise or self-hosted installs, verified CRX uploads.
  • .pem: Private key that signs the CRX. Use it for: Re-signing later versions; never upload or share it. The store signs and distributes your extension itself, so most developers never ship a CRX to anyone.

ZIP、CRX 或 PEM:选择正确的文件。当人们谈论打包时,会出现三种文件,它们各自承担不同的工作:

  • .zip:扩展程序文件的普通归档。用途:用于所有 Chrome 应用商店的上传(也适用于 Edge 插件和 Opera)。
  • .crx:由 Chrome 创建的 ZIP 加上签名头。用途:测试打包后的构建版本、企业或自托管安装、已验证的 CRX 上传。
  • .pem:用于签署 CRX 的私钥。用途:用于后续版本的重新签名;切勿上传或共享此文件。 应用商店会自行签署并分发你的扩展程序,因此大多数开发者从不需要向任何人发送 CRX 文件。

Get the build folder ready first. Zip what your build tool outputs (usually dist/, build/ or .output/chrome-mv3/), not your source tree. Before archiving, run through this list:

  • manifest.json sits at the top level of the folder, not inside a subfolder.
  • The version is greater than the one already published. Google suggests something small like 0.0.0.1 for a first upload.
  • The manifest contains no comments. JSON doesn’t allow them, and they’re the usual reason for “Cannot parse the manifest”.
  • Every file the manifest references exists, with identical capitalization: icons, service worker, content scripts, popup, _locales.
  • Nothing private is included. Keep .env files, API secrets, node_modules, .git and test fixtures out. Source maps are allowed but reveal your original code. Anyone who installs the extension can download what’s inside.

首先准备好构建文件夹。压缩构建工具输出的内容(通常是 dist/、build/ 或 .output/chrome-mv3/),而不是你的源代码树。在归档之前,请检查以下列表:

  • manifest.json 位于文件夹的顶层,而不是子文件夹内。
  • 版本号高于已发布版本。Google 建议首次上传时使用 0.0.0.1 这样的小版本号。
  • 清单文件中不包含注释。JSON 不允许注释,它们通常是导致“无法解析清单(Cannot parse the manifest)”的原因。
  • 清单中引用的每个文件都必须存在,且大小写必须完全一致:图标、Service Worker、内容脚本、弹出窗口、_locales 等。
  • 不包含任何私密内容。将 .env 文件、API 密钥、node_modules、.git 和测试夹具排除在外。允许使用 Source maps,但它们会暴露你的原始代码。任何安装该扩展程序的人都可以下载其中的内容。

Build and verify the ZIP. The rule is the same everywhere: compress the contents of the build folder, not the folder itself. If you zip the folder, manifest.json ends up one level too deep and the upload fails. On macOS or Linux: cd dist zip -r -X ../my-extension-1.4.0.zip . -x '*.DS_Store' -x '*.map' cd .. On Windows PowerShell, the trailing * is what keeps the root folder out of the archive: Compress-Archive -Path .\dist\* -DestinationPath .\my-extension-1.4.0.zip -Force

构建并验证 ZIP 文件。规则在任何地方都是一样的:压缩构建文件夹的内容,而不是文件夹本身。如果你压缩了文件夹,manifest.json 就会深藏一层,导致上传失败。在 macOS 或 Linux 上: cd dist zip -r -X ../my-extension-1.4.0.zip . -x '*.DS_Store' -x '*.map' cd .. 在 Windows PowerShell 中,末尾的 * 可以确保根文件夹不会被包含在归档中: Compress-Archive -Path .\dist\* -DestinationPath .\my-extension-1.4.0.zip -Force

One caveat: Compress-Archive skips hidden files, so it won’t include dot-prefixed files. Also, use a fresh file name for each release so an old archive can’t keep files you’ve since removed. Then spend a minute checking the archive: unzip -l my-extension-1.4.0.zip | head -20 # manifest.json should be at the top, not dist/ unzip -p my-extension-1.4.0.zip manifest.json | python3 -m json.tool > /dev/null && echo "manifest OK" Last, test the exact build you’re about to ship. Extract the ZIP into a new folder and load that folder unpacked. Google warns that packed and unpacked extensions can behave differently.

需要注意的一点是:Compress-Archive 会跳过隐藏文件,因此它不会包含以点开头的文件。此外,每次发布都要使用新的文件名,这样旧的归档文件就不会保留你已经删除的文件。然后花一分钟检查归档文件: unzip -l my-extension-1.4.0.zip | head -20 # manifest.json 应该在顶部,而不是在 dist/ 中 unzip -p my-extension-1.4.0.zip manifest.json | python3 -m json.tool > /dev/null && echo "manifest OK" 最后,测试你即将发布的构建版本。将 ZIP 解压到一个新文件夹中,并以“已解压”模式加载该文件夹。Google 警告说,打包和未打包的扩展程序表现可能会有所不同。

When you actually need a CRX. Create one from chrome://extensions: turn on Developer mode, click Pack extension, choose the folder that contains manifest.json, and leave the key field empty the first time. Chrome saves a .crx and a .pem next to that folder. From the CLI: google-chrome --pack-extension=/path/to/dist google-chrome --pack-extension=/path/to/dist --pack-extension-key=/path/to/dist.pem Guard the .pem. The key determines which extension a CRX belongs to, so signing with a new key makes Chrome treat the package as a different extension. Store it in a password manager or vault and add *.pem to .gitignore.

当你确实需要 CRX 时。可以通过 chrome://extensions 创建:开启开发者模式,点击“打包扩展程序”,选择包含 manifest.json 的文件夹,首次打包时将密钥字段留空。Chrome 会在该文件夹旁边保存一个 .crx 和一个 .pem 文件。通过命令行: google-chrome --pack-extension=/path/to/dist google-chrome --pack-extension=/path/to/dist --pack-extension-key=/path/to/dist.pem 妥善保管 .pem 文件。该密钥决定了 CRX 属于哪个扩展程序,因此使用新密钥签名会使 Chrome 将该包视为不同的扩展程序。请将其存储在密码管理器或保险库中,并将 *.pem 添加到 .gitignore 中。

The store only accepts a CRX after you opt into Verified CRX Uploads in the Developer Dashboard. After that, every update has to be signed with the key you registered (generated with openssl genpkey as a 2048-bit RSA key). This protects you if your account is compromised, but replacing a lost key through support can take up to a week.

只有在你于开发者仪表板中选择加入“已验证 CRX 上传(Verified CRX Uploads)”后,应用商店才会接受 CRX 文件。此后,每次更新都必须使用你注册的密钥(通过 openssl genpkey 生成的 2048 位 RSA 密钥)进行签名。这可以在你的账户被盗时保护你,但通过支持渠道更换丢失的密钥可能需要长达一周的时间。

Common packaging errors and fixes:

  • “Cannot parse the manifest”: Comments or invalid JSON. Fix: Remove comments, validate, rezip.
  • Manifest missing or unreadable: Manifest is inside a subfolder. Fix: Zip the folder’s contents.
  • Version rejected: Version isn’t higher than the live one. Fix: Bump version and repackage.
  • “Your item has packaging errors”: A file the manifest references is missing or has the wrong path. Fix: Check the build output, not the source.
  • Works unpacked, fails packed: Case mismatch (Background.js vs background.js). Fix: Match the case exactly.
  • _locales subtree missing: default_locale set without messages.json. Fix: Add the locale folder or remove the key.
  • CRX_REQUIRED_PROOF_MISSING: The CRX wasn’t signed by the store. Fix: Load the extracted folder for testing; use enterprise policies for managed devices.

常见打包错误及修复:

  • “无法解析清单”:存在注释或无效的 JSON。修复:删除注释,验证,重新压缩。
  • 清单缺失或不可读:清单位于子文件夹内。修复:压缩文件夹的内容。
  • 版本被拒绝:版本号未高于线上版本。修复:提升版本号并重新打包。
  • “你的项目存在打包错误”:清单引用的文件缺失或路径错误。修复:检查构建输出,而不是源代码。
  • 未打包时正常,打包后失败:大小写不匹配(Background.js 与 background.js)。修复:确保大小写完全一致。
  • _locales 子树缺失:设置了 default_locale 但缺少 messages.json。修复:添加语言环境文件夹或删除该键。
  • CRX_REQUIRED_PROOF_MISSING:CRX 未经应用商店签名。修复:加载解压后的文件夹进行测试;对于受管设备,请使用企业策略。

Key takeaways:

  • The Chrome Web Store needs a ZIP with manifest.json at its root.
  • A CRX is for testing, enterprise installs or verified uploads.
  • Zip the contents of your build output, then list and validate the archive before you upload.
  • Keep secrets, .env, node_modules and .git out of the package.
  • Back up your .pem key and keep it out of your repository.
  • Most rejections come from manifest comments, missing files, case mismatches or a version that wasn’t bumped.

关键要点:

  • Chrome 应用商店需要一个 manifest.json 位于根目录的 ZIP 文件。
  • CRX 用于测试、企业安装或已验证的上传。
  • 压缩构建输出的内容,并在上传前列出并验证归档文件。
  • 将密钥、.env、node_modules 和 .git 排除在包外。
  • 备份你的 .pem 密钥,并将其排除在代码仓库之外。
  • 大多数拒绝原因来自清单注释、文件缺失、大小写不匹配或版本号未更新。