---
title: "依赖项"
description: "Nuxt Kit 提供用于检查可选依赖项是否已安装，并告知用户如何安装这些依赖项的实用工具。"
canonical_url: "https://nuxt.zhcndoc.com/docs/4.x/api/kit/dependencies"
---
# 依赖项

> Nuxt Kit 提供用于检查可选依赖项是否已安装，并告知用户如何安装这些依赖项的实用工具。

模块通常依赖某个仅部分项目需要，或预期由用户自行安装的包。Nuxt Kit 无需假设包已存在，也不会打印用户未使用的包管理器的安装命令，而是可以检查依赖项是否可解析，并使用项目自己的包管理器提供安装选项。

## `ensureDependencyInstalled`

检查一个或多个依赖项是否已安装，并提示用户安装所有缺失的依赖项。

### 用法

```tstwoslash
import { defineNuxtModule, ensureDependencyInstalled } from '@nuxt/kit'

export default defineNuxtModule({
  async setup () {
    if (!await ensureDependencyInstalled('sass')) {
      return
    }
  },
})
```

### 类型

```ts
function ensureDependencyInstalled (names: string, options?: EnsureDependencyInstalledOptions): Promise<boolean>
function ensureDependencyInstalled (names: string[], options?: EnsureDependencyInstalledOptions): Promise<true | string[]>
```

### 参数

**names**：要检查的一个或多个包名，如果缺失则安装。

**options**：

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Default
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        rootDir
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        nuxt.options.rootDir
      </code>
    </td>
    
    <td>
      用于解析包和安装包的目录。
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        searchPaths
      </code>
    </td>
    
    <td>
      <code>
        string[]
      </code>
    </td>
    
    <td>
      <code>
        nuxt.options.modulesDir
      </code>
    </td>
    
    <td>
      用于解析包的其他目录。
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        from
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        undefined
      </code>
    </td>
    
    <td>
      用于解析包的额外 URL 或路径，例如 <code>
        import.meta.url
      </code>
      
      。当你自己的包将该依赖项作为对等依赖项提供时，此选项很有用。
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        prompt
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      <code>
        undefined
      </code>
    </td>
    
    <td>
      是否在安装前进行提示。<code>
        true
      </code>
      
       始终提示，<code>
        false
      </code>
      
       从不提示（并在 StackBlitz 上自动安装），未设置时则在除 StackBlitz 之外的所有环境中提示。
    </td>
  </tr>
</tbody>
</table>

### 返回值

传入单个包名时，如果该包可用，则返回 `true`；如果不可用，则返回 `false`。传入数组时，如果所有包都可用，则返回 `true`；否则返回仍然缺失的包名数组（因为用户拒绝安装、安装失败，或没有交互式终端）。

<note>

在 CI 环境或没有交互式终端时，不会安装任何内容。在这种情况下，缺失的包会作为诊断信息报告，其中包含安装命令；函数也会将这些包报告为缺失，以便你的模块能够优雅地降级。

</note>

## `getAddDependencyCommand`

获取用户应运行的命令，以便将依赖项添加到项目中。该命令使用根据 `cwd` 检测到的包管理器，如果未检测到则使用 `npm`。每当你打印安装说明时，都应使用此命令，以确保命令与项目的包管理器相匹配。

### 用法

```tstwoslash
import { defineNuxtModule, getAddDependencyCommand, useLogger } from '@nuxt/kit'

export default defineNuxtModule({
  async setup (options, nuxt) {
    const logger = useLogger('my-module')
    const command = await getAddDependencyCommand('sass', nuxt.options.rootDir, { dev: true })

    logger.warn(`\`sass\` is required. Run \`${command}\` to install it.`)
  },
})
```

### 类型

```ts
function getAddDependencyCommand (names: string | string[], cwd: string, options?: { dev?: boolean }): Promise<string>
```

### 参数

**names**：要安装的一个或多个包名。

**cwd**：用于检测包管理器的目录。

**options.dev**：命令是否应将这些包作为开发依赖项安装。

---

- [Source](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/dependency.ts)


## Sitemap

See the full [sitemap](https://nuxt.zhcndoc.com/sitemap.md) for all pages.
