猫史档案馆


【JS干货教程01】如何创建NPM包并上传到NPM上

用户:少年编程狂少年编程狂查看:0 回复:1 评论:0 创建时间:2020-11-17T21:50:13


center_image

 

什么是NPM包?

在你编写JS脚本/文件时,经常会用到 require("package-name") 来获取他人编写的 & 已经封装好的函数库,调用里面的函数。如果你想让别人也可以通过 require() 函数来获取你自己编写的函数库,就赶紧看 下面啊!!!

创建一个NPM账户

你在使用别人的函数库时(比方说express), 你首先得用 npm install express 从 NPM 下载 express 函数库的源码, 然后安装到 node_modules. 最后才能用 require() 函数调用。

得... 说到NPM。NPM是一个十分大的注册表,你可以将自己的包通过NPM官方的脚手架上传上去,也可以通过脚手架来下载他人上传的包

上传包?先注册:https://www.npmjs.com/

(网站在国外,挂在CloudFlare上,遇到人机验证不要慌)

 

创建源码仓库

先创建GIT仓库,一般来讲找个开源社区较好。一般来讲是GITHUB,当然如果你英语比较差建议去Gitee。不过Gitee的服务没有GITHUB好。并且GITHUB有很多的扩展市场为你的项目提供免费服务

确保有账号,创建个仓库,然后Clone下来。如果你想做的比较大的话,可以创建组织再在组织里创建仓库

在NPM上登入

确保你的计算机上安装了NodeJS,不知道怎么安装的要么去百度要么去吉吉喵哪里问。一般来讲NodeJS都附带了NPM,现在你有NPM账号,请在你的计算机上输入:

C:\Users\snbck\nearjs>npm adduser
Username: ceorleorn
Password:
Email: (this IS public) snbck@qq.com

请记得把信息改成自己的,注意这里需要网络

在仓库里创建Package.json

呃呃,Package.json包括了整个NPM包的大量信息,你可以用脚手架来创建它:

C:\Users\snbck>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: (snbck) your-package-name
version: (1.0.0)
description: Your Description ...
entry point: (index.js)
test command: exit 0
git repository: https://github.com/<user>/<repo>
keywords: Your-Keywords
author: Your name or org-name
license: (ISC) MIT
About to write to C:\Users\snbck\package.json:

{
  "name": "your-package-name",
  "version": "1.0.0",
  "description": "Your Description ...",
  "main": "index.js",
  "scripts": {
    "test": "exit 0"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/user/repo.git"
  },
  "keywords": [
    "Your-Keywords"
  ],
  "author": "Your name or org-name",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/user/repo/issues"
  },
  "homepage": "https://github.com/user/repo#readme"
}


Is this OK? (yes) yes

发布:

npm publish

你就可以在 npmjs.com 上看到了

 

========================== 这里是分割线 ==========================

 

关于回坑这件事:

 

并没有说回坑,以后也不会再发BCM的作品了。但我依旧会在社区更新一些小教程和技巧,或者发布一些新作品(指Python和Node)介绍,尽可能的去回馈社区。

 

另外 极客工作室 也不会再管理了。我已经加入了 源码星球 和 瞳星社,创建了新的工作室 IsClub (在Github上做开源) 

 

(开头的那张图是IsClub年底的项目LOGO喽,敬请关注)


回复

上一页1 页 / 共 1下一页
天马神坑_真天马神坑_真

顶顶

点赞0


评论