React Js Visual Studio Code



-->

Visual Studio allows you to easily create a Node.js project and experience IntelliSense and other built-in features that support Node.js. In this tutorial for Visual Studio, you create a Node.js web application project from a Visual Studio template. Then, you create a simple app using React.

Using React in Visual Studio Code. React is a popular JavaScript library developed by Facebook for building web application user interfaces. The Visual Studio Code editor supports React.js IntelliSense and code navigation out of the box. Welcome to React. React Clean Code Structure. Create a boilerplate folder structure based on Presentational/Container code pattern for React/React Native. This extension is accessible in the explorer context menu as shown bellow: Requirements. There's no known issues. Npx create-create-app demo -template=typescript: for the command to work, you need to have installed create-react-app globally with: npm install -g create-react-app. Open the folder inside Visual Studio Code, after you’ve installed the respective Debugger Extension for either Edge/Chrome/Firefox. I’m using Chrome for the example. How to run react js. Code on visual studio code. Ask Question Asked 1 year ago. Active 1 year ago. Viewed 46 times -2. Was formally using sublime text to run react js. Codes, recently i tried changing my IDE to visual studio code but have being getting errors. Need to know the procedures for running react codes on visual studio code.

In this tutorial, you learn how to:

  • Create a Node.js project
  • Add npm packages
  • Add React code to your app
  • Transpile JSX
  • Attach the debugger

Before you begin

React Js Visual Studio Code

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 server-side.

What is npm?

npm is the default package manager for the Node.js. The package manager makes it easier for programmers to publish and share source code of Node.js libraries and is designed to simplify installation, updating, and uninstallation of libraries.

What is React?

React is a front-end framework to create a UI.

What is JSX?

JSX is a JavaScript syntax extension, typically used with React to describe UI elements. JSX code must be transpiled to plain JavaScript before it can run in a browser.

What is webpack?

webpack bundles JavaScript files so they can run in a browser. It can also transform or package other resources and assets. It is often used to specify a compiler, such as Babel or TypeScript, to transpile JSX or TypeScript code to plain JavaScript.

Visual

Prerequisites

  • You must have Visual Studio installed and the Node.js development workload.

    If you haven't already installed Visual Studio 2019, go to the Visual Studio downloads page to install it for free.

    If you haven't already installed Visual Studio 2017, go to the Visual Studio downloads page to install it for free.

    If you need to install the workload but already have Visual Studio, go to Tools > Get Tools and Features..., which opens the Visual Studio Installer. Choose the Node.js development workload, then choose Modify.

  • You must have the Node.js runtime installed.

    This tutorial was tested with version 12.6.2.

    If you don't have it installed, we recommend you install the LTS version from the Node.js website for best compatibility with outside frameworks and libraries. Node.js is built for 32-bit and 64-bit architectures. The Node.js tools in Visual Studio, included in the Node.js workload, support both versions. Only one is required and the Node.js installer only supports one being installed at a time.

    In general, Visual Studio automatically detects the installed Node.js runtime. If it does not detect an installed runtime, you can configure your project to reference the installed runtime in the properties page (after you create a project, right-click the project node, choose Properties, and set the Node.exe path). You can use a global installation of Node.js or you can specify the path to a local interpreter in each of your Node.js projects.

Create a project

