npm init

初始化新的 Node.js 项目,创建 package.json

语法

npm init [--yes] [<initializer>]

参数

参数说明示例级别
(交互式) 逐步填写项目信息 npm init 常用
-y --yes 使用默认值快速创建 npm init -y 常用
<initializer> 使用模板初始化 npm init vite@latest 常用

示例

快速初始化

npm init -y
使用默认值,不需要回答问题

使用 Vite 模板

npm init vite@latest my-app
等价于 npx create-vite my-app

使用 React 模板

npm init react-app my-app
等价于 npx create-react-app my-app

设置默认值

npm config set init-author-name "Your Name"
npm config set init-license MIT
以后 npm init -y 会使用这些默认值

常见错误

npm ERR! already exists 当前目录已有 package.json,删除后重试或直接编辑
Invalid name 包名不能有大写字母、空格或特殊字符

技巧

相关命令