Package Your node.js Projects Into a Standalone Applications with pkg
pkg allows you to take any of your node projects and bundle them up into standalone apps that you can ship to people or servers that do not have node.js installed. This lesson walks you through installing and using pkg on two simple projects.
Duration
1 min
Access
Free
Transcript
Available
pkg allows you to take any of your node projects and bundle them up into standalone apps that you can ship to people or servers that do not have node.js installed. This lesson walks you through installing and using pkg on two simple projects.
Video Transcript
John Lindquist: Starting from an empty folder, I will create an index.js. Inside of that, I'll console.log("hello world"). If I run this with node index.js, it logs out hello world. If I npm install -g pkg and use pkg to package up my index.js, I'm going to --target macos and name the --output hello-world, hit Enter.
I now have an app which I can run called hello-world, which will log out hello world. This app bundled up Node and my code inside of index.js. I can share this with someone who does not have Node installed.
If I want to add some Node packages I'll npm init -y from my package.json and npm install project-name-generator and then anything I require. We'll just say gen = require ("project-name-generator"). I'll console.log(gen()), which I'll invoke, .spaced, so the result of this when I node index.js is a generated name for a project. If I run that same package command on index.js --target macos, this time I will name it just gen.
Now I have an app I can share with anyone. They can just run gen and get a new name each time they run it. The goal here is to share your app with people or servers that don't have Node installed. You can still use the entire JavaScript ecosystem including Express or other servers and just bundle it up into a single app.