13

Has anyone gotten Angular2 (beta) to work in Visual Studio?

I am using Visual Studio 2015 with update 1 and I have n HTMLTypeScript project.

If I set the 'module system' to "System" I get build errors (such as: Cannot find module 'angular/core') but it will compile the ts files on save (not on build). If I already have the application running I can refresh the page and it works.

If I set the 'module system' to "CommonJS" it will build properly but I get runtime errors such as "'require' is undefined" and "Unable to get property 'split' of undefined or null reference".

I think I should use system because that is what works when using tsconfig.json and the lite-server (with npm start) that is used in the tutorials.

Don Chambers
  • 3,798
  • 9
  • 33
  • 74
  • I'm running into issues with it on my question for VS 2013: http://stackoverflow.com/questions/34366928/how-can-i-setup-my-vs-2013-typescript-project-to-compile-my-typescript-files-but I remember running into this issue myself when I was working with it in Intellij. The solution resolved itself when I used TS 1.6 and Exclude in my tsconfig.json. – Zach Dec 19 '15 at 04:23
  • I am using 1.7.3 and the problem is not with node-modules, it's with the TS that I wrote (from the angualr2 tutorial). I don't think I need to exclude this, it needs to compile. – Don Chambers Dec 19 '15 at 06:37
  • yeah, i tried too to get work the angular 2 with visual studio, but after strange behavior and multiple "boolean sh*t" errors and 8 hours of trying to solve i just shift-deleted my project... – Ceylan Mumun Kocabaş Apr 22 '16 at 08:10

1 Answers1

8

I have angular2 working with Visual Studio 2015. I had to install .NET 5 RC1 to support tsconfig.json. They have a completely new project structure which I like very much. Here is my setup:

Solution Explorer

You'll notice that I put my app folder and ts files inside my wwwroot - that's just my preference, and it's following the angular2 quickstart. I copied the bundled files from my node modules into my scripts folder inside my wwwroot.

I created a package.json based on the angular2 beta quickstart:

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.0",
    "systemjs": "0.19.6",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.0",
    "zone.js": "0.5.10"
  }
}

Then I right-clicked on the package.json and selected "Restore Packages" and it ran the NPM install to create the node_modules folder with all the packages you see. Then I setup my tsconfig.json like so and put it in the root:

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules"
  ]
}

This is key to making it work. I actually had to reload the solution for it to see the tsconfig.json and start using it. In addition to this, I went into my Tools > Options and set the typescript options like so: TypeScript Options

Next I added my index.html file:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Sample Angular 2 App</title>
</head>
<body>
    <my-app>Loading...</my-app>

    <script src="scripts/angular2-polyfills.js"></script>
    <script src="scripts/system.src.js"></script>
    <script src="scripts/Rx.js"></script>
    <script src="scripts/angular2.dev.js"></script>
    <script>
        System.config({
            packages: {
                app: {
                    format: 'register',
                    defaultExtension: 'js'
                }
            }
        });

        System.import('app/boot').then(null, console.error.bind(console));
    </script>
</body>
</html>

The order of your script includes is important! Here's my MyApp.ts file:

import { Component } from "angular2/core";

@Component({
    selector: "my-app",
    template: `
        <div>Hello from Angular 2</div>
    `
})
export class MyApp {
    public constructor() {
    }
}

and my boot.ts file:

import { bootstrap } from "angular2/platform/browser";
import { MyApp } from "./MyApp";

bootstrap(MyApp);

At this point I felt I was ready to run it, so I launched it and navigated to my /index.html - but all I saw was "Hello World!". It seemed to be ignoring my path to serve index.html and was statically writing out text. Looking at the Startup.cs, I found the problem.:

namespace Angular2Demo
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {
            app.UseIISPlatformHandler();

            // Add this!
            app.UseStaticFiles();

            // Remove this!
            //app.Run(async (context) =>
            //{
            //    await context.Response.WriteAsync("Hello World!");
            //});
        }

        // Entry point for the application.
        public static void Main(string[] args) => WebApplication.Run<Startup>(args);
    }
}

By commenting out the app.Run and adding the app.UseStaticFiles(); (which required installing an additional package), I was ready to try again:

Eureka!

Success!

Zach
  • 3,157
  • 1
  • 19
  • 32
  • 1
    I had tried the approach but gave up because of where node_modules was going (above wwwroot). I will revisit it. Thanks. – Don Chambers Dec 19 '15 at 15:23
  • 1
    Referencing the node js files from my html page meant that at deployment I would either have to maintain the node_modules directory structure at deployment or deploy the whole whopping 40+MB node_modules as well. I didn't like this approach so I copied the angular references that I needed to my scripts folder so that only the ones I need get deployed. – Zach Dec 19 '15 at 16:38
  • Could you post a link to .net RC1 that you referenced? – Michael Kang Dec 19 '15 at 22:14
  • Updated. I made the text a link. – Zach Dec 19 '15 at 22:21
  • 1
    I had to click the arrow next to "IIS Express" --> CLR Type (.NET Framework) --> and select ".NET Framework" before I could get the above example to work. +1 for @Zack solution!! – AdventurGurl Dec 24 '15 at 18:01
  • 2
    I also added: app.UseDefaultFiles(); before app.UseStaticFiles(); so I did not have to add /index.html to the end per - https://docs.asp.net/en/latest/fundamentals/static-files.html – AdventurGurl Dec 30 '15 at 23:53
  • Good. I added that myself later. – Zach Jan 01 '16 at 19:46
  • what do you use for managing dependencies? did you try jspm? I'm trying with jspm and modules cant be found – DAG Mar 04 '16 at 15:10
  • I have grown fond of npm. VS2015 has some cool intellisense when I'm editing my package.json. It will look up/autocomplete packages and versions and auto-download them when you save the file. The node_modules folder won't show up in your project so you'll have to click the "Show all files" button. – Zach Mar 04 '16 at 19:09
  • if somebody get shocked - it doesn't work in IE for some reason... (Chrome and Mozilla - works!) – Ceylan Mumun Kocabaş Apr 22 '16 at 10:09
  • I have complication with implementing your implementation, i just posted [question here](http://stackoverflow.com/q/36791432/5376197). The problem is with the typescript compilation. – Ceylan Mumun Kocabaş Apr 22 '16 at 11:14
  • @CeylanMumunKocabaş Make sure you are using backticks for your templates and not apostrophes - ` – Zach Apr 26 '16 at 15:52
  • it wasn't the problem, but i get it to work, it was the `browser.d.ts` file causing the issue. – Ceylan Mumun Kocabaş Apr 27 '16 at 07:38
  • 1
    ..but still have to clean project, rebuild it, and then build it to see my changes... in short: clean - build multiple time until it works... – Ceylan Mumun Kocabaş Apr 27 '16 at 07:49
  • Could you please post source code on github? There are so many issues that can be spoiled... For example I get this error: `Build:Duplicate identifier 'PropertyKey'` – Kuba Matjanowski Sep 30 '16 at 07:32
  • Angular 2 has entered release and a lot most likely changed since this was posted, but I'll be happy to see what I can do. In the meantime, look at this: https://github.com/angular/angular/issues/7786 – Zach Sep 30 '16 at 12:23