我的執行步驟
我創建了一個名叫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
字段的值,不能與所要安裝的依賴包名字相同。