在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 }