Alright, so you've gotten yourself started with nodejs, and now have a dope little hello world program. We're going to extend on that, and keep building up some best practices. I'll touch super briefly on functions and variables, just to make the example of how a package is used.
NPM stands for node package manager. It lets programmers easily keep track of their projects, share them, and bring in other peoples code libraries to yours!
NPM comes with your nodejs install, so it's already sitting there, waiting for you to use it. So, let's do!
You're going to initiate an npm package file. It's pretty easy.
Get into your terminal, in the root of your hello-world folder. Once you've got terminal open, type in
npm init
and npm will give you a nice little walkthrough for settings up a file names package.json
. Like your name as the author, the version of your app, and a really handy main pointer. This points to the main file in your application. So you don't have to type node index.js
any more, you can just type node .
and node will read that you want to run index.js. It sounds silly, but I swear it's the best.
You can always come back and edit this stuff in package.json
.
With all that setup, let's add a package!!!!
Delay is a simple little package you can add to your app that lets you delay your program from running by a number of milliseconds that you choose.
Type this into your terminal
npm install delay
This is going to have node pacakage manager go out to npmjs.org and look for a package named 'delay'. All pacakge names are unique, which is a requirement for npm.
This will do a couple things to your application folder. It's going to make a folder called node_modules
which is where the code for delay will live. Then it will make another file called package-lock.json
. The lock file is designed to make sure that when your app is done, the specific versions of your packages are maintained when you share your app with others.
In package.json
you'll also see a new entry called dependencies. That will list out delay now.
Now, let's show how you use a package you've brought into your app.
In your index.js
file, you'll import that code using the require
keyword. At the top of your app (the very first line) put in const delay = require('delay').default;
What the heck is that??
Well what we're doing here is declaring a variable. This is an important concept. Variables are pointers to stuff. Like a pointer to a number, or a string. You're going to use them a ton.
There's two types of variables you'll see all over the place in JavaScript. const
and let
. You will also see var
sometimes when you're out there googling in the wild. Ignore it. If you see something that wants you to use var
, use let
instead. Everytime. const
is for variables that will not change after you declare what they are. let
can change whenever. In our case, we've made a constant variable called delay
that points to the default export from the delay
package, using the require
method. Pretty cool right?
Once we've got that, we can use it.
The next thing we're going to do, is copy and paste the below! It's bit different than what you've written so far, and it goes into asynchronous code. That's a bigger topic.
For now, just go with it.
const delay = require('delay').default;
async function main() {
console.log("Hello");
await delay(2000);
console.log("world!");
}
main();
Alright! You give that a run now, using node .
(remember, you declared the main file of your program, so you can use the .
as a short hand to it) in terminal, and you'll see somethinig new. Your app will print out Hello, then wait around 2 seconds (remember, it's measuring in milliseconds, ie, 1000ms per second), then print world! After that, the app is done.
Let's talk about that function declaration real quick though (super short version).
Functions do repeatable things. You're going to write a ton of those too. They get declared as well by doing function someFunctionName() {}
. In our case, it's called main
then it's run below, as the last line of code, by calling main();
.
Next up, variables and types.