0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

npmパッケージの公開方法

Posted at

概要

初めてnpmパッケージを公開してみたのでその手順を記載します。

実装

npmを公開するだけであればgitを使用する意味はありませんが、定期的にメンテナンスしていきたいので使用します。

% git init

ディレクトリ

ディレクトリ構成は以下にしています。

├── package.json
├── tsconfig.json
├── README.md
└── src/
    └── index.ts

詳細

index.tsに実際のロジックを記載しています。

package.jsonは下記のように定義しています。

{
  "name": "offset-hours",
  "version": "1.0.1",
  "description": "Get a Date offset by N hours",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    "build": "tsc",
    "prepublishOnly": "npm run build"
  },
  "license": "MIT",
  "devDependencies": {
    "typescript": "^4.9.0"
  },
  "files": [
    "dist",
    "README.md",
  ]
}
"license": "MIT",

ライセンスはオープンソースとして扱いたかったのでMITに設定しています。

  "files": [
    "dist",
    "README.md",
  ]

"files": ["dist", "README.md"] は、npm パッケージを公開するときに どのファイル・フォルダだけを含めるか を指定する設定です。

dist/
ビルド済みの JavaScript(index.js)と型定義(index.d.ts)が入っているフォルダ。

README.md
npm/GitHub 上でパッケージの説明として表示されるマークダウンファイル。

公開

機能が作成できたら、実施にnpmに公開します。

https://www.npmjs.com/signup にアクセスし、アカウントを作成します。
スクリーンショット 2025-06-16 21.14.13.png

アカウントを作成できたら、ビルドして、npmに公開するコマンドを入力します。

% npm install
% npm run build
% npm publish

> offset-hours@1.0.0 prepublishOnly
> npm run build


> offset-hours@1.0.0 build
> tsc

npm notice
npm notice 📦  offset-hours@1.0.0
npm notice Tarball Contents
npm notice 415B README
npm notice 310B package.json
npm notice 512B src/index.js
npm notice 407B src/index.ts
npm notice 0B tsconfig.json
npm notice Tarball Details
npm notice name: offset-hours
npm notice version: 1.0.0
npm notice filename: offset-hours-1.0.0.tgz
npm notice package size: 808 B
npm notice unpacked size: 1.6 kB
npm notice shasum: 465e0c2c7fa4b57d724774957f70071db97e1a01
npm notice integrity: sha512-EIDrD6clXRNYb[...]vDSCh1IyK9j+g==
npm notice total files: 5
npm notice
npm notice Publishing to https://registry.npmjs.org/ with tag latest and default access
+ offset-hours@1.0.0
+ ```

公開できたら、画面右上のPacckagesをクリックして確認できれば公開成功です。
スクリーンショット 2025-06-16 21.26.10.png

以上です。

備考

公開したnpm
https://www.npmjs.com/package/offset-hours
Github
https://github.com/shintarou37/get-date

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?