Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught ReferenceError: window is not defined #190

Closed
wanliyunyan opened this issue Apr 17, 2018 · 12 comments
Closed

Uncaught ReferenceError: window is not defined #190

wanliyunyan opened this issue Apr 17, 2018 · 12 comments
Assignees
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@wanliyunyan
Copy link

wanliyunyan commented Apr 17, 2018

in console

./node_modules/_base64-js@1.2.3@base64-js/index.js.exports.byteLength @ bootstrap:1
(anonymous)                                                                                                   @ bootstrap:758

in webpack/bootstrap : 1
var parentHotUpdateCallback = window["webpackHotUpdate"];  // window is not defined

in webpack/bootstrap : 758
return hotCreateRequire("./node_modules/_pdfjs-dist@2.0.305@pdfjs-dist/build/pdf.worker.js")(__webpack_require__.s = "./node_modules/_pdfjs-dist@2.0.305@pdfjs-dist/build/pdf.worker.js");

my package

 "webpack": "^4.5.0",

how to solve this problem?????

@wojtekmaj wojtekmaj self-assigned this Apr 17, 2018
@wojtekmaj wojtekmaj added the question Further information is requested label Apr 17, 2018
@wojtekmaj
Copy link
Owner

Hello,
Webpack 4 support is not yet announced as critical external dependency uses Webpack 3 internally. Therefore I'm not sure what's going on there.
What I can suggest you is following instructions for non Webpack environments instead (see README).

@wanliyunyan
Copy link
Author

@wojtekmaj thanks for your answer,I close this issue

@wojtekmaj
Copy link
Owner

Update: React-PDF 4.0 comes with support for Webpack 4.0.

Here's a solution by @iki:

Also, if you get Uncaught ReferenceError: window is not defined. from pdf.js worker when running local webpack dev server with hot module reloading, then add globalObject: 'this' in the output section of webpack config, see webpack/webpack#6642 (comment).

@Ivaneyko
Copy link

Ivaneyko commented May 31, 2019

@wojtekmaj
This is no good solution
I think that most developers use to configure the application create-react-app. In this way, webpack.config is closed and you can't edit it

@mrarmyant
Copy link

mrarmyant commented Jun 4, 2019

@wojtekmaj I'm using webpack 4 and the latest version of this package and am getting this issue. Has anyone found a good fix? I'm using create react-app. If I import the package directly it doesnt find anything, if I import from "react-pdf/dist/entry.webpack" I get it to say PDF Loading and Page 1 of ____ with that error in the console. This is with a create-react-app.

@wojtekmaj wojtekmaj added the help wanted Extra attention is needed label Jun 6, 2019
@1uokun
Copy link

1uokun commented Jun 7, 2019

same issue and need help🚑

import pdfjs from 'pdfjs-dist/webpack';

pdfjs.getDocument(url).then(pdfDocument => {
  //...
})

@Marlendo
Copy link

Marlendo commented Jun 17, 2019

change node_modules\react-scripts\config\webpack.config.js
add this line from

output: {
++   globalObject: 'this'
}

example:

output: {
      globalObject: 'this', 
      // The build folder.
      path: isEnvProduction ? paths.appBuild : undefined,
      // Add /* filename */ comments to generated require()s in the output.
      pathinfo: isEnvDevelopment,
      // There will be one main bundle, and one file per asynchronous chunk.
      // In development, it does not produce real files.
      filename: isEnvProduction
        ? 'static/js/[name].[contenthash:8].js'
        : isEnvDevelopment && 'static/js/bundle.js',
      // TODO: remove this when upgrading to webpack 5
      futureEmitAssets: true,
      // There are also additional JS chunk files if you use code splitting.
      chunkFilename: isEnvProduction
        ? 'static/js/[name].[contenthash:8].chunk.js'
        : isEnvDevelopment && 'static/js/[name].chunk.js',
      // We inferred the "public path" (such as / or /my-project) from homepage.
      // We use "/" in development.
      publicPath: publicPath,
      // Point sourcemap entries to original disk location (format as URL on Windows)
      devtoolModuleFilenameTemplate: isEnvProduction
        ? info =>
          path
            .relative(paths.appSrc, info.absoluteResourcePath)
            .replace(/\\/g, '/')
        : isEnvDevelopment &&
        (info => path.resolve(info.absoluteResourcePath).replace(/\\/g, '/')),
    },

@jasonrberney
Copy link

I am having the same issue.

Is the above a long term solution?

I don't have a react-scripts folder in my node_modules. Wondering where this folder came from?

Environment:
"react": "^16.6.0",
"webpack": "^4.12.0",
"webpack-dev-server": "^3.1.4",
"@babel/core": "^7.0.0",
"react-pdf": "^4.1.0"

@wojtekmaj
Copy link
Owner

Yeah, suggested solution is globalObject: this. #190 (comment)

@Rahul-Sagore
Copy link

Rahul-Sagore commented Jul 7, 2020

Is there a solution for for Create-React-App project. I am facing similar issue

Uncaught ReferenceError: window is not defined
    at bootstrap:1
    at bootstrap:788

@wojtekmaj
Copy link
Owner

That should work in Create-React-App, unless it's very outdated, since they did exactly what's suggested as a solution, almost a year ago:

https://github.com/facebook/create-react-app/pull/7742/files

@nicholasbca
Copy link

change node_modules\react-scripts\config\webpack.config.js
add this line from

output: {
++   globalObject: 'this'
}

example:

output: {
      globalObject: 'this', 
      // The build folder.
      path: isEnvProduction ? paths.appBuild : undefined,
      // Add /* filename */ comments to generated require()s in the output.
      pathinfo: isEnvDevelopment,
      // There will be one main bundle, and one file per asynchronous chunk.
      // In development, it does not produce real files.
      filename: isEnvProduction
        ? 'static/js/[name].[contenthash:8].js'
        : isEnvDevelopment && 'static/js/bundle.js',
      // TODO: remove this when upgrading to webpack 5
      futureEmitAssets: true,
      // There are also additional JS chunk files if you use code splitting.
      chunkFilename: isEnvProduction
        ? 'static/js/[name].[contenthash:8].chunk.js'
        : isEnvDevelopment && 'static/js/[name].chunk.js',
      // We inferred the "public path" (such as / or /my-project) from homepage.
      // We use "/" in development.
      publicPath: publicPath,
      // Point sourcemap entries to original disk location (format as URL on Windows)
      devtoolModuleFilenameTemplate: isEnvProduction
        ? info =>
          path
            .relative(paths.appSrc, info.absoluteResourcePath)
            .replace(/\\/g, '/')
        : isEnvDevelopment &&
        (info => path.resolve(info.absoluteResourcePath).replace(/\\/g, '/')),
    },

thank you, this solution work for me

Nebulis added a commit to Open-Attestation/decentralized-renderer-react-components that referenced this issue Aug 27, 2020
Build failure happens on testcafe `https://app.circleci.com/pipelines/github/Open-Attestation/decentralized-renderer-react-template/30/workflows/327edc02-78e4-438a-bf67-b202b19b12c4/jobs/31` with the error `window is not defined`

According to wojtekmaj/react-pdf#190 it could be solve by adding the option `globalObject: "this"` to webpack. This was also added into create-react-app.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

9 participants