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.

react-pdf is not compatible without shims

See original GitHub issue

Describe the bug

When trying to use React PDF in a Vite React project, we get:

Uncaught ReferenceError: global is not defined
    at node_modules/blob/index.js (index.js:5)
    at __require (chunk-KVFJW2XH.js?v=ce5c473e:12)
    at node_modules/blob-stream/index.js (index.js:3)
    at __require (chunk-KVFJW2XH.js?v=ce5c473e:12)
    at asyncToGenerator.js:32

It’s from the blob NPM library, but what’s really strange is the code in .vite/@react-pdf_renderer.js on line 554 is:

var BlobBuilder = global.BlobBuilder || global.WebKitBlobBuilder || global.MSBlobBuilder || global.MozBlobBuilder;

But the module definitely does not get packaged with references to global: https://unpkg.com/blob@0.1.0/main.js

We’d like to understand where the global is coming from, then be able to fix and/or work around this so that we can use React PDF in our project.

Also noteworthy is that when we try to use Document, Page, or other exports that the TypeScript types say should be there, we get:

Uncaught SyntaxError: The requested module '/node_modules/.vite/@react-pdf_renderer.js?v=ce5c473e' does not provide an export named 'Document'

It really makes me wonder if we’re somehow selecting a server version of React PDF, but I can’t see how or where that’s happening in the monorepo imports that are going on.

Reproduction

https://github.com/thekevinbrown/vite-react-pdf-reproduction

System Info

Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers:

  System:
    OS: macOS 11.2.3
    CPU: (8) arm64 Apple M1
    Memory: 541.69 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.11.0 - ~/.nvm/versions/node/v15.11.0/bin/node
    Yarn: 1.22.5 - ~/.yarn/bin/yarn
    npm: 7.6.0 - ~/.nvm/versions/node/v15.11.0/bin/npm
  Browsers:
    Brave Browser: 90.1.23.75
    Chrome: 90.0.4430.212
    Firefox: 88.0
    Safari: 14.0.3
  npmPackages:
    vite: ^2.3.0 => 2.3.2 

Used package manager: yarn

Logs

$ vite --debug
  vite:config bundled config file loaded in 33ms +0ms
  vite:config using resolved config: {
  vite:config   plugins: [
  vite:config     'vite:pre-alias',
  vite:config     'alias',
  vite:config     'react-refresh',
  vite:config     'vite:dynamic-import-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:client-inject',
  vite:config     'vite:import-analysis'
  vite:config   ],
  vite:config   server: {
  vite:config     fsServe: {
  vite:config       root: '/Users/kevin/development/vite-react-pdf-reproduction',
  vite:config       strict: false
  vite:config     }
  vite:config   },
  vite:config   configFile: '/Users/kevin/development/vite-react-pdf-reproduction/vite.config.ts',
  vite:config   configFileDependencies: [ 'vite.config.ts' ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     server: { fsServe: [Object] }
  vite:config   },
  vite:config   root: '/Users/kevin/development/vite-react-pdf-reproduction',
  vite:config   base: '/',
  vite:config   resolve: { dedupe: undefined, alias: [ [Object] ] },
  vite:config   publicDir: '/Users/kevin/development/vite-react-pdf-reproduction/public',
  vite:config   cacheDir: '/Users/kevin/development/vite-react-pdf-reproduction/node_modules/.vite',
  vite:config   command: 'serve',
  vite:config   mode: 'development',
  vite:config   isProduction: false,
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {},
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     minify: 'terser',
  vite:config     terserOptions: {},
  vite:config     cleanCssOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     brotliSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null
  vite:config   },
  vite:config   env: { BASE_URL: '/', MODE: 'development', DEV: true, PROD: false },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen]
  vite:config   },
  vite:config   createResolver: [Function: createResolver],
  vite:config   optimizeDeps: { esbuildOptions: { keepNames: undefined } }
  vite:config } +2ms
  vite:deps Hash is consistent. Skipping. Use --force to override. +0ms

  vite v2.3.2 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

  ready in 139ms.

  vite:time 1ms   / +0ms
  vite:spa-fallback Rewriting GET / to /index.html +0ms
  vite:time 19ms  /index.html +36ms
  vite:resolve 1ms   /Users/kevin/development/vite-react-pdf-reproduction/node_modules/vite/dist/client/client -> /Users/kevin/development/vite-react-pdf-reproduction/node_modules/vite/dist/client/client.js +0ms
  vite:resolve 2ms   /@vite/client -> /Users/kevin/development/vite-react-pdf-reproduction/node_modules/vite/dist/client/client.js +0ms
  vite:resolve 0ms   /src/main.tsx -> /Users/kevin/development/vite-react-pdf-reproduction/src/main.tsx +2ms
  vite:load 2ms   [fs] /@vite/client +0ms
  vite:resolve 1ms   ./env -> /Users/kevin/development/vite-react-pdf-reproduction/node_modules/vite/dist/client/env.js +9ms
  vite:resolve 0ms   /node_modules/vite/dist/client/env.js -> /Users/kevin/development/vite-react-pdf-reproduction/node_modules/vite/dist/client/env.js +0ms
  vite:transform 7ms   /@vite/client +0ms
  vite:time 14ms  /@vite/client +45ms
  vite:load 10ms  [fs] /src/main.tsx +9ms
  vite:load 0ms   [plugin] /@react-refresh +140ms
  vite:rewrite 1ms   [no imports] /@react-refresh +0ms
  vite:transform 1ms   /@react-refresh +141ms
  vite:time 1ms   /@react-refresh +141ms
  vite:resolve 0ms   react -> /Users/kevin/development/vite-react-pdf-reproduction/node_modules/.vite/react.js?v=ce5c473e&es-interop +146ms
  vite:resolve 0ms   /node_modules/.vite/react.js?v=ce5c473e&es-interop -> /Users/kevin/development/vite-react-pdf-reproduction/node_modules/.vite/react.js?v=ce5c473e&es-interop +0ms
  vite:resolve 0ms   react-dom -> /Users/kevin/development/vite-react-pdf-reproduction/node_modules/.vite/react-dom.js?v=ce5c473e&es-interop +2ms
  vite:resolve 0ms   /node_modules/.vite/react-dom.js?v=ce5c473e&es-interop -> /Users/kevin/development/vite-react-pdf-reproduction/node_modules/.vite/react-dom.js?v=ce5c473e&es-interop +0ms
  vite:resolve 0ms   ./App -> /Users/kevin/development/vite-react-pdf-reproduction/src/App.tsx +0ms
  vite:resolve 0ms   /src/App.tsx -> /Users/kevin/development/vite-react-pdf-reproduction/src/App.tsx +0ms
  vite:resolve 0ms   /node_modules/.vite/react.js?v=ce5c473e -> /Users/kevin/development/vite-react-pdf-reproduction/node_modules/.vite/react.js?v=ce5c473e +0ms
  vite:resolve 0ms   /node_modules/.vite/react-dom.js?v=ce5c473e -> /Users/kevin/development/vite-react-pdf-reproduction/node_modules/.vite/react-dom.js?v=ce5c473e +0ms
  vite:transform 147ms /src/main.tsx +6ms
  vite:time 158ms /src/main.tsx +7ms
  vite:load 0ms   [fs] /node_modules/vite/dist/client/env.js +8ms
  vite:rewrite 0ms   [no imports] node_modules/vite/dist/client/env.js +8ms
  vite:transform 0ms   /node_modules/vite/dist/client/env.js +2ms
  vite:time 1ms   /node_modules/vite/dist/client/env.js +1ms
  vite:load 1ms   [fs] /src/App.tsx +8ms
  vite:resolve 0ms   @react-pdf/renderer -> /Users/kevin/development/vite-react-pdf-reproduction/node_modules/.vite/@react-pdf_renderer.js?v=ce5c473e +16ms
  vite:resolve 0ms   /node_modules/.vite/@react-pdf_renderer.js?v=ce5c473e -> /Users/kevin/development/vite-react-pdf-reproduction/node_modules/.vite/@react-pdf_renderer.js?v=ce5c473e +0ms
  vite:hmr [self-accepts] src/App.tsx +0ms
  vite:transform 8ms   /src/App.tsx +15ms
  vite:time 10ms  /src/App.tsx +16ms
  vite:time 2ms   /src/favicon.svg +163ms
  vite:spa-fallback Rewriting GET / to /index.html +7s
  vite:time 4ms   /index.html +6s
  vite:cache [304] /@vite/client +0ms
  vite:time 2ms   /@vite/client +38ms
  vite:cache [304] /src/main.tsx +1ms
  vite:time 1ms   /src/main.tsx +2ms
  vite:cache [304] /@react-refresh +15ms
  vite:time 0ms   /@react-refresh +14ms
  vite:cache [304] /node_modules/vite/dist/client/env.js +2ms
  vite:time 1ms   /node_modules/vite/dist/client/env.js +2ms
  vite:cache [304] /src/App.tsx +3ms
  vite:time 1ms   /src/App.tsx +3ms
  vite:time 1ms   /src/favicon.svg +161ms

