运行节点server.js的意外令牌导入

时间:2016-12-16 07:50:57

标签: node.js angular angular-universal

我试图执行Angular Universal quickstart并在运行" 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$ 

我正在为现有的角度2应用添加通用功能。

如何摆脱错误?

编辑:运行npm installs时出现了几个错误:

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)

2 个答案:

答案 0 :(得分:3)

使用require代替import

因为它们尚未实施。

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

答案 1 :(得分:0)

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

实施例: typings install --save --global dt~node