Photo by Clément Hélardot on Unsplash
How To Run JavaScript Code in Visual Studio Code (Step by Step)
If you are like me, you would probably spend most of your time writing your code using Visual Studio Code. In that case, I will show you the exact steps and methods I use to run my JavaScript Code in the Visual Studio Code.
How It Started
Most of the projects I've been working on so far are web projects which requires an HTML file, a CSS file and then the Javascript file. Thus, whenever I need to run them, I simply run them on my Internet browser.
However, recently I was trying to learn some concepts about JavaScript and I only wish to have the JavaScript code without an HTML and/or CSS file. This then leads to me having to make research on running only JavaScript code in my Visual Studio code.
While I was able to come across different ways of running your JavaScript Code in Visual Studio Code Editor, I found a method that seems to me, the most efficient and easiest way.
Why Should You Run Your JavaScript Code in Visual Studio Code
While there are different reasons you might want to run your JS file in VS Code terminal just like mine, here are some reasons why you might want to do so;
Running a JS Code in Visual Studio Code Terminal Saves Time
Helps you Debug JavaScript Code without creating any other files
Saves the Hassle of having to Check your Browser for results
How To Run JavaScript Code in Visual Studio Code Terminal
Without further ado, below are the steps I took to be able to run my JS file in VS Code terminal;
To be able to run JavaScript code in your terminal, you need to first download NodeJS on your system.
Visit NodeJS Official Website to download the recommended version and run the Installer
After completing the setup, open your Visual Studio Code Editor (where your JavaScript file is located)
Open a new
Terminal
and typenpm init -y
in your Terminal as seen belownpm
stands forNode Package Manager
init
is the keyword for NodeJs to create a new project.
If successful, a
package.json
file will be created in your project folder.Finally, to run your JavaScript code from the terminal, simply
type node jsfilename
For instance; If I want to run my JavaScript code, I would type in
node example
as seen below
Conclusion
And there you go!
If you encounter any issues while trying to achieve this, feel free to comment and I will try my possible best to provide answers.
If you also have any alternative means of running your JavaScript code in Visual Studio Code Editor, feel free to share.