我创建了一个名叫`express`的文件夹,想在这个工程中学习`express`
进入该文件夹,执行`npm init`来初始化package.json文件
我们会发现当前文件夹多了一个 package.json 文件。
然后安装express包并保存在package.json的依赖中
```
npm install express --save
```
## 出现错误:
```
10:04:53 @~/code/express$ npm install express --save
npm ERR! code ENOSELF
npm ERR! Refusing to install package with name "express" under a package
npm ERR! also called "express". Did you name your project the same
npm ERR! as the dependency you're installing?
npm ERR!
npm ERR! For more information, see:
npm ERR! <https://docs.npmjs.com/cli/install#limitations-of-npms-install-algorithm>
```
## 问题原因
可以确定我平常使用npm 安装依赖包的时候,是没有问题的。
我又看了一下`package.json`文件,发现我在初始化的时候`name`字段为`express`。怀疑是这个问题。
然后我将`name`字段的值修改为`express-test`
然后重新执行
```
npm install express --save
```
成功后出现:
然后我们再看一下`package.json`文件:
发现多了一个`dependencies`字段,里面保存了`express`的版本
然后在文件夹内输入`ls`,多了个`node_modules`文件夹
## 总结
`npm install xxxx --save`命令会将安装包的名字及版本保存在`package.json`文件中
`package.json`文件中的`name`字段的值,不能与所要安装的依赖包名字相同。