未捕获的SyntaxError:意外的令牌<在AngularJS2中使用nodejs本地

时间:2016-06-27 21:33:32

标签: node.js angular systemjs

我正在尝试使用nodejs在本地运行angularjs2项目。我可以使用npm start运行它,但我需要使用节点app.js。

问题是它给出了一个错误“systemjs.config.js:1 Uncaught SyntaxError:Unexpected token<”。我不确定这个问题出在哪里,因为我对systemjs不够熟悉,无法调试此问题。

提前致谢。

的index.html

<!DOCTYPE html>
<html>
<head>
    <title>Fischer Hopkins</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <base href=".">

    <link href='https://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="stylesheets/normalize.css">
    <link rel="stylesheet" href="stylesheets/custom-material.css" type="text/css">
    <link rel="stylesheet" href="stylesheets/screen.css" media="screen, projection" type="text/css">

    <!-- 1. Load libraries -->
        <!-- Polyfill(s) for older browsers -->
    <script src="/core-js/client/shim.min.js"></script>
    <script src="/zone.js/dist/zone.js"></script>
    <script src="/reflect-metadata/Reflect.js"></script>
    <script src="/systemjs/dist/system.src.js"></script>
    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
        System.import('app/main').catch(function(err){
            console.error(err);
        });
    </script>
</head>
<!-- 3. Display the application -->
<body>
        <c-main>Loading...</c-main>
</body>

app.js

var express = require('express');
var path    = require('path');
var app = express();

app.use(express.static(path.join(__dirname, '/app')));
app.use(express.static(path.join(__dirname, '/node_modules')));

app.use('/*', function(req, res)
{
    res.sendFile(__dirname + '/index.html');
});

var portVar = process.env.PORT || 3000;
app.set('port', portVar);

var server = app.listen(app.get('port'), function() {
    var port = server.address().port;
    console.log('Listening on port: ' + port);
});

这是我的systemjs.config.js:

/**
 * System configuration for Angular 2 samples
 * Adjust as necessary for your application needs.
 */
(function(global) {
    // map tells the System loader where to look for things
    var map = {
        'app':                        'app', // 'dist',
        '@angular':                   'node_modules/@angular',
        'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
        'rxjs':                       'node_modules/rxjs'
    };
    // packages tells the System loader how to load when no filename and/or no extension
    var packages = {
        'app':                        { main: 'main.js',  defaultExtension: 'js' },
        'rxjs':                       { defaultExtension: 'js' },
        'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
    };
    var ngPackageNames = [
        'common',
        'compiler',
        'core',
        'forms',
        'http',
        'platform-browser',
        'platform-browser-dynamic',
        'router',
        'router-deprecated',
        'upgrade',
    ];
    // Individual files (~300 requests):
    function packIndex(pkgName) {
        packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
    }
    // Bundled (~40 requests):
    function packUmd(pkgName) {
        if (pkgName == "router") {
            packages['@angular/' + pkgName] = { main: 'index.js', defaultExtension: 'js' };
        } else {
            packages['@angular/' + pkgName] = { main: 'bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
        }
    }
    // Most environments should use UMD; some (Karma) need the individual index files
    var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
    // Add package entries for angular packages
    ngPackageNames.forEach(setPackageConfig);
    var config = {
        map: map,
        packages: packages
    };
    System.config(config);
})(this);

文件结构:

|-app
  |-main.ts
|
|-app.js
|-index.html
|-package.json
|-tsconfig.json
|-typings.json
|-systemjs.config.js

1 个答案:

答案 0 :(得分:0)

错误说:

systemjs.config.js:1 Uncaught SyntaxError: Unexpected token <

systemjs.config.js文件无效javascript。

这通常是因为服务器为您提供了HTML(以<开头)而不是JS,可能是为了响应错误的URL(例如,404页而不是脚本)。

尝试手动输入网址(在地址栏中输入根网址并向其添加systemjs.config.js),看看会发生什么。您确定不会错过主要斜杠(/)或完整网址吗?