如何使用npm打包发布nodejs程序包

2025-04-14 14:47:22
推荐回答(2个)
回答1:

 一、注册自己的npm账户
  在 npm 官网申请一个账号,并且进行必要的验证,需要填写用户名、密码和邮箱。并且注册成功后会向邮箱发一封邮件,需要进行验证。

  二、添加用户名到npm环境中
  因为添加的是npm用户,所以需要指定registry为npm。这主要是区分本机已经安装了其它仓库例如cnpm的情况。

  根据提示输入用户名、密码、邮箱,其中密码不会显示的

  三、打包node项目
  发布的node项目需要有package.json,可以使用npm init命令,再根据提示输入相应的信息,最后完成打包即可。

回答2:

在npm官网 https://www.npmjs.org 申请一个账号:

增加你申请完成的用户名到npm环境中。

$ npm adduser
Username: your name
Password: your password
Email: yourmail[@gmail](/user/gmail).com

初始化package.json 文件。

$ npm init

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (index.js) /*模块名称确保和现有库不重名, 可以npm install 检查*/
version: (0.0.0) 0.1.0
description: An easy mongodb client for node.js based on native mongodb driver.
entry point: (index.js) /*模块执行入口*/
test command: make test
git repository: https://github.com/shy2850/node-server
keywords: node server build f2e
author: shy2850
license: (BSD-2-Clause) MIT

发布:

$ npm publish

这样就完成了,以后更新版本只需要修改package里面的version版本信息【递增】就可以了。