core: ng build - Unexpected token import error.

I’m submitting a … (check one with “x”)

[x ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[ ] support request => check the FAQ and search github for a similar issue before submitting
[ ] feature request
H:\d\node_modules\@ngx-translate\core\index.js:1
(function (exports, require, module, __filename, __dirname) { import { NgModule
} from "@angular/core";
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (H:\d\dist\ngfactory\src\app\app.server.module.ngfacto
ry.ts:18:1)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! d@0.0.0 build: `ts-node src/server.ts`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the d@0.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional log
ging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\ChrisR\AppData\Roaming\npm-cache\_logs\2017-06-26T16_41_10
_806Z-debug.log

app.server.ts

import { NgModule } from '@angular/core';

import { ServerModule } from '@angular/platform-server'

import { AppComponent } from './app.component';
import { AppModule } from './app.module';

// TRANSLATE
import { TranslateModule, TranslateLoader, TranslateService } from '@ngx-translate/core';
import { translateLoaderFactory } from './core/services/translate.service';

@NgModule({
    imports: [

        ServerModule,
        AppModule,

        // TRANSLATE
        TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useFactory: translateLoaderFactory
            }
        })        
    ],
    bootstrap: [ AppComponent ]
})

export class AppServerModule {}

server.ts

import 'reflect-metadata';
import 'zone.js/dist/zone-node';
import { platformServer, renderModuleFactory } from '@angular/platform-server'
import { enableProdMode } from '@angular/core'
import { AppServerModuleNgFactory } from '../dist/ngfactory/src/app/app.server.module.ngfactory'
import * as express from 'express';
import { readFileSync } from 'fs';
import { join } from 'path';

const PORT = 4000;

enableProdMode();

const app = express();

let template = readFileSync(join(__dirname, '..', 'dist', 'index.html')).toString();

app.engine('html', (_, options, callback) => {
  const opts = { document: template, url: options.req.url };

  renderModuleFactory(AppServerModuleNgFactory, opts)
    .then(html => callback(null, html));
});

app.set('view engine', 'html');
app.set('views', 'src')

app.get('*.*', express.static(join(__dirname, '..', 'dist')));

app.get('*', (req, res) => {
  res.render('index', { req });
});

app.listen(PORT, () => {
  console.log(`listening on http://localhost:${PORT}!`);
});

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 18

Commits related to this issue

Most upvoted comments

i forked the repo few days ago and i made some updates! please try my version and give me your feedback. If that works your machine, i will make a pull request.

So the solution is the following:

  1. remove the npm module via npm un --save @ngx-translate/core
  2. install my forked module via npm i --save git+ssh://git@github.com:AnthonyNahas/core.git or npm i --save https://github.com/AnthonyNahas/core.git

Cheers, no more Unexpected token import error 💃

@AnthonyNahas - can you look help us also with localize-router? I think that it’s the same problem. Maybe you can give me a tips how I can manage with this problem?

/home/xxx/Projects/private/angular4-cli-seed/node_modules/localize-router/src/localize-router.config.js:1
(function (exports, require, module, __filename, __dirname) { import { Inject, OpaqueToken } from '@angular/core';

@AnthonyNahas I think you wait on our feedback, so for me your solution work perfectly. Thank you! I had another error but it was connected with wrong json import.

Hi @AnthonyNahas, thanks for your work. I am just wondering if by any chance you have any idea on the following AoT Error. The problem is different with unexpected token, but you may have any idea on this matter, since you generally solved the unexpected token import. Appreciate the lead, been trying to solve this for days with no progress. screen shot 2017-09-14 at 9 24 36 am