我有angular 5应用程序和nodejs 9服务器,当我尝试使用以下命令执行节点服务器时,我收到错误。 它开始,因为我使用角谷歌地图库 https://angular-maps.com/
sudo node server.js
错误:
/node_modules/@agm/core/directives/marker.js:1
(function (exports, require, module, __filename, __dirname) { import { Directive, EventEmitter, ContentChildren, QueryList, Input, Output } from '@angular/core';
^^^^^^
SyntaxError: Unexpected token import
at new Script (vm.js:51:7)
at createScript (vm.js:136:10)
at Object.runInThisContext (vm.js:197:10)
at Module._compile (module.js:613:28)
at Object.Module._extensions..js (module.js:660:10)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:501:12)
at Function.Module._load (module.js:493:3)
at Module.require (module.js:593:17)
at require (internal/module.js:11:18)
server.js内容
'use strict';
/* Server specific version of Zone.js */
require('zone.js/dist/zone-node');
const express = require('express');
const ngUniversal = require('@nguniversal/express-engine');
/* The server bundle is loaded here, it's why you don't want a changing hash in it */
const appServer = require('./dist-server/main.bundle');
/* Server-side rendering */
function angularRouter(req, res) {
/* Server-side rendering */
res.render('index', {req, res});
}
const app = express();
/* Root route before static files, or it will serve a static index.html, without pre-rendering */
app.get('/', angularRouter);
/* Serve the static files generated by the CLI (index.html, CSS? JS, assets...) */
app.use(express.static(`${__dirname}/dist`));
/* Configure Angular Express engine */
app.engine('html', ngUniversal.ngExpressEngine({
bootstrap: appServer.AppServerModuleNgFactory
}));
app.set('view engine', 'html');
app.set('views', 'dist');
/* Direct all routes to index.html, where Angular will take care of routing */
app.get('*', angularRouter);
app.listen(80, () => {
console.log(`Listening on http://localhost:80`);
});
nodejs:v9.3.0
角度:5.0
答案 0 :(得分:0)
直接使用导入时仍然需要--experimental-mode而不使用像babel这样的转换器!
从节点10开始,将支持本机节点。
https://nodejs.org/api/esm.html
参见=> (复制条目BTW !!!) Node.js - SyntaxError: Unexpected token import
答案 1 :(得分:0)
我解决了这个问题,显然,我的server.js为客户端和服务器端进行了渲染。
我离开那个
'use strict';
/* Server specific version of Zone.js */
require('zone.js/dist/zone-node');
const express = require('express');
const app = express();
/* Serve the static files generated by the CLI (index.html, CSS? JS, assets...) */
app.use(express.static(`${__dirname}/dist`));
app.set('view engine', 'html');
app.set('views', 'dist');
app.listen(80, () => {
console.log(`Listening on http://localhost:80`);
});