Written by:
The most common way to run a javascript file would be:
1node ./filename.js
There are many ways to execute javascript locally, without the need of internet or hosting our web apps on a server. Here are a few ways to do this:
If you are in the need to interact for debugging or testing a running web app on a server, or locally, you can make use of the built-in browser console.
Here´s an example:
If you don´t want to use the browser built-in terminal, you can always choose the command line to run javascript. For the command line to be able to execute javascript code, you´ll need to install NodeJS (or just Node as referred usually).
Node.js is a JavaScript engine that allows us to execute and write javascript for front-end, back-end o full-stack apps. Node it´s the go-to solution for most cases when we want to develop web apps for most developers (myself included).
Installing NodeJS on your system is pretty straightforward:
We downloaded and installed NodeJS on our system, now let´s run some javascript!
node
Now we started the node environment and we have access to javascript. Here´s an example of javascript executed on the command line
So, I understand that you want to run javascript locally for developing purposes, let´s dig in in how to start a javascript project from your favorite IDE. We´ll be using Visual Code, but the process is the same for all of them.
We already covered how to install NodeJS, the javascript engine we´ll need to execute our code. Having this step been taken care of, lets dig in in how to run javascript from your IDE.
mkdir nodeTest
)cd nodeTest
npm init -y
npm
stands form Node Package Managerinit
stands for initialize/initiate-y
is to apply the default values and start, you can change them later or run the code without the -y
and see for yourself the given optionsAfter it finishes, a package.json
will show on your files, and if you open it, it´ll have all the data we manually specified by omitting the -y
flag or the generic information which can be edited anytime we want.
Next, we need to create our javascript file and start coding.
New file
local.js
(js is the javascript extension)To run your app, you can do it by writing on the terminal:
1 $ node local
Remember to take out the $
when writing on the terminal.
We use node
to summon our node engine and we tell which file we want to run, in this how-to we named our file local
, that’s why node local
.
Well now, even if we manage to execute our app, this is not the most common way to do it. Remember the package.json
we created with npm init -y
? Well, we´re editing it now to run our app. We need to add to our package.json
the following:
1 "start": "node local"
We´ll add it on the "scripts"
section, start
will be the keyword we´ll use to tell node to run local
, our javascript file we created on the previous steps.
To run our app, all we need to do now is write in the IDE built-in terminal
npm run start
Here´s a visual example of how the package.json
file:
1{ 2 "name": "nodetest", 3 "version": "1.0.0", 4 "description": "", 5 "main": "index.js", 6 "scripts": { 7 "test": "echo \"Error: no test specified\" && exit 1", 8 "start": "node local" 9 }, 10 "keywords": [], 11 "author": "", 12 "license": "ISC" 13}
Now, every time we run npm run start
, node will run our local.js
file.
We covered how to run javascript in different environments, from the browser´s console to a fully installed NodeJS running with Visual Code (or the IDE of your choice).
You can check the 4Geeks Blog and will find more valuable content like How to print in Javascript or How to call a Function in Javascript. Hope you enjoyed the reading and keep on the Geek side!