Package Your node.js Projects Into a Standalone Applications with pkg

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

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.

John Lindquist: [0:00] 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.

[0:28] 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.

[0:41] 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.

[1:10] 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.

egghead
egghead
~ 36 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today