要创建package.json文件,运行以下命令:
> npm init
此为初始化项目命令,会在你运行此命令的文件夹根目录下创建项目配置文件:package.json。同时每行会出现一个问题,你输入答案后会出来另一个问题。这些问题最终会记录到package.json文件中。
也可以运行带--yes或-y标签的npm init命令,来生成默认的package.json文件:
> npm init --yes
或者
> npm init --y
比如最近做的相关项目的package.json就是以下这个样子。
{
"name": "devil",
"version": "1.0.0",
"description": "",
"main": "",
"scripts": {
"test": "mocha --compilers js:babel-core/register assets/es6/test"
},
"repository": {
"type": "git",
"url": "............"
},
"author": "bluesky",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.18.2",
"babel-eslint": "^7.2.2",
"babel-preset-es2015": "^6.18.0",
"babel-preset-stage-0": "^6.16.0",
"babelify": "^7.3.0",
"browser-sync": "^2.9.11",
"browserify": "^14.3.0",
"chai": "^3.5.0",
"coffee-script": "^1.10.0",
"coffeeify": "^2.1.0",
"eslint": "^3.10.2",
"gulp": "^3.9.0",
"gulp-autoprefixer": "^3.1.0",
"gulp-babel": "^6.1.2",
"gulp-bless": "^3.1.0",
"gulp-coffee": "^2.3.1",
"gulp-compass": "^2.1.0",
"gulp-concat": "^2.6.0",
"gulp-csso": "^3.0.0",
"gulp-eslint": "^3.0.1",
"gulp-if": "^2.0.2",
"gulp-jshint": "^2.0.4",
"gulp-load-plugins": "^1.0.0",
"gulp-notify": "^3.0.0",
"gulp-plumber": "^1.0.1",
"gulp-rename": "^1.2.2",
"gulp-rev": "^7.1.2",
"gulp-rev-replace": "^0.4.3",
"gulp-sass": "^3.1.0",
"gulp-sequence": "^0.4.6",
"gulp-sourcemaps": "^2.4.1",
"gulp-uglify": "^2.1.2",
"gulp-watch": "^4.3.5",
"gulp.spritesmith": "^6.2.1",
"jshint": "^2.9.2",
"jshint-stylish": "^2.0.1",
"merge-stream": "^1.0.1",
"minimatch": "^3.0.2",
"minimist": "^1.2.0",
"mocha": "^3.2.0",
"require-dir": "^0.3.0",
"rimraf": "^2.6.1",
"run-sequence": "^1.1.4",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0"
},
"dependencies": {
"babel-runtime": "^6.18.0"
}
}
- name:默认为作者名字,除非在git目录中,它会是git仓库的名字;
- version:版本号,刚初始化的项目总是1.0.0;
- main:项目启动的名字,这个项目没有设置;
- scripts:项目测试路径;
- author:作者
- license:ISC开源证书
- repository: github版本控制路径
指定依赖包
- “dependencies”: 在项目中所需要的安装包
- “devDependencies”: 只在项目开发中需要的安装包
添加package.json依赖的入口(dependencies):
npm install <package_name> --save
添加package.json开发环境依赖的入口(devDependencies):
npm install <package_name> --save-dev
scripts字段
scripts指定了运行脚本命令的npm命令行缩写,比如start指定了运行npm run start时,所要执行的命令。
下面的设置指定了npm run preinstall、npm run postinstall、npm run start、npm run test时,所要执行的命令。
"scripts": {
"preinstall": "echo here it comes!",
"postinstall": "echo there it goes!",
"start": "node index.js",
"test": "tap test/*.js"
}
查看本地包是否有新版本
npm outdated
更新本地包
npm update
卸载本地包
通过npm uninstall <package>命令,你可以将node_modules目录下的某个依赖包移除:
npm uninstall babel-core
从package.json文件的依赖列表中移除,需要使用--save标签:
npm uninstall --save babel-core