0

I'm using Angular 4 +webpack.I've added a jQuery plugin to nonTreeShakableModules const in webpack.config.vendor.js:

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const merge = require('webpack-merge');

const treeShakableModules = [
    '@angular/animations',
    '@angular/common',
    '@angular/compiler',
    '@angular/core',
    '@angular/forms',
    '@angular/http',
    '@angular/platform-browser',
    '@angular/platform-browser-dynamic',
    '@angular/router',
    'font-awesome/css/font-awesome.css',
    'zone.js',

];
const nonTreeShakableModules = [
    'bootstrap',
    'bootstrap/dist/css/bootstrap.css',
    'es6-promise',
    'es6-shim',
    'event-source-polyfill',
    'jquery',
    'virtual-keyboard'              //HERE
];

When starting the application I got this error:

NodeInvocationException: Prerendering failed because of error: Error: jQuery requires a window with a document

If I refresh the page for 2-3 times,error is gone. Thanks for any help!

2
  • 1
    What are you trying to achieve? Do you want to be able to use jquery in your application? Commented Oct 30, 2017 at 12:28
  • Hi.I want to use Mottie Virtual keryboard jquey plugin in an Angular 4 +webapck application.Jquery is incuded and seems to be working.thanks Commented Oct 30, 2017 at 12:33

1 Answer 1

2

As said in the comments of your previous question, you can't run javascript code which depends on window and some other cases (like session storage) on the server-side with server-sided pre-rendering.

Templates such as ASP.NET Core Angular Web templates comes with server-sided rendering enabled. This works fine for applications which doesn't require session storage, authentication or access to browser components or dom-tree.

You have to disable server-sided prerendering by removing the asp-prerender-module="ClientApp/dist/app.module.server.ts" tag helper from your Index.cshtml.

Replace

<my-app asp-prerender-module="ClientApp/dist/app.module.server.ts"></my-app>

with

<my-app></my-app>

Of course replace my-app with the selector of your application, typically app in templates.

Alternatively you have to run code conditionally, as pointed in this GitHub issue:

// boot-client.ts file 
import 'ngx-charts';

// some.component.ts
import { isBrowser } from 'angular2-universal';
import * as $ from 'jquery';

// inside ngOnInit 
if (isBrowser) { 
    $('body').hide();  // or whatever call you need to make
}

to avoid running such code on the server-sided pre-rendering.

Sign up to request clarification or add additional context in comments.

1 Comment

for the moment the error has gone.i'll keep testing.thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.