互联网技术 · 2024年2月15日

在npm上如何发布自己的包

1、创建npm账号

官网:https://www.npmjs.com/

创建账号入口:https://www.npmjs.com/signup

2、创建目录及初始化

$ mkdir ts-test

$ cd ts-test

$ ts-test> npm init

3、文件内容及目录结构

注意:在生成package.json中,name的名称和项目的名称保持一至

1.package.json

{

“name”: “ts-test”,

“version”: “1.0.0”,

“description”: “tianshan”,

“main”: “index.js”,

“scripts”: {

“test”: “echo “Error: no test specified” && exit 1″

},

“author”: “”,

“license”: “ISC”

}

2. 入口文件编写,index.js,加一句简单的打印

console.info(hello world);

3. 最终目录结构

├── ts-test

│ ├── index.js

└── └── package.json

4、发布

1.项目根目录下,运行npm addUser命令,添加自己的用户信息

ts-test> npm addUser

2. 如果已经注册过账号,直接登录就行了

ts-test> npm login

输入用户名、密码、邮箱

3. 发布

ts-test> npm publish

5、下载调用

1. 下载

ts-test> npm install zqh-test -D

2. 使用

require(ts-test);

3. 控制台执行

ts-test> node index.js

6、删除发布的包

ts-test> npm –force unpublish zqh-test

注意:超过24小时就不能删除了

7、废弃包(这个包并不会删除,只是在别人下载使用的时候会提示)

ts-test> npm deprecate –force ts-test@1.0.0 “这个包不在维护了。”

8、更新包

1、先把package.json里的version版本号修改了,再执行publish命令就行了

ts-test> npm publish

2、 更新(重新下载)

ts-test> npm install ts-test -D

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.

登录免费注册