Angular2 Visual Studio 2017 MVC: Uncaught TypeError: System.config is not a function

Viewed 579

I am building Angular2 web application through Visual Studio 2017 ASP.NET MVC

All the basic configuration I did.

But when I am trying to build app then it is throwing an error.

Error:

enter image description here

I am surprised some time application run successfully and sometimes it throws this error.

Following this link to do quick setup

package.json, tsconfig.json and systemjs.config.js copied from quickstart

_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
<script src="/node_modules/core-js/client/shim.min.js"></script>
<script src="/node_modules/zone.js/dist/zone.js"></script>
<script src="/node_modules/systemjs/dist/system.src.js"></script>
<script src="/system.config.js"></script>
<script>
    System.import('app').catch(function (err) { console.error(err); });
</script>

<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")

</head>
<body>
<div class="container body-content">
    @RenderBody()
    <hr />
    <footer>
        <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
    </footer>
</div>

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>

Index.cshtml

@{
ViewBag.Title = "Home Page";
}

<app-root>Loading...</app-root>
1 Answers

You need to resolve the path to your script with Url.Content():

<script src="@Url.Content("~/node_modules/core-js/client/shim.min.js")"></script>
<script src="@Url.Content("~/node_modules/zone.js/dist/zone.js")"></script>
<script src="@Url.Content("~/node_modules/systemjs/dist/system.src.js")"></script>
<script src="@Url.Content("~/system.config.js")"></script>

This way you ensure it is always resolving to the root of your applications path and not some fubfolder if your Route changes to /controller/action/something.

Related