skip to Main Content

Currently I am trying to build a project using Angular Universal. I’m using Angular 4. If I import Material Module – the project can be built, but when serving through ts-node an error appears –

distngfactorynode_modules@angularmaterialtypingsindex.ngfactory.ts:9
import * as import0 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 Module._extensions..js (module.js:580:10)

Or If I try to import NgXBootstrap then this error appears –

node_modulesngx-bootstrapdropdownbs-dropdown.module.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)

I’m following this project structure – https://github.com/designcourse/angular-seo

I am pasting the code here –

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

//Material Module
import {MaterialModule} from '@angular/material';

//Ngx Bootstrap
import {BsDropdownModule} from 'ngx-bootstrap';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { PostsComponent } from './posts/posts.component';

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    PostsComponent
  ],
  imports: [
    BrowserModule.withServerTransition({appId: 'universal-app'}),
    FormsModule,
    HttpModule,
    AppRoutingModule,
    MaterialModule,
    BsDropdownModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]

})
export class AppModule { }

app.server.module.ts

import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';

@NgModule({
imports: [
    ServerModule,
    AppModule
],
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}!`);
});

I guess this is a problem for all third party libraries in Angular Universal. So, How do I properly import these libraries ?

2

Answers


  1. I would suggest you use this seed project ng-seed/universal instead of the one you are currently following.

    Now if you structure your app following ng-seed/universal then for using Material Module and BsDropdownModule you would simply add these lines in client/app/app.module.ts

    import {MaterialModule} from '@angular/material';
    import {BsDropdownModule} from 'ngx-bootstrap';
    

    Make sure you update your package.json accordingly. Then run :

    npm install
    
    # dev build (Universal)
    npm run build:universal-dev
    # prod build (Universal)
    npm run build:universal-prod
    
    # start the server (Angular Universal)
    npm run serve
    

    All should be working alright now.

    Login or Signup to reply.
  2. Currently I have an app using MdButtonModule, MdSidenavModule, and MdToolbarModule. These definitely work, and the trick was to import hammerjs in src/main.ts

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search