Nodejs開發之一 npm init 初始化, 生成pakeage.json文件


 

在node開發中使用npm init會生成一個pakeage.json文件,這個文件主要是用來記錄這個項目的詳細信息的,它會將我們在項目開發中所要用到的包,以及項目的詳細信息等記錄在這個項目中。方便在以后的版本迭代和項目移植的時候會更加的方便。也是防止在后期的項目維護中誤刪除了一個包導致的項目不能夠正常運行。

使用npm init初始化項目還有一個好處就是在進行項目傳遞的時候不需要將項目依賴包一起發送給對方,對方在接受到你的項目之后再執行npm install就可以將項目依賴全部下載到項目里。

 

1. 創建一個空文件夾: 例如: C:\Program Files\nodejs\MyBook, 作為項目的目錄

2. 打開項目文件夾, shift+鼠標右鍵 ==> 在此處打開命令行窗口

3. >npm int 如下

C:\Program Files\nodejs\MyBook>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (mybook) mybook  //---- 輸入包的名稱
version: (1.0.0)         //---- 版本
description: My book shelf    //----描述信息
entry point: (index.js)      //-----入口信息
test command: node app.js   //---測試腳本入口 npm test
git repository:
keywords:
author: author
license: (ISC)
About to write to C:\Program Files\nodejs\MyBook\package.json:

{
"name": "mybook",
"version": "1.0.0",
"description": "My book shelf",
"main": "index.js",
"scripts": {
"test": "node app.js"
},
"author": "author",
"license": "ISC"
}


Is this OK? (yes) yes

C:\Program Files\nodejs\MyBook>

 4. 現在已經成功生成了一個pakeage.json文件:

 1 {
 2   "name": "mybook",
 3   "version": "1.0.0",
 4   "description": "My book shelf",
 5   "main": "index.js",
 6   "scripts": {
 7     "test": "node app.js"
 8   },
 9   "author": "author",
10   "license": "ISC"
11 }

5.  在命令行下, npm test , 可以運行對應的腳本.

6. 下面是Vue-cli腳手架自動生成的JSON文件, 多了 ' dependencies 和devDependencies依賴庫文件列表.

 1 {
 2   "name": "books",
 3   "version": "0.1.0",
 4   "private": true,
 5   "scripts": {
 6     "serve": "vue-cli-service serve",
 7     "build": "vue-cli-service build",
 8     "lint": "vue-cli-service lint"
 9   },
10   "dependencies": {   //---------項目開發依賴的庫
11     "core-js": "^3.6.5",
12     "vue": "^2.6.11",
13     "vue-router": "^3.2.0",
14     "vuex": "^3.4.0"
15   },
16   "devDependencies": {  // ----------項目打包依賴的庫
17     "@vue/cli-plugin-babel": "~4.4.0",
18     "@vue/cli-plugin-eslint": "~4.4.0",
19     "@vue/cli-plugin-router": "^4.4.6",
20     "@vue/cli-plugin-vuex": "^4.4.6",
21     "@vue/cli-service": "~4.4.0",
22     "babel-eslint": "^10.1.0",
23     "eslint": "^6.7.2",
24     "eslint-plugin-vue": "^6.2.2",
25     "vue-template-compiler": "^2.6.11"
26   },
27   "eslintConfig": {
28     "root": true,
29     "env": {
30       "node": true
31     },
32     "extends": [
33       "plugin:vue/essential",
34       "eslint:recommended"
35     ],
36     "parserOptions": {
37       "parser": "babel-eslint"
38     },
39     "rules": {}
40   },
41   "browserslist": [
42     "> 1%",
43     "last 2 versions",
44     "not dead"
45   ]
46 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM