一分鐘釋出一個屬於自己的npm包

x619y發表於2018-04-21

如何釋出一個npm包

1.註冊npm賬戶

登陸 https://www.npmjs.com 用郵箱註冊接收驗證郵件驗證就完事了
(此處有兩封郵件:Welcome to npm和Verify your npm email address )

2.選擇目標資料夾,命令列cd進去

E:\xxx>cd test
E:\xxx\test>

3.執行npm init

輸入以下命令,會提示配置包的相關資訊,名稱版本等等,都是包的基本配置資訊

 E:\xxx\test> 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> --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: (npm-publish)                       //包名
version: (1.0.0)                          //版本號
description:                              //描述
entry point: (index.js)                   // 入口檔案
test command:
git repository:                          //git倉庫地址
keywords:                                //關鍵字
author:                                  //作者
license: (ISC)                           //開源協議
About to write to E:\learn\node\npm-publish\package.json:

{
  "name": "fashaoge",
  "version": "0.0.1",
  "description": "the npm pulish test ��, only a test ,please don't install",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "test"
  ],
  "author": "hu-qi",
  "license": "ISC"
}


Is this ok? (yes)

4.配置完畢,會生成package.json檔案,開始寫包的程式碼

最後一句 :Is this ok? (yes)
建立index.js檔案 // 寫 module.exports = 123456789;

5.釋出包:npm publish 包名

登陸npm

 E:\xxx\test>npm login 
 E:\xxx\test>Username: yourname
 E:\xxx\test>Password: yourpassword
 E:\xxx\test>Email: (this IS public) youremail 
E:\xxx\test>npm publish 

題外話

一、遇到了一個小小的錯誤 就是沒有通過驗證就發包 結果報錯了

npm ERR! publish Failed PUT 403
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\                                                                                            node_modules\\npm\\bin\\npm-cli.js" "publish"
npm ERR! node v6.3.0
npm ERR! npm  v3.10.3
npm ERR! code E403

npm ERR! you must verify your email before publishing a new package: https://www                                                                                            .npmjs.com/email-edit : 

二、包的刪除

npm --force unpublish 包名

原文連結:https://segmentfault.com/a/1190000010399761

相關文章