Setting up ASP.NET Core in Visual Studio 2017 with npm, webpack, and TypeScript: Part I
Jakob Christensen
Posted on April 17, 2017
This post first appeared on my personal blog.
Example code on Github.
Things have been moving so fast in the last couple of years when it comes to web development. The days of page refreshes on web sites are long gone. The youngsters and hipsters want ajax, animations, spinners, and what not. The JavaScript world has exploded with cool libraries and what is the newest, coolest, hottest library right now changes faster than I change my underwear (or maybe I'm just getting old, or I need to change my underwear more often).
Recently, Microsoft came out with the final release of Visual Studio 2017 (VS), and I figured now would be a good time to get my head around ASP.NET Core and all the cool JavaScript things.
The case in mind is a small intranet website that will allow the users to do some quick actuarial calculations.
Getting started
Let's start by creating a Visual Studio ASP.NET Core project. Start Visual Studio 2017 and create a new project from the "ASP.NET Core Web Application (.NET Framework)" template. We will choose this template to make the code compatible with non-Core assemblies (the actuarial assemblies are compiled for .NET 4.5.2).
At the top of the dialog, choose which .NET framework you wish to be compatible with.
In the next dialog choose "Web Application", and Visual Studio will set up a basic structure for your project. Among other things, Visual Studio creates a folder named "wwwroot". This is where the files that will be published to the actual web server should be placed. Don't put anything else such as your code in that folder.
Setting up npm
To get all the JavaScript goodness into our project, we need to install the Node Package Manager (npm). As the name implies, npm is a package manager for JavaScript. Think Nuget for Javascript kind of thing. But npm can do more than that. It can also build TypeScript with the help of webpack which is what we will do later on.
You can install npm (Node.js) through the Visual Studio installer but you should always make sure that you are running the latest version. You can also download and install from the node.js web site.
Now we need to initialize node for our VS project directory. Open a command prompt and cd to the VS project folder (the folder where the .csproj file is located).
Run the command:
npm init -y
This will initialize npm for your project with default settings. The initialization creates a file named package.json. Notice how VS automatically recognizes the file and adds it to the web project.
You might want to open the package.json file and change the name attribute to all lower case since upper case letters are not supported (I have no idea why npm init does not change it to lower case while it is at it).
Run the following command to update npm at a later time.
npm install npm@latest
Installing webpack
Next up is webpack. Webpack seems to be the Swiss Army knife of JavaScript and it looks like people are moving from things like Grunt and Gulp to webpack. Also, in my humble opinion, the webpack documentation is far better than what you will se for a lot of the other "hot and cool" open source JavaScript libraries.
We will be using webpack for compiling TypeScript and for bundling script files.
Install webpack with npm by running the command
npm install webpack --save-dev
This will save webpack as a development (not production) dependency in package.json. We will be using webpack as part of the build process, hence the development dependency. The npm install command also creates a folder named "node_modules" in your project folder with dependencies. Do not include this folder in your project.
Next
In the next part of this series we will get up and running with TypeScript.
Â
Posted on April 17, 2017
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
April 17, 2017