react-pdf is not compatible without shims
See original GitHub issueDescribe 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:
- Created 2 years ago
- Comments:11 (4 by maintainers)
@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!Alrighty, finally found a set of shims and workarounds that make it work, both in Vite Dev and Production builds.
node_modules
. Yuck.)readable-stream
which is needed for Node streams in the browser works fine in dev mode, but in a production build it fails because of circular imports which Rollup doesn’t handle particularly well, but also probably shouldn’t be there in the first place in ESM land (https://github.com/rollup/rollup-plugin-commonjs/issues/293, https://github.com/nodejs/readable-stream/issues/348). I changed the package to use a Registry pattern so it’d work (https://github.com/techinsite/readable-stream) and published it as vite-compatible-readable-stream.node_modules
I bundled that up into vite-plugin-shim-react-pdf.So, if you’re coming here from Google, you do the following in your Vite project:
Then in
vite.config.js
:The you’ll also need to use the default import because of the missing exports. E.g.
Instead of:
You have to do:
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