1

我正在尝试执行Angular Universal 快速入门,但在运行“node server.js”时出现此错误。

Benjamins-MBP:vepo Ben$ node server.js
/Users/Ben/Development/vepo/server.js:3
import 'angular2-universal/polyfills';
^^^^^^
SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3
Benjamins-MBP:vepo Ben$ 

我正在将通用添加到现有的 Angular 2 应用程序中。

如何摆脱错误?

编辑:运行 npm 安装时出现几个错误:

Benjamins-MBP:vepo Ben$ sudo npm install body-parser angular2-universal preboot express --save
Password:
vepo@1.0.0 /Users/Ben/Development/vepo
├── angular2-universal@2.1.0-rc.1 
├── body-parser@1.15.2 
├── express@4.14.0 
├── preboot@4.5.2 
└── UNMET PEER DEPENDENCY tslint@4.0.2 invalid

Benjamins-MBP:vepo Ben$ sudo typings install node express body-parser serve-static express-serve-static-core mime --global
typings ERR! message Unable to find "node" ("npm") in the registry.
typings ERR! message However, we found "node" for 2 other sources: "dt" and "env"
typings ERR! message You can install these using the "source" option.
typings ERR! message We could use your help adding these typings to the registry: https://github.com/typings/registry
typings ERR! caused by https://api.typings.org/entries/npm/node/versions/latest responded with 404, expected it to equal 200
typings ERR! 
typings ERR! cwd /Users/Ben/Development/vepo
typings ERR! system Darwin 16.1.0
typings ERR! command "/usr/local/bin/node" "/usr/local/bin/typings" "install" "node" "express" "body-parser" "serve-static" "express-serve-static-core" "mime" "--global"
typings ERR! node -v v6.9.1
typings ERR! typings -v 2.0.0
typings ERR! 
typings ERR! If you need help, you may report this error at:
typings ERR!   <https://github.com/typings/typings/issues>

我把它改成了这个,但它抛出了错误:

require('angular2-universal/polyfills');
require ('path');
require ('express');

// Angular 2 Universal
require ('@angular/router/provideRouter');
require ('@angular/core/enableProdMode');
require ('angular2-universal/*');

// replace this line with your Angular 2 root component
require ('./app/AppModule');
require ('./app/app.routes/*')

错误:

Benjamins-MBP:vepo Ben$ node server.js
module.js:471
    throw err;
    ^

Error: Cannot find module 'angular2-universal/polyfills'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/Ben/Development/vepo/server.js:3:1)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
Benjamins-MBP:vepo Ben$ node server.js
module.js:471
    throw err;
    ^

Error: Cannot find module 'angular2-universal/polyfills'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/Ben/Development/vepo/server.js:3:1)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
4

2 回答 2

3

使用require而不是import.

因为它们还没有实施。

Node 使用 V8 版本,完成了大部分 ES6 功能。不幸的是,模块不是那些已完成的功能之一。

于 2016-12-16T07:53:56.720 回答
0

对于您的打字安装,请使用“dt~”前缀

示例:typings install --save --global dt~node

于 2016-12-16T08:03:33.967 回答