做一个npm包

后台出身,因工作需要做react前端。一直好奇如何做一个npm包,前段时间也有类似需求不过用别的方式规避掉了。今天有点时间学习一下。

首先注册npm账号 https://www.npmjs.com/
注册完成后,打开cmd执行一下登录操作

C:\Users\Administrator>npm config get registry
https://registry.npm.taobao.org/

C:\Users\Administrator>npm config set registry https://registry.npmjs.org/

C:\Users\Administrator>npm login
Username: she***
Password:
Email: (this IS public) she***33@qq.com
Logged in as shenbo on https://registry.npmjs.org/.

登录成功之后,创建一个文件夹,然后cd进去npm init, 然后一路回车,会生成一个package.json的文件。

E:\02_Developer\11_ReactProjects\shenbotest>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: (shenbotest)
version: (1.0.0)
description: 创建一个npm包
entry point: (index.js)
test command:
git repository:
keywords:
author: shenbo
license: (ISC)
About to write to E:\02_Developer\11_ReactProjects\shenbotest\package.json:

{
  "name": "shenbotest",
  "version": "1.0.0",
  "description": "创建一个npm包",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "shenbo",
  "license": "ISC"
}
Is this OK? (yes)

然后创建index.js文件,导出一个简单方法。如下

moudule.exports = (...args) => {
  return args.reduce((total, num) => {
    return total + num;
  }, 0);
};

保存以上内容,执行 npm publish命令,发布到npm中。

PS E:\02_Developer\11_ReactProjects\shenbotest> npm publish
npm notice
npm notice package: shenbotest@1.0.0
npm notice === Tarball Contents ===
npm notice 113B index.js
npm notice 230B package.json
npm notice === Tarball Details ===
npm notice name:          shenbotest
npm notice version:       1.0.0
npm notice package size:  374 B
npm notice unpacked size: 343 B
npm notice shasum:        3ba6c5088f6e00a173c08c26dd112089cbd598ae
npm notice integrity:     sha512-q9H84hZQ0RzzZ[...]oCtlpTn1DTopg==
npm notice total files:   2
npm notice
+ shenbotest@1.0.0

收到邮件

Hi Mr.shen!
A new version of the package shenbotest (1.0.0) was published at 2021-05-23T03:17:57.880Z from
222.90.159.89\. The shasum of this package was 3ba6c5088f6e00a173c08c26dd112089cbd598ae.

If you have questions or security concerns, you can reply to this message or
email [support@npmjs.com](mailto:support@npmjs.com).

扩展

撤销发布
npm unpublish --force

遇到的坑

  1. 登录报错E500,最后发现npm用的是淘宝镜像网址。
npm config set registry https://registry.npmjs.org/
  1. 发布报错E403,如下
npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT https://registry.npmjs.org/shenbo-npm-package-test-20210523 - Package name triggered spam detection; if you believe this is in error, please contact support at https://npmjs.com/support
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!     C:\\Users\\Administrator\\AppData\\Roaming\\npm-cache\\_logs\\2021-05-23T03_13_50_524Z-debug.log

最后发现是文件名不规范导致的,我猜是后边的那一段数字 Package name triggered spam detection;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容