后臺(tái)出身凡傅,因工作需要做react前端寸癌。一直好奇如何做一個(gè)npm包,前段時(shí)間也有類似需求不過用別的方式規(guī)避掉了死相。今天有點(diǎn)時(shí)間學(xué)習(xí)一下。
首先注冊(cè)npm賬號(hào) https://www.npmjs.com/
注冊(cè)完成后咬像,打開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)建一個(gè)文件夾,然后cd進(jìn)去npm init
县昂, 然后一路回車肮柜,會(huì)生成一個(gè)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)建一個(gè)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)建一個(gè)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
文件审洞,導(dǎo)出一個(gè)簡(jiǎn)單方法待讳。如下
moudule.exports = (...args) => {
return args.reduce((total, num) => {
return total + num;
}, 0);
};
保存以上內(nèi)容创淡,執(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).
擴(kuò)展
撤銷發(fā)布
npm unpublish --force
遇到的坑
- 登錄報(bào)錯(cuò)E500术辐,最后發(fā)現(xiàn)npm用的是淘寶鏡像網(wǎng)址。
npm config set registry https://registry.npmjs.org/
- 發(fā)布報(bào)錯(cuò)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ī)范導(dǎo)致的兴想,我猜是后邊的那一段數(shù)字 Package name triggered spam detection;
。