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

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

问题描述

我正在尝试执行 Angular Universal 快速入门,但在运行时出现此错误节点服务器.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天全站免登陆