--- title: "Create a Node.js and React app" description: Create a Node.js web application project from a Visual Studio template, use IntelliSense and other built-in features for Node.js, and create a simple app with React. ms.custom: vs-acquisition ms.date: 11/06/2023 ms.topic: tutorial ms.devlang: javascript author: mikejo5000 ms.author: mikejo manager: jmartens ms.subservice: javascript-typescript dev_langs: - JavaScript --- # Tutorial: Create a Node.js and React app in Visual Studio [!INCLUDE [Visual Studio](~/includes/applies-to-version/vs-windows-only.md)] With Visual Studio, you can easily create a Node.js project and use IntelliSense and other built-in features that support Node.js. In this tutorial, you create a Node.js web app project from a Visual Studio template. Then, you create a simple app using React. In this tutorial, you learn how to: > [!div class="checklist"] > * Create a Node.js project > * Add npm packages > * Add React code to your app > * Transpile JSX > * Attach the debugger ::: moniker range=">=vs-2022" > [!IMPORTANT] > Starting in Visual Studio 2022, you can alternatively [create a React project](../javascript/tutorial-create-react-app.md) using the recommended [CLI-based project type](https://devblogs.microsoft.com/visualstudio/the-new-javascript-typescript-experience-in-vs-2022-preview-3/). Some of the information in this article applies only to the Node.js project type (.njsproj). The template used in this article is no longer available starting in Visual Studio 2022 version 17.8 Preview 2. ::: moniker-end Before you begin, here's a quick FAQ to introduce you to some key concepts: - **What is Node.js?** Node.js is a server-side JavaScript runtime environment that executes JavaScript code. - **What is npm?** The default package manager for Node.js is npm. A package manager makes it easier to publish and share Node.js source code libraries. The npm package manager simplifies library installation, updating, and uninstallation. - **What is React?** React is a front-end framework for creating a user interface (UI). - **What is JSX?** JSX is a JavaScript syntax extension typically used with React to describe UI elements. You must transpile JSX code to plain JavaScript before it can run in a browser. - **What is Webpack?** Webpack bundles JavaScript files so they can run in a browser, and can also transform or package other resources and assets. Webpack can specify a compiler, such as Babel or TypeScript, to transpile JSX or TypeScript code to plain JavaScript. ## Prerequisites This tutorial requires the following prerequisites: - Visual Studio with the Node.js development workload installed. If you haven't yet installed Visual Studio: 1. Go to the [Visual Studio downloads](https://visualstudio.microsoft.com/downloads/?cid=learn-onpage-download-cta) page to install Visual Studio for free. 1. In the Visual Studio Installer, select the **Node.js development** workload, and select **Install**.  If you have Visual Studio installed but need the Node.js workload: 1. In Visual Studio, go to **Tools** > **Get Tools and Features**. 1. In the Visual Studio Installer, choose the **Node.js development** workload, and select **Modify** to download and install the workload. - The Node.js runtime installed: If you don't have the Node.js runtime installed, [install the LTS version from the Node.js website](https://nodejs.org/en/download/). The LTS version has the best compatibility with other frameworks and libraries. The Node.js tools in the Visual Studio Node.js workload support both Node.js 32-bit and 64-bit architecture versions. Visual Studio requires only one version, and the Node.js installer only supports one version at a time. Visual Studio usually detects the installed Node.js runtime automatically. If not, you can configure your project to reference the installed runtime: 1. After you create a project, right-click the project node and select **Properties**. 1. In the **Properties** pane, set the **Node.exe path** to reference a global or local installation of Node.js. You can specify the path to a local interpreter in each of your Node.js projects. ::: moniker range=">=vs-2022" This tutorial was tested with Node.js 18.5.0. ::: moniker-end ::: moniker range="<=vs-2019" This tutorial was tested with Node.js 12.6.2. ::: moniker-end ## Create a project First, create a Node.js web app project. ::: moniker range=">=vs-2022" 1. Open Visual Studio, and press **Esc** to close the start window. 1. Press **Ctrl**+**Q**, type *node.js* in the search box, and then choose **Blank Node.js Web Application - JavaScript** from the dropdown list. Although this tutorial uses the TypeScript compiler, the steps require that you start with the **JavaScript** template. If you don't see the **Blank Node.js Web Application** choice, you need to install the Node.js development workload. For instructions, see the [Prerequisites](#prerequisites). 1. In the **Configure your new project** dialog box, select **Create**. Visual Studio creates the new solution and project, and opens the project in the right pane. The *server.js* project file opens in the editor in the left pane. 1. Look at the project structure in **Solution Explorer** in the right pane.  - At the top level is the *solution* (**1**), which by default has the same name as your project. A solution, represented by a *.sln* file on disk, is a container for one or more related projects. - Your project (**2**), using the name you gave in the **Configure your new project** dialog box, is highlighted in bold. In the file system, the project is a *.njsproj* file in your project folder. To see and set project properties and environment variables, press **Alt**+**Enter**, or right-click the project and select **Properties** from the context menu. You can work with other development tools, because the project file doesn't make custom changes to the Node.js project source. - The **npm** node (**3**) shows any installed npm packages. Right-click the **npm** node to search for and install npm packages. You can install and update packages by using the settings in *package.json* and the right-click options in the **npm** node. - Npm uses the *package.json* file (**4**) to manage dependencies and versions for locally installed packages. For more information, see [Manage npm packages](npm-package-management.md). - Project files (**5**) appear under the project node. The project startup file, *server.js*, shows in bold. You can set the startup file by right-clicking a file in the project and selecting **Set as Node.js startup file**. ::: moniker-end ::: moniker range="vs-2019" 1. Open Visual Studio. 1. Create a new project. Press **Esc** to close the start window. Type **Ctrl + Q** to open the search box, type **Node.js**, then choose **Blank Node.js Web Application - JavaScript**. (Although this tutorial uses the TypeScript compiler, the steps require that you start with the **JavaScript** template.) In the dialog box that appears, choose **Create**. If you don't see the **Blank Node.js Web Application** project template, you must add the **Node.js development** workload. For detailed instructions, see the [Prerequisites](#prerequisites). Visual Studio creates the new solution and opens your project.  (1) Highlighted in **bold** is your project, using the name you gave in the **New Project** dialog box. In the file system, this project is represented by a *.njsproj* file in your project folder. You can set properties and environment variables associated with the project by right-clicking the project and choosing **Properties** (or press **Alt** + **Enter**). You can do round-tripping with other development tools, because the project file does not make custom changes to the Node.js project source. (2) At the top level is a solution, which by default has the same name as your project. A solution, represented by a *.sln* file on disk, is a container for one or more related projects. (3) The npm node shows any installed npm packages. You can right-click the npm node to search for and install npm packages using a dialog box or install and update packages using the settings in *package.json* and right-click options in the npm node. (4) *package.json* is a file used by npm to manage package dependencies and package versions for locally installed packages. For more information, see [Manage npm packages](../javascript/npm-package-management.md). (5) Project files such as *server.js* show up under the project node. *server.js* is the project startup file and that is why it shows up in **bold**. You can set the startup file by right-clicking a file in the project and selecting **Set as Node.js startup file**. ::: moniker-end ## Add npm packages This app requires the following npm modules to run correctly: - react - react-dom - express - path - ts-loader - typescript - webpack - webpack-cli To install a package: 1. In **Solution Explorer**, right-click the **npm** node and select **Install New npm Packages**. 1. In the **Install New npm Packages** dialog box, search for the **react** package, and select **Install Package** to install it. ::: moniker range=">=vs-2022"  ::: moniker-end ::: moniker range="<=vs-2019"  ::: moniker-end In the **Install New npm Packages** dialog box, you can choose to install the most current package version or to specify a version. If you choose to install the current versions, but run into unexpected errors later, try installing the exact package versions listed in the next step. The **Output** window in the Visual Studio lower pane shows package installation progress. Open the **Output** window by selecting **View** > **Output** or pressing **Ctrl**+**Alt**+**O**. In the **Show output from** field of the **Output** window, select **Npm**. When installed, the **react** package appears under the **npm** node in **Solution Explorer**. The project's *package.json* file updates with the new package information, including the package version. Instead of using the UI to search for and add the rest of the packages one at a time, you can paste the required package code into *package.json*. 1. From **Solution Explorer**, open **package.json** in the Visual Studio editor. Add the following `dependencies` section before the end of the file: ```json "dependencies": { "express": "^4.18.2", "path": "^0.12.7", "react": "^18.2.0", "react-dom": "^18.2.0", "ts-loader": "^9.4.2", "typescript": "^5.0.2", "webpack": "^5.76.3", "webpack-cli": "^5.0.1" }, ``` If the file already has a `dependencies` section, replace it with the preceding JSON code. For more information on using the *package.json* file, see [package.json configuration](configure-packages-with-package-json.md). 1. Press **Ctrl**+**S** or select **File** > **Save package.json** to save the changes. 1. In **Solution Explorer**, right-click the **npm** node in your project and select **Install npm Packages**. This command runs the npm install command directly to install all the packages listed in *packages.json*. Select the **Output** window in the lower pane to see installation progress. Installation might take a few minutes, and you might not see results immediately. Make sure that you select **Npm** in the **Show output from** field in the **Output** window. After installation, the npm modules appear in the **npm** node in **Solution Explorer**. ::: moniker range=">=vs-2022"  ::: moniker-end ::: moniker range="<=vs-2019"  ::: moniker-end > [!NOTE] > You can also install npm packages by using the command line. In **Solution Explorer**, right-click the project name and select **Open Command Prompt Here**. Use standard Node.js commands to install packages. ## Add project files Next, add four new files to your project. - *app.tsx* - *webpack-config.js* - *index.html* - *tsconfig.json* For this simple app, you add the new project files in the project root. For most apps, you add the files to subfolders and adjust relative path references accordingly. 1. In **Solution Explorer**, select the project name and press **Ctrl**+**Shift**+**A**, or right-click the project name and select **Add** > **New Item**. If you don't see all the item templates, choose **Show All Templates**, and then choose the item template. 1. In the **Add New Item** dialog box, choose **TypeScript JSX File**, type the name *app.tsx*, and select **Add** or **OK**. 1. Repeat these steps to add a **JavaScript file** named *webpack-config.js*. 1. Repeat these steps to add an **HTML file** named *index.html*. 1. Repeat these steps to add a **TypeScript JSON Configuration File** named *tsconfig.json*. ## Add app code 1. In **Solution Explorer**, open **server.js** and replace the existing code with the following code: ```javascript 'use strict'; var path = require('path'); var express = require('express'); var app = express(); var staticPath = path.join(__dirname, '/'); app.use(express.static(staticPath)); // Allows you to set port in the project properties. app.set('port', process.env.PORT || 3000); var server = app.listen(app.get('port'), function() { console.log('listening'); }); ``` The preceding code uses Express to start Node.js as your web application server. The code sets the port to the port number configured in the project properties, which by default is 1337. If you need to open the project properties, right-click the project name in **Solution Explorer** and select **Properties**. 1. Open **app.tsx** and add the following code: ```javascript declare var require: any var React = require('react'); var ReactDOM = require('react-dom'); export class Hello extends React.Component { render() { return (