「新手問題」為什麼我不能下載 npm 的套件?


Posted by estella00911 on 2021-05-04

緣起

一開始想用 ls 來確認一下有沒有 node_module 跟 package.json ,這樣就可以知道有沒有引入 npm (Node.js 套件管理程式)

發生的問題

開一個新資料夾,建立 index.js,然後想要從 npm 引入 module(npm install)發生的問題如下:

$ ls -l
total 0
-rw-r--r--@ 1 jeanlu  staff  0 May  4 11:59 index.js
$ npm install request
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN lioj@1.0.0 No description
npm WARN lioj@1.0.0 No repository field.
$ ls
index.js

結果我再一次 ls,我發現並沒有 package.json,表示 npm install request失敗。在想說為什麼沒有這些檔案時,把 npm WARN lioj@1.0.0 No description npm WARN lioj@1.0.0 No repository field 查了一下 google,發現這篇
Can't istall Node.js Packages properly on Shared hosting 的 1F Answer。原來是需要 npm init,初始化 npm 後,確認有沒有 package.json 檔案生成。

$ npm init
... # 一直按 enter
Is this OK? (yes)
$ ls  # 確認有 package.json 生成, npm 安裝完成
index.js    package.json

成功引入 npm (套件管理程式),會生成 package.json。如此一來,下一步驟就可以來下載所需要的套件囉,下載好我需要的套件「request」後會生成 package-lock.json 及 node_modules。

$ npm install request # 下載 npm 套件「request」
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN week4_json@1.0.0 No description
npm WARN week4_json@1.0.0 No repository field.

+ request@2.88.2
added 47 packages from 58 contributors and audited 47 packages in 2.641s

2 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
$ ls # 下載好套件後,會有新的檔案生成:package-lock.json, node_modules
index.js        package-lock.json
node_modules        package.json

然後就可以開始使用了!


#npm #beginner







Related Posts

【隨堂筆記】資料結構基礎概論

【隨堂筆記】資料結構基礎概論

Day05:從 class 看 bytecode

Day05:從 class 看 bytecode

這次,我是芬蘭極地導遊分享會「聽眾」

這次,我是芬蘭極地導遊分享會「聽眾」


Comments