DEVLOG

[nodeJS] child_process.execSync :: node에서 터미널명령 실행하기 본문

others/node

[nodeJS] child_process.execSync :: node에서 터미널명령 실행하기

meroriiDev 2021. 10. 19. 16:59
728x90
반응형

 

 

child_process npm 설치

 

$ npm i child_process

 

 

https://www.npmjs.com/package/child_process

 

child_process

This package name is not currently in use, but was formerly occupied by another package. To avoid malicious use, npm is hanging on to the package name, but loosely, and we'll probably give it to you if you want it.

www.npmjs.com

 

 

child_process spawn()과 exec()두가지가 있는데 이 둘의 차이점은 링크에서 확인할 수 있다.

간단하게 가장 큰 차이점은 spawn은 stream으로, exec는 buffer로 작동한다는 점이다.

 

나는 여기서 가볍게 한두개의 명령문만 실행할 예정이라 exec중에서도 동기방식인 execSync()를 사용했다.

 

 

const { execSync } = require("child_process");

 

 

execSync('터미널 명령문');
ex)

execSync(`cp ~/Downloads/${branch}.xlsx ../data/${branch}.xlsx`);
execSync(`rm ~/Downloads/${branch}.xlsx`);
execSync(`node ../test.js ${branch}.xlsx ${date} ${branch}`);
execSync(`cp -R ../public/${date} ~/desktop/test/${branch}`);

 

 

execSync(`start C:\\...경로...\\test\\${branch}\\${date}`);

윈도우에서 스크립트를 실행해서 그런지 터미널 언어와 윈도우 cmd언어 모두 사용해도 정상 작동이 된다..!!

 

 

728x90
반응형

'others > node' 카테고리의 다른 글

mac OS 특정 버전 node설치, 다운그레이드 하기  (0) 2021.11.29
Comments