首页 > 随笔档案 > 上传自己创建的npm包

上传自己创建的npm包

Publish:

1、注册npmjs账号。记住自己的用户名、密码、邮箱

2、在电脑上登陆npm npm login

➜  ~ npm login
Username: levyleo
Password:
Email: (this IS public) levy@levyleo.cn
Logged in as levyleo on https://registry.npmjs.org/.

3、创建自己的npm包,

npm search npm_test_levyleo //先查询一下是否已经存在同名的npm包,必须保证自己使用的包名不存在,否则会提示没有权限(因为别人已经占用,会被认为你要修改别人的包)
No matches found for "npm_test_levyleo"

mkdir npm_test_levyleo //创建目录
cd npm_test_levyleo //切换至此目录下

➜npm_test_levyleo 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 json` 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: (npm_test_levyleo)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /Users/simple/npm_test_levyleo/package.json:

{
  "name": "npm_test_levyleo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this ok? (yes)

➜npm_test_levyleo //此时已经创建成功,需要添加自己的代码

4、添加代码

vi index.js

`
	module.exports = "hello";
`

保存。

5、发布到npm上

npm publish npm_test_levyleo

6、验证下载(在新建的项目目录下)

npm install npm_test_levyleo

7、撤回发布的npm包

npm --force unpublish npm_test_levyleo

8、撤回npm包后,删除安装的npm包时出错

No valid versions available for xxxx
在package.json对应的目录下是否有个package-lock.json。删除里面有关xxxx的数据即可。

声明: 本文采用 BY-NC-SA 授权。转载请注明转自: levy