major697
major697

Reputation: 1862

install peer.js in vite with Vue 3 Typescript

I'm using Vue 3 Typescript with Vite.js I want to install library peer.js. When I try using Peer:

import Peer from 'peerjs'
const myPeer = new Peer()

then Vite return error:

ReferenceError: parcelRequire is not defined

How I can install Peer.js in Vite?

Upvotes: 0

Views: 739

Answers (2)

Héctor BlisS
Héctor BlisS

Reputation: 135

You can externalize it:


import { reactRouter } from "@react-router/dev/vite";
import autoprefixer from "autoprefixer";
import tailwindcss from "tailwindcss";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";

export default defineConfig({
  build: { rollupOptions: { external: ["peerjs"] } },
  server: { port: 3000 },
  css: {
    postcss: {
      plugins: [tailwindcss, autoprefixer],
    },
  },
  plugins: [reactRouter(), tsconfigPaths()],
});

Example code (not related to vue), just for context. This is what I usually do: build: { rollupOptions: { external: ["peerjs"] } }

Docs: https://vite.dev/guide/build#library-mode

Upvotes: 0

major697
major697

Reputation: 1862

I don't know it's correct answer, but I added:

<script>
    var parcelRequire;
</script>

in index.html file

'parcelRequire is not defined' When used with snowpack. (Github)

Upvotes: 0

Related Questions