canvas-capture icon indicating copy to clipboard operation
canvas-capture copied to clipboard

ReferenceError self is not defined : Can this library work with React/Next applications ?

Open kishimisu opened this issue 2 years ago • 6 comments

I'm trying to use this library in a Next/Vercel project however I get the following error :

ReferenceError: self is not defined
    at eval (webpack-internal:///./libs/canvas-capture.js:5:4)

which seems to indicate that the library doesn't have the window context. Not sure if this is more of a Next setup issue or if the library should work with these frameworks ?

kishimisu avatar Sep 07 '22 01:09 kishimisu

Seems like this could be relevant:

  • https://stackoverflow.com/questions/64639839/typescript-webpack-library-generates-referenceerror-self-is-not-defined
  • https://webpack.js.org/configuration/output/#outputglobalobject

I made a branch with this change, try:

npm install git+https://github.com/amandaghassaei/canvas-capture.git#globalObject

let me know if that works and I'll merge it in.

amandaghassaei avatar Sep 13 '22 00:09 amandaghassaei

@amandaghassaei it worked, thanks. But now there's a different error - ReferenceError: window is not defined i'm trying to execute this on node, so i suppose it's not built for node usage 😢

roygav avatar Oct 19 '22 12:10 roygav

@amandaghassaei Sorry I didn't see your update. I've tested the branch on a new NextJS App and I get the same error as @roygav : ReferenceError: window is not defined

kishimisu avatar Oct 19 '22 13:10 kishimisu

@amandaghassaei @roygav roy @indiana-dev in I got this error too : ReferenceError: window is not defined Any update fix it? Please help me

nguyenvanan18031993 avatar Nov 01 '22 10:11 nguyenvanan18031993

I looked into this a bit - the underlying dependencies in CCapture do not play nicely with the node environment, so unfortunately I think this is not supported right now. Eventually I'd like to redo this whole repo from the ground up and get rid of the dependency on CCapture, but not in the cards anytime soon.

amandaghassaei avatar Nov 07 '22 21:11 amandaghassaei

@indiana-dev @amandaghassaei I could make it work in nextjs by using dynamic import inside an async function:

async function capture(canvas){
    const CanvasCapture = (await import('canvas-capture')).default;
    CanvasCapture.init( canvas );
    ....
}

This way, we can make sure, that this npm package is not loaded on the server but only on client-side where window is available

kevmiota avatar Nov 11 '22 12:11 kevmiota