본문 바로가기
Frontend/Node.js

[Node.js] 실행 파라미터 넘기기 (passing parameter)

by 지구 2021. 6. 25.

package.json 파일로 정의한 실행 명령어에서 내가 원하는 파일에게 내가 원하는 파라미터를 넘기고 싶으면 아래처럼 사용하면 된다.

// package.json
{
 "scripts" {
   "my-command": "node myFile.js one two three"
 }
}}

// myFile.js
const args = process.argv.slice(2);
console.log(args[0]); // one
console.log(args[1]); // two
console.log(args[2]); // three

 

핵심은 process.argv.slice(2) 이고 반환타입은 array 라는 점 :)

반응형

댓글