Stouter
Stouter

백엔드 개발자입니다.

NPM Package 배포하는 법

모노레포 환경에서 개발을 하다보면 자체적으로 라이브러리나 패키지를 구현할 때가 많다. 이러한 라이브러리를 다른 프로젝트들에서도 사용하려면 NPM으로 배포하는 것도 좋은 방안이 될 수 있다. 본 글에선 NPM에 내 라이브러리를 배포하는 방법을 정리해봤다.

Local PC 에서 Github Packages 배포

1. Github Access Token 준비

  • write:packages 권한이 있는 access Token 필요
    • 없다면 발급
    • 있다면 key 값 찾기


2. npm 에 로그인

  • 해당 컴퓨터에 github 및 npm 정보 저장해두기

    • ~./npmrc 파일에 해당 내용 저장
    1
    2
    3
    
    vim ~/.npmrc
    
    @[깃허브닉네임]:registry=https://npm.pkg.github.com/:_authToken=[1에서받은Token]
    
  • 다음 명렁어로 npm 에 로그인 및 github 연동

1
npm login --scope=@[깃허브닉네임] --registry=https://npm.pkg.github.com


3. 배포 패키지의 package.json 작성

  • 해당 package 의 root 위치에 다음과 같은 package.json 작성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
  "name": "@[깃허브닉네임]/[패키지명]",
  "version": "0.0.1",
  "description": "API for PROJECT",
  "main": "index.js", // entry 파일
  "typings": "index.d.ts",
  "publishConfig": {
    "@[깃허브닉네임]:registry": "https://npm.pkg.github.com/"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/@[깃허브닉네임]/[패키지명]"
  },
  "author": "comeintostout", // 마음대로
  "license": "MIT",
  "bugs": {
    "url": ""
  },
  "homepage": "https://www.instagram.com/moonjin_official/", // 마음대로
  "dependencies": {
    "@nestia/fetcher": "^2.4.3",
    "typia": "^5.3.4"
  }
}


4. npm 에 배포

1
npm publish --access=public