# 如何修复AI编码代理虚构不存在的npm包

> AI代理为何会臆想不存在的npm包、如何识别以及如何阻止。

**Type:** Failure  
**Tools:** Cursor, Claude Code, Codex  
**Stack:** TypeScript  
**Updated:** 2026-06-08

---

一个常见且危险的失败：代理导入了一个不存在的包——
更糟的是，一个抢注者已注册了与臆想名称匹配的包。

## 症状

代理为一个听起来合理的包编写了导入语句，并将其添加到
`package.json`，但安装失败或拉取了一个未知包。

```txt
import { magicValidate } from "zod-magic-helpers"; // does not exist
```

## 发生原因

模型会对命名约定进行模式匹配（“可能有一个 `-helpers` 包”），并生成听起来*真实*的名称。这是“slopsquatting”供应链攻击的基础。

## 如何识别

- 差异中出现了你不认识的依赖项。
- 该包的下载量几乎为零，或者最近才发布。
- 安装解析出的名称与流行包仅差一个字符。

## 如何修复

```txt
[ ] Verify every new dependency exists and is the one you intend
[ ] Check weekly downloads + repo link before installing
[ ] Prefer packages already in the lockfile
[ ] Pin versions; review the lockfile diff
```

## 修复提示

```txt title="Fix Prompt"
You added a dependency I can't verify. List every new package, its npm URL,
and weekly download count. Replace any unverified package with a standard,
widely-used alternative or inline the logic instead.
```

## 测试

```bash
bun pm view <package> 2>/dev/null || echo "DOES NOT EXIST"
```