First, create a Node.js web application project.

  1. Open Visual Studio.

  2. 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.

    From the top menu bar, choose File > New > Project. In the left pane of the New Project dialog box, expand JavaScript, then choose Node.js. In the middle pane, choose Blank Node.js Web Application, type the name NodejsWebAppBlank, then choose OK.

    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.

    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. 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.

    (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.

Add npm packages

This app requires a number of npm modules to run correctly.

  • react
  • react-dom
  • express
  • path
  • ts-loader
  • typescript
  • webpack
  • webpack-cli
  1. In Solution Explorer (right pane), right-click the npm node in the project and choose Install New npm Packages.

    In the Install New npm Packages dialog box, you can choose to install the most current package version or specify a version. If you choose to install the current version of these packages, but run into unexpected errors later, you may want to install the exact package versions described later in these steps.

  2. In the Install New npm Packages dialog box, search for the react package, and select Install Package to install it.

    Select the Output window to see progress on installing the package (select Npm in the Show output from field). When installed, the package appears under the npm node.

    The project's package.json file is updated with the new package information including the package version.

  3. Instead of using the UI to search for and add the rest of the packages one at a time, paste the following code into package.json. To do this, add a dependencies section with this code:

    If there is already a dependencies section in your version of the blank template, just replace it with the preceding JSON code. For more information on use of this file, see package.json configuration.

  4. Save the changes.

  5. Right-click npm node in your project and choose Install npm Packages.

    This command runs the npm install command directly.

    In the lower pane, select the Output window to see progress on installing the packages. Installation may take a few minutes and you may not see results immediately. To see the output, make sure that you select Npm in the Show output from field in the Output window.

    Here are the npm modules as they appear in Solution Explorer after they are installed.

    Note

    If you prefer to install npm packages using the command line, right-click the project node and choose Open Command Prompt Here. Use standard Node.js commands to install packages.

Add project files

In these steps, you add four new files to your project.

  • app.tsx
  • webpack-config.js
  • index.html
  • tsconfig.json
Visual studio react tutorial

For this simple app, you add the new project files in the project root. (In most apps, you typically add the files to subfolders and adjust relative path references accordingly.)

React Js Visual Studio Code
  1. In Solution Explorer, right-click the project NodejsWebAppBlank and choose Add > New Item.

  2. In the Add New Item dialog box, choose TypeScript JSX file, type the name app.tsx, and select Add or OK.

  3. Repeat these steps to add webpack-config.js. Instead of a TypeScript JSX file, choose JavaScript file.

  4. Repeat the same steps to add index.html to the project. Instead of a JavaScript file, choose HTML file.

  5. Repeat the same steps to add tsconfig.json to the project. Instead of a JavaScript file, choose TypeScript JSON Configuration file.

Add app code

  1. Open server.js and replace the existing code with the following code:

    The preceding code uses Express to start Node.js as your web application server. This code sets the port to the port number configured in the project properties (by default, the port is configured to 1337 in the properties). To open the project properties, right-click the project in Solution Explorer and choose Properties.

  2. Open app.tsx and add the following code:

    The preceding code uses JSX syntax and React to display a simple message.

  3. Open index.html and replace the body section with the following code:

    This HTML page loads app-bundle.js, which contains the JSX and React code transpiled to plain JavaScript. Currently, app-bundle.js is an empty file. In the next section, you configure options to transpile the code.

Configure webpack and TypeScript compiler options

In the previous steps, you added webpack-config.js to the project. Next, you add webpack configuration code. You will add a simple webpack configuration that specifies an input file (app.tsx) and an output file (app-bundle.js) for bundling and transpiling JSX to plain JavaScript. For transpiling, you also configure some TypeScript compiler options. This code is a basic configuration that is intended as an introduction to webpack and the TypeScript compiler.

  1. In Solution Explorer, open webpack-config.js and add the following code.

    The webpack configuration code instructs webpack to use the TypeScript loader to transpile the JSX.

  2. Open tsconfig.json and replace the default code with the following code, which specifies the TypeScript compiler options:

    app.tsx is specified as the source file.

Transpile the JSX

  1. In Solution Explorer, right-click the project node and choose Open Command Prompt Here.

  2. In the command prompt, type the following command:

    node_modules.binwebpack app.tsx --config webpack-config.js

    The command prompt window shows the result.

    If you see any errors instead of the preceding output, you must resolve them before your app will work. If your npm package versions are different than the versions shown in this tutorial, that can be a source of errors. One way to fix errors is to use the exact versions shown in the earlier steps. Also, if one or more of these package versions has been deprecated and results in an error, you may need to install a more recent version to fix errors. For information on using package.json to control npm package versions, see package.json configuration.

  3. In Solution Explorer, right-click the project node and choose Add > Existing Folder, then choose the dist folder and choose Select Folder.

    Visual Studio adds the dist folder to the project, which contains app-bundle.js and app-bundle.js.map.

  4. Open app-bundle.js to see the transpiled JavaScript code.

  5. If prompted to reload externally modified files, select Yes to All.

Each time you make changes to app.tsx, you must rerun the webpack command. To automate this step, add a build script to transpile the JSX.

Add a build script to transpile the JSX

Starting in Visual Studio 2019, a build script is required. Instead of transpiling JSX at the command line (as shown in the preceding section), you can transpile JSX when building from Visual Studio.

  • Open package.json and add the following section after the dependencies section:

Run the app

  1. Select either Web Server (Google Chrome) or Web Server (Microsoft Edge) as the current debug target.

    If Chrome is available on your machine, but does not show up as an option, choose Browse With from the debug target dropdown list, and select Chrome as the default browser target (choose Set as Default).

  2. To run the app, press F5 (Debug > Start Debugging) or the green arrow button.

    A Node.js console window opens that shows the port on which the debugger is listening.

    Visual Studio starts the app by launching the startup file, server.js.

  3. Close the browser window.

  4. Close the console window.

Set a breakpoint and run the app

  1. In server.js, click in the gutter to the left of the staticPath declaration to set a breakpoint:

    Breakpoints are the most basic and essential feature of reliable debugging. A breakpoint indicates where Visual Studio should suspend your running code so you can take a look at the values of variables, or the behavior of memory, or whether or not a branch of code is getting run.

  2. To run the app, press F5 (Debug > Start Debugging).

    The debugger pauses at the breakpoint you set (the current statement is marked in yellow). Now, you can inspect your app state by hovering over variables that are currently in scope, using debugger windows like the Locals and Watch windows.

  3. Press F5 to continue the app.

  4. If you want to use the Chrome Developer Tools or F12 Tools for Microsoft Edge, press F12. You can use these tools to examine the DOM and interact with the app using the JavaScript Console.

  5. Close the web browser and the console.

Set and hit a breakpoint in the client-side React code

In the preceding section, you attached the debugger to server-side Node.js code. To attach the debugger from Visual Studio and hit breakpoints in client-side React code, the debugger needs help to identify the correct process. Here is one way to enable this.

Prepare the browser for debugging

For this scenario, use either Microsoft Edge (Chromium), currently named Microsoft Edge Beta in the IDE, or Chrome.

  1. Close all windows for the target browser.

    Other browser instances can prevent the browser from opening with debugging enabled. (Browser extensions may be running and preventing full debug mode, so you may need to open Task Manager to find unexpected instances of Chrome.)

    For Microsoft Edge (Chromium), also shut down all instances of Chrome. Because both browsers share the chromium code base, this gives the best results.

  2. Start your browser with debugging enabled.

    Starting in Visual Studio 2019, you can set the --remote-debugging-port=9222 flag at browser launch by selecting Browse With... > from the Debug toolbar, then choosing Add, and then setting the flag in the Arguments field. Use a different friendly name for the browser such as Edge with Debugging or Chrome with Debugging. For details, see the Release Notes.

    Alternatively, open the Run command from the Windows Start button (right-click and choose Run), and enter the following command:

    msedge --remote-debugging-port=9222

    or,

    chrome.exe --remote-debugging-port=9222

    Open the Run command from the Windows Start button (right-click and choose Run), and enter the following command:

    chrome.exe --remote-debugging-port=9222

    This starts your browser with debugging enabled.

    The app is not yet running, so you get an empty browser page.

Attach the debugger to client-side script

  1. Switch to Visual Studio and then set a breakpoint in your source code, either app-bundle.js or app.tsx.

    For app-bundle.js, set the breakpoint in the render() function as shown in the following illustration:

    To find the render() function in the transpiled app-bundle.js file, use Ctrl+F (Edit > Find and Replace > Quick Find).

    For app.tsx, set the breakpoint inside the render() function, on the return statement.

  2. If you are setting the breakpoint in the .tsx file (rather than app-bundle.js), you need to update webpack-config.js. Replace the following code:

    with this code:

    This is a development-only setting to enable debugging in Visual Studio. This setting allows you to override the generated references in the source map file, app-bundle.js.map, when building the app. By default, webpack references in the source map file include the webpack:/// prefix, which prevents Visual Studio from finding the source file, app.tsx. Specifically, when you make this change, the reference to the source file, app.tsx, gets changed from webpack:///./app.tsx to ./app.tsx, which enables debugging.

  3. Select your target browser as the debug target in Visual Studio, then press Ctrl+F5 (Debug > Start Without Debugging) to run the app in the browser.

    If you created a browser configuration with a friendly name, choose that as your debug target.

    The app opens in a new browser tab.

  4. Choose Debug > Attach to Process.

    Tip

    Starting in Visual Studio 2017, once you attach to the process the first time by following these steps, you can quickly reattach to the same process by choosing Debug > Reattach to Process.

  5. In the Attach to Process dialog box, get a filtered list of browser instances that you can attach to.

    In Visual Studio 2019, choose the correct debugger for your target browser, JavaScript (Chrome) or JavaScript (Microsoft Edge - Chromium) in the Attach to field, type chrome or edge in the filter box to filter the search results.

    In Visual Studio 2017, choose Webkit code in the Attach to field, type chrome in the filter box to filter the search results.

  6. Select the browser process with the correct host port (localhost in this example), and select Attach.

    The port (1337) may also appear in the Title field to help you select the correct browser instance.

    The following example shows how this looks for the Microsoft Edge (Chromium) browser.

    You know the debugger has attached correctly when the DOM Explorer and the JavaScript Console open in Visual Studio. These debugging tools are similar to Chrome Developer Tools and F12 Tools for Microsoft Edge.

    Tip

    If the debugger does not attach and you see the message 'Unable to attach to the process. An operation is not legal in the current state.', use the Task Manager to close all instances of the target browser before starting the browser in debugging mode. Browser extensions may be running and preventing full debug mode.

  7. Because the code with the breakpoint already executed, refresh your browser page to hit the breakpoint.

    While paused in the debugger, you can examine your app state by hovering over variables and using debugger windows. You can advance the debugger by stepping through code (F5, F10, and F11). For more information on basic debugging features, see First look at the debugger.

    You may hit the breakpoint in either app-bundle.js or its mapped location in app.tsx, depending on which steps you followed previously, along with your environment and browser state. Either way, you can step through code and examine variables.

    • If you need to break into code in app.tsx and are unable to do it, use Attach to Process as described in the previous steps to attach the debugger. Make sure you that your environment is set up correctly:

      • You closed all browser instances, including Chrome extensions (using the Task Manager), so that you can run the browser in debug mode. Make sure you start the browser in debug mode.

      • Make sure that your source map file includes a reference to ./app.tsx and not webpack:///./app.tsx, which prevents the Visual Studio debugger from locating app.tsx.Alternatively, if you need to break into code in app.tsx and are unable to do it, try using the debugger; statement in app.tsx, or set breakpoints in the Chrome Developer Tools (or F12 Tools for Microsoft Edge) instead.

    • If you need to break into code in app-bundle.js and are unable to do it, remove the source map file, app-bundle.js.map.

Next steps

Visual Studio Code includes built-in JavaScript IntelliSense, debugging, formatting, code navigation, refactorings, and many other advanced language features.

Most of these features just work out of the box, while some may require basic configuration to get the best experience. This page summarizes the JavaScript features that VS Code ships with. Extensions from the VS Code Marketplace can augment or change most of these built-in features. For a more in-depth guide on how these features work and can be configured, see Working with JavaScript.

IntelliSense

IntelliSense shows you intelligent code completion, hover info, and signature information so that you can write code more quickly and correctly.

VS Code provides IntelliSense within your JavaScript projects; for many npm libraries such as React, lodash, and express; and for other platforms such as node, serverless, or IoT.

See Working with JavaScript for information about VS Code's JavaScript IntelliSense, how to configure it, and help troubleshooting common IntelliSense problems.

JavaScript projects (jsconfig.json)

A jsconfig.json file defines a JavaScript project in VS Code. While jsconfig.json files are not required, you will want to create one in cases such as:

  • If not all JavaScript files in your workspace should be considered part of a single JavaScript project. jsconfig.json files let you exclude some files from showing up in IntelliSense.
  • To ensure that a subset of JavaScript files in your workspace is treated as a single project. This is useful if you are working with legacy code that uses implicit globals dependencies instead of imports for dependencies.
  • If your workspace contains more than one project context, such as front-end and back-end JavaScript code. For multi-project workspaces, create a jsconfig.json at the root folder of each project.
  • You are using the TypeScript compiler to down-level compile JavaScript source code.

To define a basic JavaScript project, add a jsconfig.json at the root of your workspace:

See Working with JavaScript for more advanced jsconfig.json configuration.

Tip: To check if a JavaScript file is part of JavaScript project, just open the file in VS Code and run the JavaScript: Go to Project Configuration command. This command opens the jsconfig.json that references the JavaScript file. A notification is shown if the file is not part of any jsconfig.json project.

Snippets

VS Code includes basic JavaScript snippets that are suggested as you type;

There are many extensions that provide additional snippets, including snippets for popular frameworks such as Redux or Angular. You can even define your own snippets.

Tip: To disable snippets suggestions, set editor.snippetSuggestions to 'none' in your settings file. The editor.snippetSuggestions setting also lets you change where snippets appear in the suggestions: at the top ('top'), at the bottom ('bottom'), or inlined ordered alphabetically ('inline'). The default is 'inline'.

JSDoc support

VS Code understands many standard JSDoc annotations, and uses these annotations to provide rich IntelliSense. You can optionally even use the type information from JSDoc comments to type check your JavaScript.

Quickly create JSDoc comments for functions by typing /** before the function declaration, and select the JSDoc comment snippet suggestion:

To disable JSDoc comment suggestions, set 'javascript.suggest.completeJSDocs': false.

Hover Information

Hover over a JavaScript symbol to quickly see its type information and relevant documentation.

The ⌘K ⌘I (Windows, Linux Ctrl+K Ctrl+I) keyboard shortcut shows this hover info at the current cursor position.

Signature Help

As you write JavaScript function calls, VS Code shows information about the function signature and highlights the parameter that you are currently completing:

Signature help is shown automatically when you type a ( or , within a function call. Press ⇧⌘Space (Windows, Linux Ctrl+Shift+Space) to manually trigger signature help.

Auto imports

Automatic imports speed up coding by suggesting available variables throughout your project and its dependencies. When you select one of these suggestions, VS Code automatically adds an import for it to the top of the file.

Just start typing to see suggestions for all available JavaScript symbols in your current project. Auto import suggestions show where they will be imported from:

React Js In Visual Studio Code

If you choose one of these auto import suggestions, VS Code adds an import for it.

In this example, VS Code adds an import for Button from material-ui to the top of the file:

To disable auto imports, set 'javascript.suggest.autoImports' to false.

Tip: VS Code tries to infer the best import style to use. You can explicitly configure the preferred quote style and path style for imports added to your code with the javascript.preferences.quoteStyle and javascript.preferences.importModuleSpecifier settings.

Visual Studio Code Run React

Formatting

VS Code's built-in JavaScript formatter providers basic code formatting with reasonable defaults.

The javascript.format.*settings configure the built-in formatter. Or, if the built-in formatter is getting in the way, set 'javascript.format.enable' to false to disable it.

For more specialized code formatting styles, try installing one of the JavaScript formatting extensions from the Marketplace.

JSX and auto closing tags

All of VS Code's JavaScript features also work with JSX:

You can use JSX syntax in both normal *.js files and in *.jsx files.

Visual Studio React Tutorial

VS Code also includes JSX-specific features such as autoclosing of JSX tags:

Set 'javascript.autoClosingTags' to false to disable JSX tag closing.

Code navigation

Code navigation lets you quickly navigate JavaScript projects.

  • Go To DefinitionF12 - Go to the source code of a symbol definition.
  • Peek Definition⌥F12 (Windows Alt+F12, Linux Ctrl+Shift+F10) - Bring up a Peek window that shows the definition of a symbol.
  • Go to References⇧F12 (Windows, Linux Shift+F12) - Show all references to a symbol.
  • Go to Type Definition - Go to the type that defines a symbol. For an instance of a class, this will reveal the class itself instead of where the instance is defined.

You can navigate via symbol search using the Go to Symbol commands from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).

  • Go to Symbol in File⇧⌘O (Windows, Linux Ctrl+Shift+O)
  • Go to Symbol in Workspace⌘T (Windows, Linux Ctrl+T)

Rename

Press F2 to rename the symbol under the cursor across your JavaScript project:

Refactoring

VS Code includes some handy refactorings for JavaScript such as Extract function and Extract constant. Just select the source code you'd like to extract and then click on the lightbulb in the gutter or press (⌘. (Windows, Linux Ctrl+.)) to see available refactorings.

Available refactorings include:

  • Extract to method or function.
  • Extract to constant.
  • Convert between named imports and namespace imports.
  • Move to new file.

See Refactorings for more information about refactorings and how you can configure keyboard shortcuts for individual refactorings.

Unused variables and unreachable code

Unused JavaScript code, such the else block of an if statement that is always true or an unreferenced import, is faded out in the editor:

You can quickly remove this unused code by placing the cursor on it and triggering the Quick Fix command (⌘. (Windows, Linux Ctrl+.)) or clicking on the lightbulb.

To disable fading out of unused code, set 'editor.showUnused' to false. You can also disable fading of unused code only in JavaScript by setting:

Organize Imports

The Organize Imports Source Action sorts the imports in a JavaScript file and removes any unused imports:

You can run Organize Imports from the Source Action context menu or with the ⇧⌥O (Windows, Linux Shift+Alt+O) keyboard shortcut.

Organize imports can also be done automatically when you save a JavaScript file by setting:

Code Actions on Save

The editor.codeActionsOnSave setting lets you configure a set of Code Actions that are run when a file is saved. For example, you can enable organize imports on save by setting:

You can also set editor.codeActionsOnSave to an array of Code Actions to execute in order.

Here are some source actions:

  • 'organizeImports' - Enables organize imports on save.
  • 'fixAll' - Auto Fix on Save computes all possible fixes in one round (for all providers including ESLint).
  • 'fixAll.eslint' - Auto Fix only for ESLint.
  • 'addMissingImports' - Adds all missing imports on save.
React Js Visual Studio Code

See Node.js/JavaScript for more information.

Code suggestions

VS Code automatically suggests some common code simplifications such as converting a chain of .then calls on a promise to use async and await

Set 'javascript.suggestionActions.enabled' to false to disable suggestions.

References CodeLens

The JavaScript references CodeLens displays an inline count of reference for classes, methods, properties, and exported objects:

To enable the references CodeLens, set 'javascript.referencesCodeLens.enabled' to true.

Click on the reference count to quickly browse a list of references:

Update imports on file move

When you move or rename a file that is imported by other files in your JavaScript project, VS Code can automatically update all import paths that reference the moved file:

The javascript.updateImportsOnFileMove.enabled setting controls this behavior. Valid settings values are:

React Js Visual Studio Code
  • 'prompt' - The default. Asks if paths should be updated for each file move.
  • 'always' - Always automatically update paths.
  • 'never' - Do not update paths automatically and do not prompt.

Linters

Linters provides warnings for suspicious looking code. While VS Code does not include a built-in JavaScript linter, many JavaScript linter extensions available in the marketplace.

Tip: This list is dynamically queried from the VS Code Marketplace. Read the description and reviews to decide if the extension is right for you.

Type checking

You can leverage some of TypeScript's advanced type checking and error reporting functionality in regular JavaScript files too. This is a great way to catch common programming mistakes. These type checks also enable some exciting Quick Fixes for JavaScript, including Add missing import and Add missing property.

TypeScript tried to infer types in .js files the same way it does in .ts files. When types cannot be inferred, they can be specified explicitly with JSDoc comments. You can read more about how TypeScript uses JSDoc for JavaScript type checking in Working with JavaScript.

Type checking of JavaScript is optional and opt-in. Existing JavaScript validation tools such as ESLint can be used alongside built-in type checking functionality.

Debugging

VS Code comes with great debugging support for JavaScript. Set breakpoints, inspect objects, navigate the call stack, and execute code in the Debug Console. See the Debugging topic to learn more.

Debug client side

You can debug your client-side code using a browser debugger such as Debugger for Chrome, Debugger for Edge or Debugger for Firefox.

Debug server side

Debug Node.js in VS Code using the built-in debugger. Setup is easy and there is a Node.js debugging tutorial to help you.

Popular extensions

VS Code ships with excellent support for JavaScript but you can additionally install debuggers, snippets, linters, and other JavaScript tools through extensions.

Tip: The extensions shown above are dynamically queried. Click on an extension tile above to read the description and reviews to decide which extension is best for you. See more in the Marketplace.

Next steps

Read on to find out about:

  • Working with JavaScript - More detailed information about VS Code's JavaScript support and how to troubleshoot common issues.
  • jsconfig.json - Detailed description of the jsconfig.json project file.
  • IntelliSense - Learn more about IntelliSense and how to use it effectively for your language.
  • Debugging - Learn how to set up debugging for your application.
  • Node.js - A walkthrough to create an Express Node.js application.
  • TypeScript - VS Code has great support for TypeScript, which brings structure and strong typing to your JavaScript code.

Watch these introductory videos:

  • IntelliSense - Tutorial on IntelliSense with JavaScript.
  • Debugging - Learn how to debug a Node.js application.

Common questions

Does VS Code support JSX and React Native?

VS Code supports JSX and React Native. You will get IntelliSense for React/JSX and React Native from automatically downloaded type declaration (typings) files from the npmjs type declaration file repository. Additionally, you can install the popular React Native extension from the Marketplace.

To enable ES6 import statements for React Native, you need to set the allowSyntheticDefaultImports compiler option to true. This tells the compiler to create synthetic default members and you get IntelliSense. React Native uses Babel behind the scenes to create the proper run-time code with default members. If you also want to do debugging of React Native code, you can install the React Native Extension.

Does VS Code support the Dart programming language and the Flutter framework?

Yes, there are VS Code extensions for both Dart and Flutter development. You can learn more at the Flutter.dev documentation.

IntelliSense is not working for external libraries

React Js Intellisense Visual Studio Code

Automatic Type Acquisition works for dependencies downloaded by npm (specified in package.json), Bower (specified in bower.json), and for many of the most common libraries listed in your folder structure (for example jquery-3.1.1.min.js).

ES6 Style imports are not working.

When you want to use ES6 style imports but some type declaration (typings) files do not yet use ES6 style exports, then set the TypeScript compiler optionallowSyntheticDefaultImports to true.

Can I debug minified/uglified JavaScript?

Yes, you can. You can see this working using JavaScript source maps in the Node.js Debugging topic.

How do I disable Syntax Validation when using non-ES6 constructs?

Some users want to use syntax constructs like the proposed pipeline (|>) operator. However, these are currently not supported by VS Code's JavaScript language service and are flagged as errors. For users who still want to use these future features, we provide the javascript.validate.enablesetting.

With javascript.validate.enable: false, you disable all built-in syntax checking. If you do this, we recommend that you use a linter like ESLint to validate your source code.

Can I use other JavaScript tools like Flow?

React Js Visual Studio Code Shortcuts

Yes, but some of Flow's language features such as type and error checking may interfere with VS Code's built-in JavaScript support. To learn how to disable VS Code's built-in JavaScript support, see Disable JavaScript support.