Automate the Boring Stuff with Node.js
  • Introduction
  • 退屈なことは Node.js にやらせよう
  • npm module を公開しよう
    • name-scope
  • node でコマンドラインツールを作ろう
    • node でコマンドラインツールを作ろう
    • 引数を受け取る
  • commander を使用した CLI の作成
    • commander の基礎
    • 装飾する
    • 各種 path を特定する module
Powered by GitBook
On this page
  • npm registry のアカウントを作ろう
  • package.json を作ろう
  • node module を作ろう
  • npm package を公開しよう
  • npm を インストールして使おう

npm module を公開しよう

Previous退屈なことは Node.js にやらせようNextname-scope

Last updated 7 years ago

npm registry のアカウントを作ろう

まずは npm registry のアカウントを作ります。terminal で以下のコマンドを実行します。

npm adduser

次の項目を聞かれるので、入力します。E-mail アドレスは公開される点に注意してください。

Username: superyusuke
Password:
Email: (this IS public) super.yusuke0000@gmail.com

mail の verification が送られてくるので、メールに添付された URL をクリックして verification しましょう。

package.json を作ろう

以下のコマンドを terminal で実行します。

npm init -y

これで全ての項目を仮に埋めた package.json ができます。

package.json
{
  "name": "your-package-name",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {},
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

name が特に重要で、これが公開されるパッケージ名になりますので、任意の名前に変更しましょう。

node module を作ろう

次に node module を作ります。

index.js
module.exports = val => {
  console.log(val);
};

npm package を公開しよう

npm publish --access public

これで npm registry にあなたの npm package が公開されました。

npm を インストールして使おう

先ほどのプロジェクトとは別の、新規プロジェクトを作って、先ほど公開した npm package をインストールして使用します。

npm install package-name -D

require で install した package を読み込んで使用します。

index.js
const yourModule = require("your-package-name");
yourModule("cant kkk");

以下のコマンドで実行しましょう。

node index.js

https://docs.npmjs.com/getting-started/publishing-npm-packages