Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Svelte - ReferenceError: buffer is not defined

Tags:

webrtc

svelte

Trying to setup a WebRtc connection in my new Svelte app, and am hitting this error whenever I try to import a library.

ReferenceError: buffer is not defined

Example Code: https://github.com/nickgrealy/svelte-webrtc (link also below)

Here are the steps to reproduce: install the library, then import it on a *.svelte component.

e.g. peerjs

npm i peerjs
import Peer from "peerjs";

also occurs for this library - simple-peer

npm i simple-peer
import Peer from "simple-peer";

Both give the following error:

Uncaught ReferenceError: buffer is not defined
    at main.ts:14

Not really sure what to try next... writing vanilla Javascript? Try other libraries?

I don't know how to configure rollup... (hoping it's something simple that one of you have come across before!).

N.B. not sure if relevant, I converted the Svelte project to Typescript (using the inbuilt script).

** Added code to reproduce **

https://github.com/nickgrealy/svelte-webrtc/blob/main/src/main.ts#L2

Console logs from yarn dev

yarn run v1.22.10
$ rollup -c -w
rollup v2.56.3
bundles src/main.ts → public/build/bundle.js...
(!) Missing shims for Node.js built-ins
Creating a browser bundle that depends on "buffer" and "events". You might need to include https://github.com/snowpackjs/rollup-plugin-polyfill-node
LiveReload enabled
(!) Plugin node-resolve: preferring built-in module 'buffer' over local alternative at '/Users/userx/svelte-webrtc/node_modules/buffer/index.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
(!) Circular dependencies
node_modules/readable-stream/lib/_stream_readable.js -> node_modules/readable-stream/lib/_stream_duplex.js -> node_modules/readable-stream/lib/_stream_readable.js
node_modules/readable-stream/lib/_stream_duplex.js -> node_modules/readable-stream/lib/_stream_writable.js -> node_modules/readable-stream/lib/_stream_duplex.js
node_modules/readable-stream/lib/_stream_duplex.js -> node_modules/readable-stream/lib/_stream_writable.js -> /Users/userx/svelte-webrtc/node_modules/readable-stream/lib/_stream_duplex.js?commonjs-proxy -> node_modules/readable-stream/lib/_stream_duplex.js
node_modules/readable-stream/lib/_stream_readable.js -> node_modules/readable-stream/lib/_stream_duplex.js -> /Users/userx/svelte-webrtc/node_modules/readable-stream/lib/_stream_readable.js?commonjs-proxy -> node_modules/readable-stream/lib/_stream_readable.js
(!) Missing global variable names
Use output.globals to specify browser global variable names corresponding to external modules
buffer (guessing 'buffer')
events (guessing 'require$$0$1')
created public/build/bundle.js in 2.9s

[2021-09-05 00:31:10] waiting for changes...

> [email protected] start
> sirv public --no-clear "--dev"


  Your application is ready~! šŸš€

  - Local:      http://localhost:5000
  - Network:    Add `--host` to expose

────────────────── LOGS ──────────────────

  [00:31:18] 200 ─ 5.50ms ─ /
  [00:31:18] 200 ─ 0.60ms ─ /global.css
  [00:31:18] 200 ─ 0.71ms ─ /build/bundle.css
  [00:31:18] 200 ─ 1.35ms ─ /build/bundle.js
  [00:31:22] 200 ─ 0.76ms ─ /global.css
  [00:31:22] 200 ─ 1.83ms ─ /build/bundle.css
  [00:31:22] 200 ─ 3.96ms ─ /build/bundle.js.map

** Update **

Added the node plugin, now getting this error...

Uncaught ReferenceError: require$$1$1 is not defined

... with these logs.

yarn run v1.22.4
$ rollup -c -w
rollup v2.56.3
bundles src/main.ts → public/build/bundle.js...
(!) Missing shims for Node.js built-ins
Creating a browser bundle that depends on "buffer". You might need to include https://github.com/snowpackjs/rollup-plugin-polyfill-node
LiveReload enabled
(!) Circular dependency
polyfill-node.global.js -> polyfill-node.global.js
(!) Missing global variable name
Use output.globals to specify browser global variable names corresponding to external modules
buffer (guessing 'require$$1$1')
created public/build/bundle.js in 2.6s

[2021-09-05 00:57:04] waiting for changes...
npm WARN lifecycle The node binary used for scripts is /var/folders/05/qnr367194ss7ktgg_c2r57440000gp/T/yarn--1630767421122-0.4620373266212361/node but npm is using /Users/userx/.nvm/versions/node/v14.17.3/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.

> [email protected] start /Users/userx/svelte-webrtc
> sirv public --no-clear "--dev"


  Your application is ready~! šŸš€
like image 590
Nick Grealy Avatar asked Aug 27 '26 02:08

Nick Grealy


1 Answers

When using peerjs browserify is not required and change output format to cjs in rollup.config.js.

If you want to keep iife then you need to disable strict mode in rollup.config.js under output property strict: false because the library has some issue.

like image 83
Chandan Avatar answered Aug 29 '26 11:08

Chandan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!