意外的令牌进口运行节点server.js(Unexpected token import runni

2019-09-27 05:50发布

我试图做的角度通用快速启动 ,当我运行“节点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个应用程序。

如何摆脱错误的?

编辑:我得到了几个错误,运行故宫安装时:

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)

Answer 1:

使用require ,而不是import

因为他们只是尚未实现。

节点使用与大多数ES6的功能完成版V8的。 不幸的是模块是不是这些已完成的功能之一。



Answer 2:

为了您的分型安装使用“DT〜”前缀

例如:分型安装--save --global DT〜节点



文章来源: Unexpected token import running node server.js