avatarHussain Arif

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1173

Abstract

he words “Hello World” printed out on the console</p><h1 id="2c63">Executing code from a file</h1><p id="64b8">To run JavaScript code from a file, you can use :</p><div id="7293"><pre><span class="hljs-keyword">node</span> <span class="hljs-title"><filename</span>></pre></div><h2 id="fea7">Running code files located within the same directory as your CMD home directory</h2><p id="7b7f">If you have a script called “<i>myscript.js</i>” which is located within your <i>C:/Users/<user name> </i>directory, you will run the command:</p><div id="4b3c"><pre><span class="hljs-keyword">node</span> <span class="hljs-title">myscript</span>.js</pre></div><p id="be1c">or</p><div id="dafc"><pre><span class="hljs-keyword">node</span> <span class="hljs-title">myscript</span></pre></div><blockquote id="7a0b"><p>As you can see from the above commands, including the extension <b>does not</b> matter. However, this rule is<b> only</b> applicable if <i>myscript</i> is the JavaScript file, <b>not a directory</b></p></blockquote><h2 id="c5a0">Running Code from a file from a directory that is not the same as your CMD home directory</h2><h2 id="9efe">To run from parent di

Options

rectory</h2><div id="de99"><pre><span class="hljs-keyword">node</span> <span class="hljs-title">../myscript</span>.js</pre></div><h2 id="2f93">To run from other directory relative to CMD home directory</h2><p id="6a56">To run file <i>myscript</i> located in<i> C:/Users/<User name>/myfolder</i></p><div id="234c"><pre><span class="hljs-keyword">node</span> <span class="hljs-title">./myfolder</span>/myscript.js</pre></div><h2 id="e2f7">To run from an entirely different directory</h2><p id="bd46">Let’s say you want to run <i>myscript.js</i> from E:/nodejs/</p><div id="4e96"><pre><span class="hljs-keyword">node</span> E:<span class="hljs-title">/nodejs</span>/myscript.js</pre></div><p id="d504">In the <a href="https://readmedium.com/node-js-modules-exports-80d9b1bc2acf?source=friends_link&amp;sk=43c0391064fcd6d9026e972c28fe4d6b">next article</a>, we will dive deeper into the Node.JS engine to write scripts through utilization of modules and exports.</p><p id="d66c">All of the commands listed above are the ways you can run NodeJS scripts directly from the command line. Thank you so much for reading!</p><p id="aaaa">Stay home, Stay safe</p></article></body>

The NodeJS logo. Source:Wikipedia commons

How to Launch Node.JS scripts from the Terminal

In this page, we will learn to execute JavaScript files and code through the command line or the bash terminal

First , open your command line window. Here, I’m using the Windows Command Prompt. An alternative to the Windows Command Prompt is also Git Bash

To execute a single JavaScript line, you can use the command :

node -e  " <command> " 

An example can be:

node -e  " console.log('Hello World') " 

Hit the enter key, and you will see the words “Hello World” printed out on the console

Executing code from a file

To run JavaScript code from a file, you can use :

node <filename>

Running code files located within the same directory as your CMD home directory

If you have a script called “myscript.js” which is located within your C:/Users/<user name> directory, you will run the command:

node myscript.js

or

node myscript

As you can see from the above commands, including the extension does not matter. However, this rule is only applicable if myscript is the JavaScript file, not a directory

Running Code from a file from a directory that is not the same as your CMD home directory

To run from parent directory

node ../myscript.js

To run from other directory relative to CMD home directory

To run file myscript located in C:/Users/<User name>/myfolder

node ./myfolder/myscript.js

To run from an entirely different directory

Let’s say you want to run myscript.js from E:/nodejs/

node E:/nodejs/myscript.js

In the next article, we will dive deeper into the Node.JS engine to write scripts through utilization of modules and exports.

All of the commands listed above are the ways you can run NodeJS scripts directly from the command line. Thank you so much for reading!

Stay home, Stay safe

Recommended from ReadMedium