Skip to content

Commit 24cd59e

Browse files
authored
Merge pull request #6258 from Mikejo5000/mikejo-br16
Node.js updates for npm and js library integration
2 parents e5a87b1 + c4d100d commit 24cd59e

11 files changed

+183
-26
lines changed

docs/javascript/develop-javascript-code-without-solutions-projects.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Starting in Visual Studio 2017, you can [develop code without projects or soluti
2121
To get started, select **File** > **Open** > **Folder** from the toolbar. Solution Explorer displays all the files in the folder, and you can open any of the files to begin editing. In the background, Visual Studio indexes the files to enable npm, build, and debug features.
2222

2323
> [!IMPORTANT]
24-
> Many of the features described in this article, including npm integration, require Visual Studio 2017 version 15.8 or later versions.
24+
> Many of the features described in this article, including npm integration, require Visual Studio 2017 version 15.8 or later versions. The Visual Studio **Node.js development** workload must be installed.
2525
2626
## npm integration
2727

docs/javascript/javascript-in-vs-2019.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: JavaScript and TypeScript in Visual Studio 2019
3-
ms.date: "03/27/2019"
3+
ms.date: "03/16/2020"
44
ms.technology: vs-javascript
55
ms.topic: conceptual
66
dev_langs:
@@ -31,11 +31,39 @@ The option to restore to the legacy JavaScript language service is no longer ava
3131
Visual Studio 2019 provides several options for integrating TypeScript compilation into your project:
3232

3333
* [The TypeScript NuGet package](https://www.nuget.org/packages/Microsoft.TypeScript.MSBuild). When the NuGet package for TypeScript 3.2 or higher is installed into your project, the corresponding version of the TypeScript language service gets loaded in the editor.
34-
* The TypeScript SDK, available by default in the Visual Studio installer, as well as a standalone SDK download from the [VS Marketplace](https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.typescript-331-vs2017).
3534
* [The TypeScript npm package](https://www.npmjs.com/package/typescript). When the npm package for TypeScript 2.1 or higher is installed into your project, the corresponding version of the TypeScript language service gets loaded in the editor.
35+
* The TypeScript SDK, available by default in the Visual Studio installer, as well as a standalone SDK download from the [VS Marketplace](https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.typescript-331-vs2017).
3636

3737
For projects developed in Visual Studio 2019, we encourage you to use the TypeScript NuGet and npm packages for greater portability across different platforms and environments.
3838

39+
One common usage for the NuGet package is to compile TypeScript using the .NET Core CLI. Unless you manually edit your project file to import build targets from a TypeScript SDK installation, the NuGet package is the only way to enable TypeScript compilation using .NET Core CLI commands such as `dotnet build` and `dotnet publish`.
40+
41+
## Remove default imports (ASP.NET Core projects)
42+
43+
In older projects that use the [non-SDK-style format](https://docs.microsoft.com/nuget/resources/check-project-format), you may need to remove some project file elements.
44+
45+
If you are using the NuGet package for MSBuild support for a project, the project file must not import `Microsoft.TypeScript.Default.props` or `Microsoft.TypeScript.targets`. The files get imported by the NuGet package, so including them separately may cause unintended behavior.
46+
47+
1. Right-click the project and choose **Unload Project**.
48+
49+
1. Right-click the project and choose **Edit \<*project file name*\>**.
50+
51+
The project file opens.
52+
53+
1. Remove references to `Microsoft.TypeScript.Default.props` and `Microsoft.TypeScript.targets`.
54+
55+
The imports to remove look similar to the following:
56+
57+
```xml
58+
<Import
59+
Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props"
60+
Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props')" />
61+
62+
<Import
63+
Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets"
64+
Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets')" />
65+
```
66+
3967
## Projects
4068

4169
UWP JavaScript apps are no longer supported in Visual Studio 2019. You cannot create or open JavaScript UWP projects (files with extension *.jsproj*). You can learn more using our documentation on [creating Progressive Web Apps (PWAs)](/microsoft-edge/progressive-web-apps/get-started) that run well on Windows.
Loading
Loading
Loading
76.6 KB
Loading
56.7 KB
Loading

docs/javascript/media/npm-output.png

13.5 KB
Loading

docs/javascript/npm-package-management.md

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Manage npm packages"
33
description: Visual Studio helps you to manage packages using the Node.js package manager (npm)
44
ms.custom: "seodec18"
5-
ms.date: "06/06/2018"
5+
ms.date: "03/12/2020"
66
ms.topic: "conceptual"
77
ms.devlang: javascript
88
author: "mikejo5000"
@@ -16,25 +16,31 @@ ms.workload:
1616

1717
# Manage npm packages in Visual Studio
1818

19-
npm allows you to install and manage packages for use in your Node.js applications. If you're unfamiliar with
20-
npm and want to learn more, go to the [npm documentation](https://docs.npmjs.com/).
19+
npm allows you to install and manage packages for use in your Node.js applications. Visual Studio makes it easy to interact with npm and issue npm commands through the UI or directly. If you're unfamiliar with npm and want to learn more, go to the [npm documentation](https://docs.npmjs.com/).
2120

22-
Visual Studio makes it easy to interact with npm and issue npm commands through the UI or directly. You can use the following methods:
21+
Visual Studio integration with npm is different depending on your project type.
22+
* [Node.js](#nodejs-projects)
23+
* [ASP.NET Core](#aspnet-core-projects)
24+
* [Open folder (Node.js)](../javascript/develop-javascript-code-without-solutions-projects.md)
25+
26+
> [!Important]
27+
> npm expects the *node_modules* folder and *package.json* in the project root. If your app's folder structure is different, you should modify your folder structure if you want to manage npm packages using Visual Studio.
28+
29+
> [!NOTE]
30+
> For existing Node.js projects, use the **From existing Node.js code** solution template to enable npm in your project.
31+
32+
## Node.js projects
33+
34+
For Node.js projects, use one the following methods:
2335
* [Install packages from Solution Explorer](#npmInstallWindow)
2436
* [Manage installed packages from Solution Explorer](#solutionExplorer)
2537
* [Use the `.npm` command in the Node.js Interactive Window](#interactive)
2638

2739
These features work together and synchronize with the project system and the *package.json* file in the project.
2840

29-
> [!Important]
30-
> NPM expects the *node_modules* folder and *package.json* in the project root. If your app's folder structure is different, you should update your folder structure if you want to manage npm packages using Visual Studio.
31-
32-
> [!NOTE]
33-
> For existing NPM projects, use the **From existing Node.js code** solution template.
34-
35-
## <a name="npmInstallWindow"></a> Install packages from Solution Explorer
41+
### <a name="npmInstallWindow"></a> Install packages from Solution Explorer (Node.js)
3642

37-
The easiest way to install npm packages is through the npm package installation window. To access this window, right-click the **npm** node in the project and select **Install New npm Packages**.
43+
For Node.js projects, the easiest way to install npm packages is through the npm package installation window. To access this window, right-click the **npm** node in the project and select **Install New npm Packages**.
3844

3945
![Install new npm package from solution explorer](../javascript/media/solution-explorer-install-package.png)
4046

@@ -47,18 +53,21 @@ In this window you can search for a package, specify options, and install.
4753
* **Selected version** - Select the version of the package you want to install.
4854
* **Other npm arguments** - Specify other standard npm arguments. For example, you can enter a version value such as `@~0.8` to install a specific version that is not available in the versions list.
4955

50-
You can see the progress of the installation in the npm tab in the Output window. This may take some time.
56+
You can see the progress of the installation in the **npm** output in the **Output** window. This may take some time.
57+
58+
![npm output](../javascript/media/npm-output.png)
5159

5260
> [!TIP]
53-
> You can search for scoped packages by prepending the search query with the scope you're interested in, for example, type `@types/mocha` to look for TypeScript definition files for mocha. Also, when installing type definitions for TypeScript, you can specify the TypeScript version you're targetting by adding `@ts2.6` in the npm argument field.
61+
> You can search for scoped packages by prepending the search query with the scope you're interested in, for example, type `@types/mocha` to look for TypeScript definition files for mocha. Also, when installing type definitions for TypeScript, you can specify the TypeScript version you're targeting by adding `@ts2.6` in the npm argument field.
5462
55-
## <a name="solutionExplorer"></a>Manage installed packages in Solution Explorer
63+
### <a name="solutionExplorer"></a>Manage installed packages in Solution Explorer (Node.js)
5664

5765
npm packages are shown in Solution Explorer. The entries under the **npm** node mimic the dependencies in the *package.json* file.
5866

5967
![Search npm package](../javascript/media/solution-explorer-status.png)
6068

6169
### Package status
70+
6271
* ![Installed package](../javascript/media/installed-npm.png) - Installed and listed in package.json
6372
* ![Extraneous package](../javascript/media/extraneous-npm.png) - Installed, but not explicitly
6473
listed in package.json
@@ -69,7 +78,7 @@ Right-click a package node or the **npm** node to take one of the following acti
6978
* **Update packages** to the latest version
7079
* **Uninstall a package** and remove from *package.json*
7180

72-
## <a name="interactive"></a>Use the .npm command in the Node.js Interactive Window
81+
### <a name="interactive"></a>Use the .npm command in the Node.js Interactive Window (Node.js)
7382

7483
You can also use the `.npm` command in the Node.js Interactive Window to execute
7584
npm commands. To open the window, right-click the project in Solution Explorer and choose **Open Node.js Interactive Window**.
@@ -86,3 +95,51 @@ In the window, you can use commands such as the following to install a package:
8695
> [!Tip]
8796
> If your project doesn't contain a package.json file, use `.npm init -y` to create a new package.json file
8897
> with default entries.
98+
99+
## ASP.NET Core projects
100+
101+
For projects such as ASP.NET Core projects, you can integrate npm support in your project and use npm to install packages.
102+
* [Add npm support to a project](#npmAdd)
103+
* [Install packages using package.json](#npmInstallPackage)
104+
105+
>[!NOTE]
106+
> For ASP.NET Core projects, you can also use [Library Manager](https://docs.microsoft.com/aspnet/core/client-side/libman/?view=aspnetcore-3.1) or yarn instead of npm to install client-side JavaScript and CSS files.
107+
108+
### <a name="npmAdd"></a> Add npm support to a project (ASP.NET Core)
109+
110+
If your project does not already include a *package.json* file, you can add one enable npm support by adding a package.json file to the project.
111+
112+
1. To add the file, right-click the project in Solution Explorer and choose **Add** > **New Item**. Choose the **npm Configuration File**, use the default name, and click **Add**.
113+
114+
![Add package.json to your project](../javascript/media/npm-add-package-json.png)
115+
116+
1. Include one or more npm packages in the `dependencies` or `devDependencies` section of *package.json*. For example, you might add the following to the file:
117+
118+
```json
119+
"devDependencies": {
120+
"gulp": "4.0.2",
121+
"@types/jquery": "3.3.33"
122+
}
123+
```
124+
125+
When you save the file, Visual Studio adds the package under the **Dependencies / npm** node in Solution Explorer. If you don't see the node, right-click **package.json** and choose **Restore Packages**.
126+
127+
>[!NOTE]
128+
> In some scenarios, Solution Explorer may indicate that an npm package is out of sync with *package.json* due to a known issue described [here](https://github.com/aspnet/Tooling/issues/479). For example, the package may appear as not installed when it is installed. In most cases, you can update Solution Explorer by deleting *package.json*, restarting Visual Studio, and re-adding the *package.json* file as described earlier in this article.
129+
130+
### <a name="npmInstallPackage"></a>Install packages using package.json (ASP.NET Core)
131+
132+
For projects with npm included, you can configure npm packages using `package.json`. Right-click the npm node in Solution Explorer and choose **Open package.json**.
133+
134+
![Search npm package](../javascript/media/npm-add-package.png)
135+
136+
IntelliSense in *package.json* helps you select a particular version of an npm package.
137+
138+
![Search npm package](../javascript/media/npm-add-package-intellisense.png)
139+
140+
When you save the file, Visual Studio adds the package under the **Dependencies / npm** node in Solution Explorer. If you don't see the node, right-click **package.json** and choose **Restore Packages**.
141+
142+
It may take several minutes to install a package. Check progress on package installation by switching to **npm** output in the **Output** window.
143+
144+
![npm output](../javascript/media/npm-output.png)
145+

docs/javascript/tutorial-aspnet-with-typescript.md

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Create an ASP.NET Core app with TypeScript"
33
description: In this tutorial, you create an app using ASP.NET Core and TypeScript
4-
ms.date: 01/03/2020
4+
ms.date: 03/16/2020
55
ms.topic: tutorial
66
ms.devlang: javascript
77
author: mikejo5000
@@ -34,6 +34,7 @@ In this tutorial, you learn how to:
3434
> * Add the NuGet package for TypeScript support
3535
> * Add some TypeScript code
3636
> * Run the app
37+
> * Add a third-party library using npm
3738
3839
## Prerequisites
3940

@@ -52,21 +53,26 @@ In this tutorial, you learn how to:
5253

5354
Visual Studio manages files for a single application in a *project*. The project includes source code, resources, and configuration files.
5455

56+
>[!NOTE]
57+
> To start with an empty ASP.NET Core project and add a TypeScript frontend, see [ASP.NET Core with TypeScript](https://www.typescriptlang.org/docs/handbook/asp-net-core.html) instead.
58+
5559
In this tutorial, you begin with a simple project containing code for an ASP.NET Core MVC app.
5660

5761
1. Open Visual Studio.
5862

5963
1. Create a new project.
6064

6165
::: moniker range=">=vs-2019"
62-
Press **Esc** to close the start window. Type **Ctrl + Q** to open the search box, type **ASP.NET**, then choose **ASP.NET Core Web Application - C#**. In the dialog box that appears, choose **Create**.
66+
If the start window is not open, choose **File** > **Start Window**. On the start window, choose **Create a new project**. In the language drop-down list, choose **C#**. In the search box, type **ASP.NET**, then choose **ASP.NET Core Web Application**. Choose **Next**.
67+
68+
Type a name for the project and choose **Create**.
6369
::: moniker-end
6470
::: moniker range="vs-2017"
6571
From the top menu bar, choose **File** > **New** > **Project**. In the left pane of the **New Project** dialog box, expand **Visual C#**, then choose **.NET Core**. In the middle pane, choose **ASP.NET Core Web Application - C#**, then choose **OK**.
6672
::: moniker-end
6773
If you don't see the **ASP.NET Core Web Application** project template, you must add the **ASP.NET and web development** workload. For detailed instructions, see the [Prerequisites](#prerequisites).
6874

69-
1. After you choose **Create**, select **Web Application (Model-View-Controller)** in the dialog box, and then choose **Create**.
75+
1. In the dialog box that appears, select **Web Application (Model-View-Controller)** in the dialog box, and then choose **Create** (or **OK**).
7076

7177
![Choose the MVC template](../javascript/media/aspnet-core-ts-mvc-template.png)
7278

@@ -206,6 +212,76 @@ In this tutorial, you begin with a simple project containing code for an ASP.NET
206212
207213
The application pauses at the breakpoint. Now, you can inspect variables and use debugger features.
208214
215+
## Add TypeScript support for a third-party library
216+
217+
1. Follow instructions in [npm package management](../javascript/npm-package-management.md##aspnet-core-projects) to add a package to your project.
218+
219+
>[!NOTE]
220+
> For ASP.NET Core projects, you can also use [Library Manager](https://docs.microsoft.com/aspnet/core/client-side/libman/?view=aspnetcore-3.1) or yarn instead of npm to install client-side JavaScript and CSS files.
221+
222+
1. In this example, add a TypeScript definition file for jQuery to your project. Include the following in your *package.json* file.
223+
224+
```json
225+
"devDependencies": {
226+
"@types/jquery": "3.3.33"
227+
}
228+
```
229+
230+
This adds TypeScript support for jQuery. The jQuery library itself is already included in the MVC project template (look under wwwroot/lib in Solution Explorer). If you are using a different template, you may need to include the jquery npm package as well.
231+
232+
1. If the package in Solution Explorer is not installed, right-click the npm node and choose **Restore Packages**.
233+
234+
>[!NOTE]
235+
> In some scenarios, Solution Explorer may indicate that an npm package is out of sync with *package.json* due to a known issue described [here](https://github.com/aspnet/Tooling/issues/479). For example, the package may appear as not installed when it is installed. In most cases, you can update Solution Explorer by deleting *package.json*, restarting Visual Studio, and re-adding the *package.json* file as described earlier in this article.
236+
237+
1. In Solution Explorer, right-click the scripts folder and choose **Add** > **New Item**.
238+
239+
1. Choose **TypeScript File**, type *library.ts*, and choose **Add**.
240+
241+
1. In *library.ts*, add the following code.
242+
243+
```ts
244+
var jqtest = {
245+
showMsg: function (): void {
246+
let v: any = jQuery.fn.jquery.toString();
247+
let content: any = $("#ts-example-2")[0].innerHTML;
248+
alert(content.toString());
249+
$("#ts-example-2")[0].innerHTML = content + " " + v + "!!";
250+
}
251+
};
252+
253+
jqtest.showMsg();
254+
```
255+
256+
For simplicity, this code displays a message using jQuery and an alert.
257+
258+
With TypeScript type definitions for jQuery added, you get IntelliSense support on jQuery objects when you type a "." following a jQuery object, as shown here.
259+
260+
![jquery IntelliSense](../javascript/media/aspnet-core-ts-jquery-intellisense.png)
261+
262+
1. In _Layout.cshtml, update the script references to include `library.js`.
263+
264+
```html
265+
<script src="~/js/app.js"></script>
266+
<script src="~/js/library.js"></script>
267+
```
268+
269+
1. In Index.cshtml, add the following HTML to the end of the file.
270+
271+
```html
272+
<div>
273+
<p id="ts-example-2">jQuery version is:</p>
274+
</div>
275+
```
276+
277+
1. Press **F5** (**Debug** > **Start Debugging**) to run the application.
278+
279+
The app opens in the browser.
280+
281+
Click **OK** in the alert to see the page updated to **jQuery version is: 3.3.1!!**.
282+
283+
![jquery example](../javascript/media/aspnet-core-ts-jquery-example.png)
284+
209285
## Next steps
210286

211287
You may want to learn more details about using TypeScript with ASP.NET Core.

docs/javascript/tutorial-nodejs-with-react-and-jsx.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,6 @@ For this scenario, use Chrome.
401401
For Microsoft Edge (Chromium), also shut down all instances of Chrome. Because both browsers share the chromium code base, this gives the best results.
402402
::: moniker-end
403403

404-
::: moniker range=">=vs-2019"
405-
For Microsoft Edge (Chromium), also shut down all instances of Chrome. Because both browsers use the chromium code base, this gives the best results.
406-
::: moniker-end
407-
408404
2. Start your browser with debugging enabled.
409405

410406
::: moniker range=">=vs-2019"

0 commit comments

Comments
 (0)