LoginSignup
1
1

More than 5 years have passed since last update.

npm module を公開しよう

Posted at

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

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

まずは 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

{
  "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 を作ろう

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("test test test");

以下のコマンドで実行しましょう。
console に表示されるはずです。

node index.js
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1