0.0.3 • Published 4 years ago
lf-npm-test v0.0.3
发布npm包
项目托管地址
https://gitee.com/windRainCode/lf-npm-test
参考文章
手把手教你用npm发布包: https://blog.csdn.net/taoerchun/article/details/82531549
如何使用npm发布自己的npm包:https://blog.csdn.net/zyg1515330502/article/details/81112653
01-注册npm账户
02-本地登录npm
命令
npm login
查看npm
1.查看当前的镜像地址
npm get registry 设成淘宝的
npm config set registry http://registry.npm.taobao.org/将npm替换为cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org安装yarn(全局安装yarn)
cnpm install yarn -g 2.换成原来的
npm config set registry https://registry.npmjs.org/针对mac 设置默认node版本
首先切换到 你的版本号
nvm use 8.10.0
之后设置微默认的版本
nvm alias default 8.10.0
03-创建项目
npm init文件内容
{
  "name": "lf-npm-test",
  "version": "0.0.1",
  "description": "一个测试用npm包",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "lf"
  ],
  "repository": {
    "type": "git",
    "url": "https://gitee.com/windRainCode/lf-npm-test.git"
  },
  "author": "linFen",
  "license": "ISC"
}04-编写代码
index.js
// 计算总数
sum = function () {
  let res = 0;
  for (let i = 0; i < arguments.length; i++) {
    res += arguments[i];
  }
  return res;
};
exports = {
  sum,
};05-上传npm包
npm publish上传完之后可以在npm中看到自己的包

06-项目中使用包
安装包
npm install --save lf-npm-test使用包
let { sum } = require('lf-npm-test');
console.log(sum(1, 2, 3, 4, 5));
07-其他情况
07-A 更新已经发布的包
第一步、修改包的版本
npm version patch该命令在原来的版本上自动加1,实际上是将package.json文件中的version值修改了。
第二步、重新发布包
npm publish07-B 删除已发布的包
删除指定的版本
npm unpublish 包名@版本号2、删除整个包
npm unpublish 包名 --force