做一個npm包

后臺出身,因工作需要做react前端。一直好奇如何做一個npm包,前段時間也有類似需求不過用別的方式規(guī)避掉了。今天有點時間學習一下。

首先注冊npm賬號 https://www.npmjs.com/
注冊完成后,打開cmd執(zhí)行一下登錄操作

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/.

登錄成功之后,創(chuàng)建一個文件夾,然后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: 創(chuàng)建一個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": "創(chuàng)建一個npm包",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "shenbo",
  "license": "ISC"
}
Is this OK? (yes)

然后創(chuàng)建index.js文件,導出一個簡單方法。如下

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

保存以上內容,執(zhí)行 npm publish命令,發(fā)布到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).

擴展

撤銷發(fā)布
npm unpublish --force

遇到的坑

  1. 登錄報錯E500,最后發(fā)現(xiàn)npm用的是淘寶鏡像網址。
npm config set registry https://registry.npmjs.org/
  1. 發(fā)布報錯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

最后發(fā)現(xiàn)是文件名不規(guī)范導致的,我猜是后邊的那一段數字 Package name triggered spam detection;

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容