Hello Node - Our First Package
The Birth Certificate of a Project
Section titled “The Birth Certificate of a Project”Every living thing has DNA. Every citizen has an identity card. And every Node.js project has a package.json.
Without this file, your directory is just a folder of loose JavaScript files with no identity, no dependencies, and no purpose. It’s what transforms a “folder” into a “package.” It tells Node (and npm) exactly who your project is, who built it, and how to run it.
Figure 1: The package.json file determines the genetic makeup of your application.
Creating Life (npm init)
Section titled “Creating Life (npm init)”We could create this file manually, but we have tools for that. Open your terminal or PowerShell, navigate to your HelloNode directory, and run the initialization wizard:
npm initThis utility will ask you a series of questions. It’s an interview for your code.
- package name: Press Enter to accept
hellonode. (Pro-tip: npm hates capital letters). - version: Press Enter for
1.0.0. - description: “My first Node app” (or something equally profound).
- entry point: CRITICAL STEP. Type
app.jsand hit Enter. This tells Node where the brain of your application lives. - test command: Skip (Enter).
- git repository: Skip (Enter).
- keywords: Skip (Enter).
- author: Your name (Sign your work!).
- license: Press Enter (defaults to ISC).
When it asks Is this OK? (yes), hit Enter one last time.
[!NOTE] If you’re feeling dangerous (or lazy), you can run
npm init -yto skip the interview and accept all defaults immediately. But where’s the romance in that?
The Result
Section titled “The Result”You now have a package.json file in your root. It might look small now, but this JSON object is the heart of your project. We’ll be visiting it frequently to add dependencies (libraries we borrow) and scripts (commands we run).
App.js
Section titled “App.js”If you haven’t already, ensure you have your app.js file ready to go.
'use strict';console.log('Hello Node');Extra Bits & Bytes
Section titled “Extra Bits & Bytes”Hello Node Demo Repo
Official npm init Docs
⏭ Ignition Sequence Start
Section titled “⏭ Ignition Sequence Start”We have the DNA, now let’s spark the life into this Frankenstein’s monster.