Before submitting the issue, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the docs.
  • Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
  • Provide a description in this issue that describes the bug.
  • Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
  • Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
Cslovecommented, Jun 6, 2021

@thekevinbrown thank you so much!! meet same problem. and your vite plugin solved my issue! by the way, it should yarn add vite-plugin-shim-react-pdf -D, thanks again!

2reactions
thekevinbrowncommented, Jun 7, 2021

Alrighty, finally found a set of shims and workarounds that make it work, both in Vite Dev and Production builds.

So, if you’re coming here from Google, you do the following in your Vite project:

$ yarn add --dev vite-plugin-shim-react-pdf

Then in vite.config.js:

import { defineConfig } from "vite";
import reactRefresh from "@vitejs/plugin-react-refresh";
import shimReactPdf from "vite-plugin-shim-react-pdf";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [reactRefresh(), shimReactPdf()],
});

The you’ll also need to use the default import because of the missing exports. E.g.

Instead of:

import { Text, View, StyleSheet } from "@react-pdf/renderer";

You have to do:

import pdf from "@react-pdf/renderer";
const { Text, View, StyleSheet } = pdf;

React PDF also didn’t have a development project you could use to test changes, so I made a really rough one out of the examples: https://github.com/TechInSite/react-pdf-development-harness

Read more comments on GitHub >

github_iconTop Results From Across the Web

vite-plugin-shim-react-pdf
This shim is no longer necessary. Upgrade to @react-pdf/renderer >= 3.0.0 for native Vite / Rollup compatibility in React PDF itself.
Read more >
React PDF + Vite implementation Error: Uncaught ...
I fixed this import error with this, adding a file "reactPdf.js" like follows to my project ... yarn add --dev vite-plugin-shim-react-pdf.
Read more >
vite-plugin-shim-react-pdf - npm package
The npm package vite-plugin-shim-react-pdf was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed as ......
Read more >
vite-plugin-shim-react-pdf: Docs, Community, Tutorials
This shim is no longer necessary. Upgrade to @react-pdf/renderer >= 3.0.0 for native Vite / Rollup compatibility in React PDF itself.
Read more >
Support Internet Explorer 11
... Internet Explorer 11 because it will not be supported by Microsoft on June 15, 2022 ... .com/ajax/libs/es6-shim/0.35.3/es6-shim.min.js"></script>.
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