npm-发布

初始化项目

  1. 新建目录并初始化
1
2
3
mkdir publish-test
cd publish-test
npm init -y
  1. 修改 package.json 信息,如:
1
2
3
4
5
6
7
8
9
10
11
12
13
{
"name": "publish-test",
"version": "1.0.0",
"description": "first publish test.",
"main": "index.js",
"script:": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"keywords": [],
"author": "auser",
"license": "ISC"
}
  1. 创建 index.js 文件
1
console.log('publish-test', 'index.js')

注册 npm 账号

  1. 前往官网注册账号: https://www.npmjs.com/

  2. 确保npm源为官方库

1
npm config set registry https://registry.npmjs.org

发布

  1. 执行发布命令
1
npm publish
  1. 打开个人页面查看发布信息

其他项目中安装

  1. 创建测试项目
1
2
3
mkdir publish-test-install
cd publish-test-install
npm init -y
  1. 安装项目
1
npm install --save-dev publish-test
  1. package.json 文件查看是否安装成功,并在 scripts 下添加指令
1
2
3
4
5
{
"scripts": {
"start": "node index.js"
}
}
  1. 创建 index.js
1
require('publish-test')
  1. 运行测试
1
2
3
npm start
# 或
node index.js