plyr icon indicating copy to clipboard operation
plyr copied to clipboard

Error importing plyr.min.mjs in Node.js

Open seifertm opened this issue 5 years ago • 2 comments

The Plyr module cannot be imported in Node.js, because it depends on document. This is an issue for server-side rendering used by some static site generators (see downstream bug) and is a regression of #935 .

I believe the issue is related to utils/browser.js. When SSR worked in v3.3.8, the browser discovery was performed at runtime, when calling getBrowser. In recent versions, the browser type is evaluated at import time and makes use of document and ẁindow.

Steps to reproduce

Using plyr-3.6.2:

  • Create an app.mjs with an import statement for plyr.min.mjs:
import 'plyr/dist/plyr.min.mjs';
  • Run the file:
$ node app.mjs

Expected behaviour

Plyr can be imported successfully

Actual behaviour

Node exits with the following error:

ReferenceError: document is not defined
    at file:///tmp/plyr-test/node_modules/plyr/dist/plyr.min.mjs:1:10106
    at file:///tmp/plyr-test/node_modules/plyr/dist/plyr.min.mjs:1:10375
    at ModuleJob.run (internal/modules/esm/module_job.js:138:23)
    at async Loader.import (internal/modules/esm/loader.js:178:24)

Environment

  • Browser: Node.js
  • Version: 14.4.0
  • Operating System: Linux

seifertm avatar Sep 04 '20 09:09 seifertm

Assuming you are doing this with react and webpack, as a workaround you can require instead of import the library in a useEffect which makes sure the plyr code only runs on the browser.

useEffect(() => {
  const Plyr = require('plyr');
  new Plyr(videoRef.current, options);    
}, []) 

erikt9 avatar Aug 31 '21 18:08 erikt9

This occurs too with vite. When will this be fixed?

ThatOneCalculator avatar Jul 14 '22 04:07 ThatOneCalculator