如何发布npm包

299 阅读1分钟

这是一个技术总结帖,如何发布一个简单的npm包并安装,使用它,然后删除掉。

注册npm账号

首先在npm官网注册一个账户

创建npm包

创建一个空文件夹,执行npm init

package name: (npm-public) npm-test
version: (1.0.0)
description: this is a test
entry point: (hello.js)
test command:
git repository:
keywords:
author: kim
license: (ISC) MIT
About to write to /Users/mac/dongzhiqin/npm-public/package.json:

{
  "name": "npm-test",
  "version": "1.0.0",
  "description": "this is a test",
  "main": "hello.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "dongzhiqin",
  "license": "MIT"
}


Is this OK? (yes) yes

填写一下descriptionnameauthor,然后登陆npm,执行npm adduser,填写刚才注册的用户名和密码。 创建一个hello.js文件,简单输出一个hello world就行。

exports.sayHello = function () {
    return 'Hello, world.';
};

发布npm包

登陆完成之后可以发布了,执行npm public .,会执行发布到npm官网的动作。我在这边碰到了点问题。

npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT https://registry.npmjs.org/npm-test - You do not have permission to publish "npm-test". Are you logged in as the correct user?
npm ERR! 403 In most cases, you or one of your dependencies are requesting
npm ERR! 403 a package version that is forbidden by your security policy.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/mac/.npm/_logs/2021-02-06T09_28_44_843Z-debug.log

这个问题一般是包名重复导致的,后面我修改了包名为dongzhiqin就上传成功了。

$ npm publish .
npm notice
npm notice 📦  dongzhiqin@1.0.0
npm notice === Tarball Contents ===
npm notice 64B  hello.js
npm notice 230B package.json
npm notice === Tarball Details ===
npm notice name:          dongzhiqin
npm notice version:       1.0.0
npm notice package size:  326 B
npm notice unpacked size: 294 B
npm notice shasum:        a6919f2eed104070dc353c7325dfc5972c9ef168
npm notice integrity:     sha512-mgx6G4Q++itS5[...]VC9uMezuLTJNA==
npm notice total files:   2
npm notice
+ dongzhiqin@1.0.0

去官网搜一下,确实可以搜到我们上传的包,已经发布成功了

安装npm包并使用

现在可以测试一下。
新建一个文件夹,执行npm init -y迅速创建一个package.json。

执行npm install dongzhiqin

看下package.json里面已经多了这个包

{
  "name": "npm-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dongzhiqin": "^1.0.0"
  }
}

我们来使用一下。创建一个index.js,内容为

const { sayHello } = require('dongzhiqin')
console.log(sayHello())

保存退出,在terminal执行node index.js看到返回了hello world


mac at yuansus in ~/dongzhiqin/npm-test
$ node index.js
Hello, world.

删除npm包

删除这个测试用的包吧npm unpublish dongzhiqin --force

参考


npm config get registry  // 查看npm当前镜像源

npm config set registry https://registry.npm.taobao.org/  // 设置npm镜像源为淘宝镜像

yarn config get registry  // 查看yarn当前镜像源

yarn config set registry https://registry.npm.taobao.org/  // 设置yarn镜像源为淘宝镜像
npm --- https://registry.npmjs.org/

cnpm --- https://r.cnpmjs.org/

taobao --- https://registry.npm.taobao.org/

nj --- https://registry.nodejitsu.com/

rednpm --- https://registry.mirror.cqupt.edu.cn/

npmMirror --- https://skimdb.npmjs.com/registry/

deunpm --- http://registry.enpmjs.org/

参考的是小火柴的前端小站-nodejs

发现一个不错的系列文章npm发布包教程