pkg를 이용한 바이너리 사용

00_8_3·2022년 7월 13일
0

pkg

개인 pc에 node가 설치 안되어 있는경우
각 os 별 바이너리 파일로 만들어주어 실행 가능케한다.
pkg

사용방법

  • pakage.json
"scripts": {
	"pkg": "pkg ."
},

"bin": {
  "app": "./intermediate/index.js" // 바이너리를 만들기 위한 엔트리 포인트
},
"pkg": {
	"targets": ["node16-linux-x64"],
  	"outputPath": "dist"
}
  • 실행
$ npm run pkg

or

$ pkg .
$ pkg . --out-path dist // dist 디렉토리에 바이너리 생성
$ pkg . --targets node16-linux-x64 // OS 플랫폼을 설정

주의사항

  • 디렉토리 접근방법

Snapshot filesystem
During packaging process pkg collects project files and places them into executable. It is called a snapshot. At run time the packaged application has access to snapshot filesystem where all that files reside.

Packaged files have /snapshot/ prefix in their paths (or C:\snapshot\ in Windows). If you used pkg /path/app.js command line, then filename value will be likely /snapshot/path/app.js at run time. dirname will be /snapshot/path as well. Here is the comparison table of path-related values:

value with node packaged comments
filename /project/app.js /snapshot/project/app.js
dirname /project /snapshot/project
process.cwd() /project /deploy suppose the app is called ...
process.execPath /usr/bin/nodejs /deploy/app-x64 app-x64 and run in /deploy
process.argv[0] /usr/bin/nodejs /deploy/app-x64
process.argv[1] /project/app.js /snapshot/project/app.js
process.pkg.entrypoint undefined /snapshot/project/app.js
process.pkg.defaultEntrypoint undefined /snapshot/project/app.js
require.main.filename /project/app.js /snapshot/project/app.js
Hence, in order to make use of a file collected at packaging time (require a javascript file or serve an asset) you should take filename, dirname, process.pkg.defaultEntrypoint or require.main.filename as a base for your path calculations. For javascript files you can just require or require.resolve because they use current dirname by default. For assets use path.join(dirname, '../path/to/asset'). Learn more about path.join in Detecting assets in source code.

On the other hand, in order to access real file system at run time (pick up a user's external javascript plugin, json configuration or even get a list of user's directory) you should take process.cwd() or path.dirname(process.execPath).

출처

https://github.com/vercel/pkg

0개의 댓글