NPM
Node.js의 패키지를 관리할 수 있는 도구
참고 사이트)
npm
Bring the best of open source to you, your team, and your company Relied upon by more than 17 million developers worldwide, npm is committed to making JavaScript development elegant, productive, and safe. The free npm Registry has become the center of Java
www.npmjs.com
#npm install
npm i -g 패키지명 // 전역설치
npm i 패키지명 // 지역설치
배포한 파일을 받는 명령어 이다.
i (install)로 표현가능하다
-g (global)로 표현되고 module 최상위에 설치되어 쓸 수 있다.
지역설치를 하면 해당 폴더 안에서만 사용 가능하다.
사용 예시)
npm i figlet
figlet이라는 패키지를 설치해보았다.
설치한 패키지는 node_modules안에 있다.
https://www.npmjs.com/package/figlet
figlet
Creates ASCII Art from text. A full implementation of the FIGfont spec.. Latest version: 1.6.0, last published: 23 days ago. Start using figlet in your project by running `npm i figlet`. There are 8421 other projects in the npm registry using figlet.
www.npmjs.com
해당 문서를 참고하여 진행보았다.
const figlet = require("figlet");
figlet("Hello World!!", function (err, data) {
if (err) {
console.log("Something went wrong...");
console.dir(err);
return;
}
console.log(data);
});
index.js 코드는 이렇다.
확인해보니 잘 출력되었다.
#npm init
package.json을 생성하기 위해 npm init을 해야한다.
npm init을 입력하면
이름, 버전, 설명 등등 관련 메타와
시작점을 지정할 수 있다. (index.js)
전부 입력하고 yes를 하면
package.json 파일이 생성된다
안을 들여다보면
{
"name": "pkg",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"figlet": "^1.6.0"
}
}
생성할 때 입력한 값이 있고
패키지를 설치하게 되면
dependencies라는 데이터가 생기고
설치 할때마다 설치한 패키지 정보가 담긴다.
그렇다면 package.json 은 무슨 역할을 할까?
만약 프로젝트를 배포하게 되면 node_modules 파일은 무겁기 때문에 git이나 형상 관리가 쉽지않다.'
그래서 package.json 파일을 배포 하게되는데
해당 폴더에서 npm i를 해주면
dependencies에 있는 패키지들이 모두 설치가된다.
'nodejs' 카테고리의 다른 글
[Node.js] express 라우팅 기초(get 요청, post 요청) (0) | 2023.05.03 |
---|---|
[node.js] express 시작 (0) | 2023.05.03 |
[node.js] module.exports (0) | 2023.04.28 |
[node.js] process, file system(fs) (0) | 2023.04.22 |
[node.js] 설치, REPL, 파일 실행 (0) | 2023.04.21 |