DEV Community

loading...

Developing SSG using Node: Hello, World program

Kiran Chauhan
I design software with and for people.
・3 min read

This is the third article in series of Developing SSG using Node.

Let's open the text-editor and write the following code inside of it and save it with name hello.kiran. Yes, .kiran is the extension.

// file name: hello.kiran
console.log("hello, world");

Save it and open the terminal where you have saved this program to run.

$ node hello.kiran
hello, world

What I have done here is, I have passed the name of program ( hello.kiran ) to Node software that will interpret it and after successful interpretation it will dump the output in terminal.

Let me modify this program little bit as,

// file name: hello.kiran
console.log("hello, kiran");

Let's run the program again by typing same command as,

$ node hello.kiran
hello, kiran

Yes, .kiran is the extension of Node program and the reason is, I and Ryan Dahl ( the creator of NodeJS ) are good friends and I was the original person who inspire him to create a Node. So, to show the acknowledgment, Ryan used my name as extension of Node program. So, all your programs must have extension as .kiran. Any questions?

Just kidding!

Node is not a programming language. It is the platform, who understand the JavaScript code. Hence, it is written as NodeJS. So, when you see the word NodeJS, interprets it as Node is a platform that understand JS or JavaScript language. This is because, may be in future someone come-up with new language, which will be understand by Node! Who knows? So, what is inside of Node software that you download?

You know, your browser understand the HTML and the CSS and the JavaScript because they have engines for these languages for these languages to understand. For example, Stylo is the engine who understand the CSS for Firefox. Similarly, V8 is the engine in Google Chrome, who understand the JavaScript for Chrome. And this V8 engine is inside of Node. So, essentially, your Node software contains a big chunk of code from V8 JavaScript engine. And that is the reason why Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine is written on Node's official website. So, by default it understands the JavaScript code. If you know the C++ and internal structure of V8, you can create your own language within this Node or V8 platform ( Don't do that! we already have lots of languages ).

This is just a brief introduction and the internal structure of Node and we will dive into the internal structure details in third session. But, the point to remember is, Node is platform who understand JavaScript and don't care about the file name extension. Why? Because, do you really think that, .kiran is the extension of Node program? No, right!

Let's create another program called hello-var.js and write down the following code inside of it.

// file name: hello-var.js

const author = "Kiran";
console.log("Hello, " + author);

Let's run it by,

$ node hello-var.js
Hello, Kiran

If you have .js extension, you can omit it as,

$ node hello-var
Hello, Kiran

And it will give the same result. But, same is not true for other extensions such as, hello.kiran as by default Node assume that file will have .js extension. And if you try to run this hello.kiran without extension, it will give big messy error as,

$ node hello
internal/modules/cjs/loader.js:596
    throw err;
    ^

Error: Cannot find module '/home/kiran/downstream/noding/hello'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15)
    at Function.Module._load (internal/modules/cjs/loader.js:520:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
    at startup (internal/bootstrap/node.js:238:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

So, the conclusion is, please use .js extension for your program even if Node assume that your file have JavaScript code inside of it ( One of the regret that Ryan Dahl has, is to make extension optional! ).

Meanwhile, you know that, if we want to print something on console or terminal, we have to use console.log() method and to define a constant we have to use const keyword. These are the basic JavaScript concepts and I assume that you should know all these.

Discussion (0)