0

I have angular 5 application and nodejs 9 server, when I try to execute the node server with the following command I get an error. It's started since I'm using angular google maps lib https://angular-maps.com/

sudo node server.js

Error:

/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 content

'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

angular: 5.0

hagai
  • 424
  • 1
  • 7
  • 13

2 Answers2

0

You still need --experimental-mode when using import directly without using a transpiler like babel!

As of node 10 it would be supported native node.

https://nodejs.org/api/esm.html

See => (Duplicate Entry BTW!!!) Node.js - SyntaxError: Unexpected token import

Bojoer
  • 898
  • 9
  • 19
0

I solved the issue, apparently, my server.js did the rendering for client side and server side.

I left with that

'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`);
});
hagai
  • 424
  • 1
  • 7
  • 13