question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: window is not defined

See original GitHub issue

When trying to run the SSR on my project I get the following error:

Failed to render.
ReferenceError: window is not defined
    at Object. (/Users/teixefel/gitDiconium/gce-shop/packages/elli/node_modules/@feature-hub/module-loader-commonjs/lib/cjs/.cache/9dc47ddd75f4c5eb2d11b2131a345910.js:10:4)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at requireAsync (/Users/teixefel/gitDiconium/gce-shop/packages/elli/node_modules/@feature-hub/module-loader-commonjs/lib/cjs/index.js:31:12)

Dependencies:

"@feature-hub/async-ssr-manager": "^0.13.0",
    "@feature-hub/core": "^0.13.0",
    "@feature-hub/history-service": "^0.13.0",
    "@feature-hub/module-loader-amd": "^0.13.0",
    "@feature-hub/module-loader-commonjs": "^0.13.0",
    "@feature-hub/react": "^0.13.0",
    "@feature-hub/serialized-state-manager": "^0.13.0",
    "@feature-hub/server-request": "^0.13.0",

feature-app-integrator-node.js

const { asyncSsrManagerDefinition } = require('@feature-hub/async-ssr-manager');
const {
  ExternalsValidator,
  FeatureAppManager,
  FeatureServiceRegistry,
} = require('@feature-hub/core');
const { loadCommonJsModule } = require('@feature-hub/module-loader-commonjs');
const { FeatureHubContextProvider } = require('@feature-hub/react');
const { serializedStateManagerDefinition } = require('@feature-hub/serialized-state-manager');
const React = require('react');
const ReactDOM = require('react-dom/server');
const App = require('../feature-hub/feature-app-loader');

module.exports = async function renderApp({ port }) {
  const integratorDefinition = {
    id: 'test:integrator',
    dependencies: {
      featureServices: {
        [asyncSsrManagerDefinition.id]: '^0.1.0',
        [serializedStateManagerDefinition.id]: '^0.1.0',
      },
    },
  };

  const externalsValidator = new ExternalsValidator({
    react: '16.7.0',
  });

  const featureServiceRegistry = new FeatureServiceRegistry({
    externalsValidator,
  });

  featureServiceRegistry.registerFeatureServices(
    [asyncSsrManagerDefinition, serializedStateManagerDefinition],
    integratorDefinition.id,
  );

  const { featureServices } = featureServiceRegistry.bindFeatureServices(
    integratorDefinition,
  );

  const asyncSsrManager = featureServices[
    asyncSsrManagerDefinition.id
  ];

  const featureAppManager = new FeatureAppManager(featureServiceRegistry, {
    moduleLoader: loadCommonJsModule,
    externalsValidator,
  });

  const urlsForHydration = [];

  const featureHubContextValue = {
    featureAppManager,
    asyncSsrManager,

    addUrlForHydration(url) {
      urlsForHydration.push(url);
    },
  };

  const html = await asyncSsrManager
    .renderUntilCompleted(() => {
      const reactElement = React.createElement(FeatureHubContextProvider, {
        value: featureHubContextValue,
      }, React.createElement(App, {
        port,
      }));

      const elementStr = ReactDOM.renderToString(reactElement);

      return elementStr;
    });

  const serializedStateManager = featureServices[
    serializedStateManagerDefinition.id
  ];

  const serializedStates = serializedStateManager.serializeStates();

  return { html, serializedStates, urlsForHydration };
};

feature-app-loader.js

const { FeatureAppLoader } = require('@feature-hub/react');
const propTypes = require('prop-types');
const React = require('react');

function App({ port }) {
  return (
    React.createElement(FeatureAppLoader, {
      src: 'app.js',
      serverSrc: port ? 'http://localhost:'.concat(port, '/app.js') : '',
    })
  );
}

App.propTypes = {
  port: propTypes.number,
};

App.defaultProps = {
  port: 3009,
};

module.exports = App;

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
unstubbablecommented, Feb 11, 2019

You cannot use the same Feature App bundle (app.js) for server and client. They can have the same source code and use the same entry for both server and client bundle, but the former needs to target node and use CommonJS as library target, whereas the latter needs to target the browser with UMD as library target. See also https://github.com/sinnerschrader/feature-hub/blob/master/packages/demos/src/server-side-rendering/webpack-config.ts

1reaction
clebertcommented, Feb 11, 2019

It looks to me like your Feature App is not SSR capable, or requires a browser environment to run. Is it possible that you provide us with a minimal example? Your provided code looks OK at first glance. The bug will probably be in your Feature App or one of its dependencies.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to solve "window is not defined" errors in React and Next.js
First solution: typeof​​ Because typeof won't try to evaluate "window", it will only try to get its type, in our case in Node....
Read more >
Javascript: 'window' is not defined - Stack Overflow
The window object represents an open window in a browser. Since you are not running your code within a browser, but via Windows...
Read more >
How to solve Next.js window is not defined
ReferenceError : window is not defined is a pretty common error you may run into when using Next.js for the first time but...
Read more >
How To Solve ReferenceError window is not defined in ...
Fixing a window is not defined error can be quite simple. In most cases, all you will need to do is wrap your...
Read more >
[Solved] ReferenceError : window is not defined - ItsJavaScript
The ReferenceError : window is not defined error mainly occurs if you are using the window object in Node.js, React.js, Next.js.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found