意外的令牌导入正在运行节点server.js [英] Unexpected token import running node server.js

查看:58
本文介绍了意外的令牌导入正在运行节点server.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 Angular Universal快速入门,并且在运行时遇到此错误节点server.js".

I'm trying to do the Angular Universal quickstart and am getting this error when I run "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应用添加通用性.

I'm adding universal to an existing angular 2 app.

如何消除错误?

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

I get a couple errors when running the 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>

我将其更改为此,但是它引发了错误:

I changed it to this but it is throwing errors:

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)

推荐答案

使用require代替import.

因为它们尚未实现.

Node使用的V8版本已完成大多数ES6功能.不幸的是,模块不是那些完整的功能之一.

Node uses a version of V8 with most of ES6 features completed. Unfortunately modules isn't one of those completed features.

这篇关于意外的令牌导入正在运行节点server.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