How to run JavaScript in the terminal?
JavaScript is the most popular language right now in the world, a high-level dynamic programming language used to create dynamic web pages, build web servers, develop server applications, etc. In this article, we will learn about different ways to run JavaScript programs in the terminal.
Introduction
Generally, To run JavaScript files in the browser we create an index.html file under the script tag we write our code. Or you can create a new JavaScript file and link it to your HTML file under the script tag. For example, to the print hello world this is the code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Codedamn</title>
</head>
<body>
<script>
console.log("hello world");
</script>
</body>
</html>
Code language: HTML, XML (xml)
To see the output open the HTML file and open the developer console then you can see the output or in you can open the console directly by clicking “ctrl+shift+j” on windows or “command +option+j” on mac. Also by right-clicking inspect > console.
What if I tell you there are other easy ways to run JavaScript code simply using the terminal? You do not need to do all the processes mentioned above. In this article, we will learn how to run JavaScript code in a terminal using Nodejs and BUN.
Node js
NodeJs is an open-source platform and a runtime environment for executing JavaScript code. In essence, the node is a program that includes a v8javascript engine plus some modules that let us work with file systems and networks we can’t with a browser. We often use Node to build Backend applications(APIs – Application Programming Interface). Like chrome, the node also uses the same V8
JavaScript engine but they provide different run time environments for JavaScript. Node is ideal for I/O-intensive apps because its applications are asynchronous by default so we can serve more clients that’s why the applications are highly scalable. Before getting into the download section first check whether it is there in your local machine or not, go to your command prompt and enter the following command
node --version
Code language: Bash (bash)
If it is not downloaded. Download it from here nodejs once again run the command node --version
on your terminal. To check whether it’s appropriately downloaded or not. The latest version is 19.0.1 but I recommend you use the earlier version as it will have no bugs.
Now open your command prompt and enter the command node. The version you are using prints on the screen.
C:\Users> node
Welcome to Node.js v18.12.1.
Type ".help" for more information.
>
Code language: JavaScript (javascript)
Now You can enter your code here. For example to print hello world
C:\Users> node
> console.log("hello world")
hello world
Code language: JavaScript (javascript)
Well it’s a bit difficult to write code in the terminal, isn’t it? You can write the source code in your favourite IDE and can run it through the node. Let me show you how to do that. First, let’s create an app.js file and then write your source code in your IDE.
Open the terminal then navigate to your app.js file through basic commands like cd. Then enter node app.js . For example, I created an app.js file to print hello world in the code directory which is a sub-directory of the Desktop directory
C:\Users\Desktop>cd code
C:\Users\Desktop\code>node app.js
hello world
C:\Users\Desktop\code>
Code language: JavaScript (javascript)
We can see the hello world message printed in the terminal. This is how we run JavaScript through the terminal with Nodejs.
Codedamn playground
Node js is a great platform but we face a lot of errors running the source code, downloading and resolving these errors consumes a lot of time. At codedamn, we provide a free online node js playground. Here is the link to it nodejs-playground.
To use the playground first open the link above mentioned. If you are new to Codedamn signup to create your Codedamn account then click on node js to create your first project then start writing your code. It is the same as node js to run the code enter node <projectname> in the terminal below. You can share the source code with your friends and colleagues. It is easy to use no need to go through the hectic download process. You check out other free online IDE also they are very much useful there are HTML/CSS, C/C++, Python, Solidity, etc. Using Codedamn playground you can create web applications without leaving the browser. Check out them once
Bun
A new JavaScript runtime BUN
It is a very fast all-in-one JavaScript runtime 3 times faster when server rendering. It is also prominent at executing native code with foreign functions, running database queries, and far better when it comes to Websocket server chat. Bun performance is very fast first of all it doesn’t use the v8chrome engine but instead it uses JavaScript core from WebKit which is written in a low-level programming language called ZIG which is very similar to C or RUST due to the lack of hidden control flow makes to simpler to write fast software. Importantly it has a Native Module Bundler instead of all webpack it also has a native transpiler even we can write typescript code out of the box, it also transpile your JSX files, also supports many node modules as well as its API which will allow many npm packages to also work in a bun.
Interestingly npm packages install 20 times faster and also have their test runner which is very fast. Also, the node module size is much smaller than the react module packages. At present, it’s in the 0.2.1(beta ) stage. But it has so many bugs, and fetch is not reliable, which has been introduced: newly and it takes time to be compatible with Node, and errors seem to occur often. So definitely it can’t replace Deno and Node it takes time but it’s very fast. Deno is also a runtime for JavaScript. If you want to learn more about Deno you can visit your article on the complete guide to Deno.js
For Windows users Bun is still not yet released you can use it through WSL(Windows Sub-system Linux). To download WSL on windows follow the steps mentioned here. Now you can download BUN on macOS, Linux, and WSL by entering the following command in your shell
$ curl https://bun.sh/install | bash
Code language: Bash (bash)
Once you installed it you can run your files using the run command
$ bun run app.ts
Code language: TypeScript (typescript)
Conclusion
So these are the ways to write JavaScript code in a terminal. Well, the new Bun challenges Deno, Node.js with its beta release also which is a powerful tool that dominates the v8 JavaScript engine which is used for Chrome, Deno, and Node js. We can run our JavaScript easily in a codedamn background platform which is very easier. Technology is leading faster day by day maybe we can find the fastest JavaScript runtime with enhanced full pledged performances. I hope you learned about executing JavaScript code in the terminal. That’s it from this article. Thank you.
Sharing is caring
Did you like what Thrishank wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: