The first step is to check if you have Node.js installed and what version it is. Enter the following command: node -v If Node.js is installed, it will return the currently installed version, such as v15.3.0. If node is not installed, download the long-term support (LTS) version of Node.js from Node.js Download. Node.js is a JavaScript runtime primarily used for creating web applications. Put another way, it's a server-side implementation of JavaScript used for writing the backend of an application. (Though many Node.js frameworks can also handle the frontend.) Here are a few examples of what you might create with Node.js.
If you're brand new to using Node.js, this guide will help you to get started with some basics.
This guide assumes that you've already completed the steps to set up your Node.js development environment on native Windows, including:
Installing Node.js directly on Windows is the most straightforward way to get started performing basic Node.js operations with a minimal amount of set up.
Once you are ready to use Node.js to develop applications for production, which typically involves deploying to a Linux server, we recommend that you set up your Node.js development environment with WSL2. Though it's possible to deploy web apps on Windows servers, it is much more common to use Linux servers to host your Node.js apps.
Node.js is a JavaScript runtime primarily used for creating web applications. Put another way, it's a server-side implementation of JavaScript used for writing the backend of an application. (Though many Node.js frameworks can also handle the frontend.) Here are a few examples of what you might create with Node.js.
Open your command line (Command prompt, PowerShell, or whatever you prefer) and create a new directory: mkdir HelloNode
, then enter the directory: cd HelloNode
Create a JavaScript file named 'app.js' with a variable named 'msg' inside: echo var msg > app.js
Open the directory and your app.js file in VS Code: : code .
Add a simple string variable ('Hello World'), then send the contents of the string to your console by entering this in your 'app.js' file:
To run your 'app.js' file with Node.js. Open your terminal right inside VS Code by selecting View > Terminal (or select Ctrl+`, using the backtick character). If you need to change the default terminal, select the dropdown menu and choose Select Default Shell.
In the terminal, enter: node app.js
. You should see the output: 'Hello World'.
Note
Notice that when you type console
in your 'app.js' file, VS Code displays supported options related to the console
object for you to choose from using IntelliSense. Try experimenting with Intellisense using other JavaScript objects.
Tip
Try the new Windows terminal if you plan to use multiple command lines (Ubuntu, PowerShell, Windows Command Prompt, etc) or if you want to customize your terminal, including text, background colors, key bindings, multiple window panes, etc.
Express is a minimal, flexible, and streamlined Node.js framework that makes it easier to develop a web app that can handle multiple types of requests, like GET, PUT, POST, and DELETE. Express comes with an application generator that will automatically create a file architecture for your app.
To create a project with Express.js:
mkdir ExpressProjects
and enter that directory: cd ExpressProjects
npx express-generator HelloWorld --view=pug
Note
We are using the npx
command here to execute the Express.js Node package without actually installing it (or by temporarily installing it depending on how you want to think of it). If you try to use the express
command or check the version of Express installed using: express --version
, you will receive a response that Express cannot be found. If you want to globally install Express to use over and over again, use: npm install -g express-generator
. You can view a list of the packages that have been installed by npm using npm list
. They'll be listed by depth (the number of nested directories deep). Packages that you installed will be at depth 0. That package's dependencies will be at depth 1, further dependencies at depth 2, and so on. To learn more, see Difference between npx and npm? on Stackoverflow.
Examine the files and folders that Express included by opening the project in VS Code, with: code .
The files that Express generates will create a web app that uses an architecture that can appear a little overwhelming at first. You'll see in your VS Code Explorer window (Ctrl+Shift+E to view) that the following files and folders have been generated:
bin
. Contains the executable file that starts your app. It fires up a server (on port 3000 if no alternative is supplied) and sets up basic error handling.public
. Contains all the publicly accessed files, including JavaScript files, CSS stylesheets, font files, images, and any other assets that people need when they connect to your website.routes
. Contains all the route handlers for the application. Two files, index.js
and users.js
, are automatically generated in this folder to serve as examples of how to separate out your application’s route configuration.views
. Contains the files used by your template engine. Express is configured to look here for a matching view when the render method is called. The default template engine is Jade, but Jade has been deprecated in favor of Pug, so we used the --view
flag to change the view (template) engine. You can see the --view
flag options, and others, by using express --help
.app.js
. The starting point of your app. It loads everything and begins serving user requests. It's basically the glue that holds all the parts together.package.json
. Contains the project description, scripts manager, and app manifest. Its main purpose is to track your app's dependencies and their respective versions.You now need to install the dependencies that Express uses in order to build and run your HelloWorld Express app (the packages used for tasks like running the server, as defined in the package.json
file). Inside VS Code, open your terminal by selecting View > Terminal (or select Ctrl+`, using the backtick character), be sure that you're still in the 'HelloWorld' project directory. Install the Express package dependencies with:
Tip
The DEBUG=myapp:*
part of the command above means you are telling Node.js that you want to turn on logging for debugging purposes. Remember to replace 'myapp' with your app name. You can find your app name in the package.json
file under the 'name' property. Using npx cross-env
sets the DEBUG
environment variable in any terminal, but you can also set it with your terminal specific way. The npm start
command is telling npm to run the scripts in your package.json
file.
You can now view the running app by opening a web browser and going to: localhost:3000
Now that your HelloWorld Express app is running locally in your browser, try making a change by opening the 'views' folder in your project directory and selecting the 'index.pug' file. Once open, change h1= title
to h1= 'Hello World!'
and selecting Save (Ctrl+S). View your change by refreshing the localhost:3000 URL on your web browser.
To stop running your Express app, in your terminal, enter: Ctrl+C
Node.js has tools to help you develop server-side web apps, some built in and many more available via npm. These modules can help with many tasks:
Tool | Used for |
---|---|
gm, sharp | Image manipulation, including editing, resizing, compression, and so on, directly in your JavaScript code |
PDFKit | PDF generation |
validator.js | String validation |
imagemin, UglifyJS2 | Minification |
spritesmith | Sprite sheet generation |
winston | Logging |
commander.js | Creating command-line applications |
Let's use the built-in OS module to get some information about your computer's operating system:
In your command line, open the Node.js CLI. You'll see the >
prompt letting you know you're using Node.js after entering: node
To identify the operating system you are currently using (which should return a response letting you know that you're on Windows), enter: os.platform()
To check your CPU's architecture, enter: os.arch()
To view the the CPUs available on your system, enter: os.cpus()
Leave the Node.js CLI by entering .exit
or by selecting Ctrl+C twice.
Tip
You can use the Node.js OS module to do things like check the platform and return a platform-specific variable: Win32/.bat for Windows development, darwin/.sh for Mac/unix, Linux, SunOS, and so on (for example, var isWin = process.platform 'win32';
).
In this guide, you learned a few basic things about what you can do with Node.js, tried using the Node.js command line in VS Code, created a simple web app with Express.js and ran it locally in your web browser, and then tried using a few of the built-in Node.js modules. To learn more about how to install and use some popular Node.js web frameworks, continue to the next guide which covers Next.js (a server-rendered web framework based on React), Nuxt.js (a server-rendered web framework based on Vue), and Gatsby (a statically-rendered web framework based on React). You can also skip to learning about how to work with MongoDB or PostgreSQL databases or Docker containers.