Universal server bundle is not working properly
Bug Report or Feature Request (mark with an x)
- [x] bug report -> please search issues before submitting
- [ ] feature request
Versions.
@angular/cli: 1.3.0-rc.1 node: 6.10.3 os: darwin x64
Repro steps.
Follow these steps here:
https://github.com/angular/angular-cli/wiki/stories-universal-rendering
Add https://github.com/ngx-translate/core to the app.
Build a server bundle.
Test it and you will see the error below.
The log given by the failure.
<some-excluded-path>/ng-boilerplate/node_modules/@ngx-translate/core/src/translate.store.js:1
(function (exports, require, module, __filename, __dirname) { import { EventEmitter } from "@angular/core";
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
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.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.17 (<some-excluded-path>/ng-boilerplate/dist-server/main.75d7fa7aeab5f9b24f61.bundle.js:1:6560)
at e (<some-excluded-path>/ng-boilerplate/dist-server/main.75d7fa7aeab5f9b24f61.bundle.js:1:149)
at Object.2cGb (<some-excluded-path>/chrillewoodz/ng-boilerplate/dist-server/main.75d7fa7aeab5f9b24f61.bundle.js:1:7952)
at e (<some-excluded-path>/ng-boilerplate/dist-server/main.75d7fa7aeab5f9b24f61.bundle.js:1:149)
at Object.Zq8w (<some-excluded-path>/ng-boilerplate/dist-server/main.75d7fa7aeab5f9b24f61.bundle.js:1:28947)
at e (<some-excluded-path>/ng-boilerplate/dist-server/main.75d7fa7aeab5f9b24f61.bundle.js:1:149)
Desired functionality.
Basically the issue here is that node is resolving the wrong module. It's looking inside of the node_modules folder instead of in the vendor file in the server bundle.
Mention any other details that might be useful.
You can download a project with these steps already done:
https://github.com/chrillewoodz/ng-boilerplate/tree/universal
So simply run npm run universal and you will see the error.
@dannyrevenant - can you explain how we can repack a lib?
We solved this issue by transforming ngx-translate with babel with a pre-script in our package.json.
Eg. package.json
...
"preserve:yourproject": "node node_modules/babel-cli/bin/babel.js node_modules/@ngx-translate/core --out-dir node_modules/@ngx-translate/core --presets es2015"
...
And the then in your components/services/whatver you can simply:
import { TranslateService } from '@ngx-translate/core';
Thanks @sjogren - your solution work fine.
for @MarkPieszak and anyone interested in using universal along with i18n translations in aot mode, as when compiling the server.ts to server.js it does not work (since the compilation adds the html strings into the resulting server.js file) here is how i manged to implement it:
-
first, in order to handle the unexpected token issue mentioned here earlier, i am using the solution @AnthonyNahas wrote here: SebastianM/angular-google-maps#1052 . this solution is specific to the problematic package and therfore does not require to compile the server.ts to server.js
-
regarding compilation, i am compiling the server packaged and browser pacakge the same as in the universal starter cli exmaple however i am doing it for every language while adding each language compilation into its own direcotry. the resulting structure is as follows:
as you can see, i am also copying the assest folder into the dist folder (i am doing this with script off-course) -
the last step after i have everything compiled is to run the node server using the server.js file with the following server.js file:
//@formatter:off
// the above line tells the lint to not change the file
require('zone.js/dist/zone-node');
require('reflect-metadata');
const express = require('express');
const fs = require('fs');
const core = require('@angular/core');
const { platformServer, renderModuleFactory } = require('@angular/platform-server');
const { ngExpressEngine } = require('@nguniversal/express-engine');
// Import module map for lazy loading
const { provideModuleMap } = require('@nguniversal/module-map-ngfactory-loader');
// Import the AOT compiled factory for your AppServerModule.
// This import will change with the hash of your built server bundle.
const serverEn = require('./dist/en/server/main.bundle');
const serverFr = require('./dist/fr/server/main.bundle');
const serverEs = require('./dist/es/server/main.bundle');
// Faster server renders w/ Prod mode (dev mode never needed)
core.enableProdMode();
const app = express();
// Set the engine
app.engine('html', ngExpressEngine({
}));
app.set('view engine', 'html');
app.set('views', './dist/');
app.get(/^\/fr((\/(.*)))?$/, (req, res) => {
res.render('./fr/browser/app-root', {
req,
res,
bootstrap: serverFr.AppServerModuleNgFactory,
providers: [
provideModuleMap(serverFr.LAZY_MODULE_MAP)
]
});
})
app.get(/^\/es((\/(.*)))?$/, (req, res) => {
res.render('./es/browser/app-root', {
req,
res,
bootstrap: serverEs.AppServerModuleNgFactory,
providers: [
provideModuleMap(serverEs.LAZY_MODULE_MAP)
]
});
})
app.get(/\/(.*)/, (req, res) => {
res.render('./en/browser/app-root', {
req,
res,
bootstrap: serverEn.AppServerModuleNgFactory,
providers: [
provideModuleMap(serverEn.LAZY_MODULE_MAP)
]
});
})
app.listen(process.env.PORT || 3000, () => {
console.log(`Listening at ${process.env.PORT || 3000}`);
})
regarding all the static files, i am handling them as part of my hosting however you can also add them to the server.js file so that the node server will handle them.
Good Luck!
@chrillewoodz did you clear the issue or did you mada angular 2/4 seo ?
please send an invitation in slack i need help ([email protected])
PROBLEM:
app.post("/login", async( req, res )=>{ ^ SyntaxError: Unexpected token ( at createScript (vm.js:56:10) at Object.runInThisContext (vm.js:97:10) 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:389:7) at startup (bootstrap_node.js:149:9)
Hey @leiradk . you can contact me at skype: golan.yosi
@intellix did you ever figure out how to exclude testing files in your webpack.server.config.js? Running into the same issue with Heroku and seems silly for it to bundle Protractor.
@intellix Did you solved it? Becaues I have same problem, on my local is working correctly but on server are errors. Thanks a lot
eeerm, don't remember tbh but this works without issue using HTTP and GQL: https://github.com/intellix/angular-cli-universal https://angular-cli-universal.herokuapp.com/
It seems that the universal bundle picks up the paths specified by the "typings" field rather than the "main" entry point.
Example of a broken package.json that causes this error:
"main": "dist/npm/main.js",
"module": "dist/es/main.js",
"typings": "dist/es/main.d.ts",
The dist/server/main.bundle.js will contain a require statement pointing to the ES Module:
e.exports=require("@progress/kendo-angular-buttons/dist/es/buttons.module")
Moving the type definitions alongside the CommonJS modules fixes it, see updated package.json:
main: "cjs/main.js",
module: "esm/main.js",
typings: "cjs/main.d.ts"
And the updated dist/server/main.bundle.js:
e.exports=require("@progress/kendo-angular-buttons/dist/npm/buttons.module")
See reproduction case and the same with a fixed version of the dependency.
Do you have any news?
yeah, I'm still waiting for universal working smooooothly 😢
I was having the issue where 3rd party dependencies (like zone.js) weren't getting included (the main.bundle.js was using a require statement to grab it from node_modules which isn't on our prod environment) I was able to resolve it by adding the --bundle-dependencies=all argument to my ng build command. For Angular 6 you would need to add the "bundleDependencies": "all" property to the configurations property in the angular.json file. Not sure if that helps anyone else, just thought I'd share it as I didn't see mention of it here.
I cannot use bundleDependencies=all as it is throwing an error like this Data path "" should NOT have additional properties(bundleDependencies).
What are the requirements for a module to be Universal compatible?
Same error using @ngx-google-places-autocomplete
import { Directive, ElementRef, EventEmitter, Input, NgZone, Output } from '@angular/core'; ^^^^^^ SyntaxError: Unexpected token import
can anyone help me to resolve this???
Same error using @edcarroll/ng2-semantic-ui
universal-starter/node_modules/ng2-semantic-ui/dist/modules/dropdown/directives/dropdown-menu.js:11
import { Directive, ContentChild, forwardRef, Renderer2, ElementRef, ContentChildren, QueryList, Input, HostListener, ChangeDetectorRef } from "@angular/core"; ^ SyntaxError: Unexpected token
Someone fixed with such a problem?
Up, still doesn't work on Angular 7. Trying with Firebase Cloud Functions to render on server.
functions\node_modules\ng-circle-progress\index.js:1
(function (exports, require, module, __filename, __dirname) { import { Component, ElementRef, EventEmitter, Inject, Input, NgModule, Output } from '@angular/core';
^
SyntaxError: Unexpected token {
Currently you have to set up custom webpack and whitelist these problematic modules, like so:
webpack custom
const nodeExternals = require('webpack-node-externals');
config.externals = nodeExternals({
whitelist: [
/^ng-circle-progress/,
/^ng2-tel-input/
]
});
A good example is here.
Hi all, this issue should be solved in version 8, and in version 9 bundleDependencies will be turned on by default.
The new 8.x versions of @nguniversal/express-engine schematics creates a setup which avoid this issue. If you are upgrading to 8.x please follow https://github.com/angular/universal/blob/master/docs/v8-upgrade-guide.md
You can also use the next version for Ivy and version 9 by using ng add @nguniversal/express-engine --next.
If the problem persists after upgrading, please open a new issue, provide a simple repository reproducing the problem, and describe the difference between the expected and current behavior.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.