Your First Node.js Program
๐ Covered in this chapter:
Creating app.js ยท node app.js ยท console.log() ยท process.argv ยท process.cwd() ยท process.version
Write, save, and run your very first Node.js file, and explore console.log() and the built-in process object.
1Your First Node.js Program โ What You'll Learn
Write, save, and run your very first Node.js file, and explore console.log() and the built-in process object.
Here's everything this chapter covers, in the order you'll learn it:
- Creating an app.js file
- Running a file with node app.js
- console.log() basics
- Reading command-line output
- process.exit()
- The process global object
- process.argv (command-line arguments)
- process.cwd() (current working directory)
- process.version
- Basic debugging with console.log
2Working Example
๐ป Example: Your First Node.js Program
JavaScript
โถ Run in Compiler
console.log("Hello, Node.js!");
console.log("Version:", process.version);
console.log("Arguments:", process.argv);
3Best Practices & Common Pitfalls
๐ก Key things to remember:
- process.argv[0] is the node binary path, process.argv[1] is your file path โ your actual arguments start at index 2.
- console.log() accepts multiple comma-separated arguments and prints them space-separated, which is handy for quick debugging.
โ Frequently Asked Questions (FAQ)
Q What's the most important thing to understand about your first node.js program?
Focus on: Creating app.js ยท node app.js ยท console.log() ยท process.argv ยท process.cwd() ยท process.version. These are the core building blocks this chapter's examples are built around, and they show up repeatedly in later chapters of this course.
Q Do I need external npm packages for your first node.js program?
Only where explicitly shown in the code examples above (like Express, Zod, or Socket.IO) โ otherwise, this chapter relies entirely on Node.js's own built-in capabilities.