컴퓨터공학/기타 프로그래밍

[npm] link를 사용하여 개발중인 라이브러리 로컬에서 테스트하기

TaeGyeong Lee 2023. 9. 5. 01:09

개요

간단한 라이브러리를 개발하던 도중, 테스트해야 할 때가 있습니다. npm에 배포하기 전, 로컬 프로젝트에서 테스트할 수 있는 방법을 안내합니다.

 

전제

간단한 라이브러리의 코드는 아래와 같이 구성되어 있습니다. shelljs 라이브러리를 사용하여 터미널에 Hi를 출력하는 기능을 제공합니다.

[ package.json ]

...
"bin": {
    "script1": "bin/runScript1.js"
  },
 ...
 "dependencies": {
    "shelljs": "^0.8.5"
  },
  ...

[ bin/runScript1.js ]

#! /usr/bin/env node
var shell = require("shelljs");
shell.exec("echo Hi");

 

link

간단한 라이브러리의 루트 디렉토리에서 link 명령을 실행

cd 간단한라이브러리/
npm link

 

로컬 프로젝트에서 symlink 사용

cd 로컬프로젝트/
npm link 간단한라이브러리
npm i

위 명령 실행 후 로컬프로젝트의 yarn.lock파일에 간단한 라이브러리가 추가되어 있음을 확인할 수 있습니다.

 

명령 수행

로컬프로젝트에서 간단한 라이브러리의 명령을 수행할 수 있습니다.

cd 로컬프로젝트/
script1
Hi

 

unlink

cd 로컬프로젝트/
npm unlink 간단한라이브러리

 

참고 자료

 

npm Blog Archive: Building a simple command line tool with npm

The npm blog has been discontinued. Updates from the npm team are now published on the GitHub Blog and the GitHub Changelog. We asked what you planned to use private modules for, and one of the most common answers was command line tools for teams to use wh

blog.npmjs.org

 

Link Your Shared Component and Project Locally With npm link and Test Changes in Real-Time

Test changes in real-time by linking your component library and your project locally using a symlink. You can do this without publishing to npm with an npm link. Perfect for software testing and web development.

foundry.sparkbox.com