ffmpeg.wasm icon indicating copy to clipboard operation
ffmpeg.wasm copied to clipboard

Uncaught ReferenceError: createFFmpegCore is not defined

Open ayech0x2 opened this issue 2 years ago • 30 comments

As you can see the

import React from "react";
import { createFFmpeg, fetchFile } from "@ffmpeg/ffmpeg";

const ffmpeg = createFFmpeg({
  log: true,
});

function App() {
  const [ready, setReady] = React.useState(false);

  const load = async () => {
    await ffmpeg.load();
    setReady(true);
  };

  React.useEffect(() => {
    load();
  }, []);

.
.
.

I got a Bad memory error too! How to fix this?

ayech0x2 avatar Oct 01 '21 22:10 ayech0x2

Same issue here.

NayamAmarshe avatar Nov 01 '21 07:11 NayamAmarshe

Same here :(

damechen avatar Nov 02 '21 22:11 damechen

Same here!

pipriles avatar Nov 03 '21 21:11 pipriles

I found a fix! You have to go to node_modules/@ffmpeg/core/dist/ and copy all the files from there and paste them in the root folder, preferrably in the public folder if you're using NextJS. Then use:

const ffmpeg = createFFmpeg({
  corePath: "http://localhost:3000/ffmpeg-core.js",
  // Use public address
  log: true,
});

NayamAmarshe avatar Nov 04 '21 16:11 NayamAmarshe

I am facing the same issue, whether running with or without corePath. None of the above workarounds work for me. I am building a Next.js PWA. Any ideas how we can avoid this and use this supercool module?

shivamragnar avatar Dec 24 '21 06:12 shivamragnar

I am facing the same issue, whether running with or without corePath. None of the above workarounds work for me. I am building a Next.js PWA. Any ideas how we can avoid this and use this supercool module?

It was working when I posted the solution but not anymore :((

NayamAmarshe avatar Dec 24 '21 11:12 NayamAmarshe

Did anyone find a solution for this? I guess downgrading @ffmpeg/ffmpeg to 0.9.8 and @ffmpeg/core to 0.9.0 for now is the way to go 🤷‍♂️

Junoburger avatar Jan 02 '22 01:01 Junoburger

@Junoburger I changed @ffmpeg/ffmpeg to 0.9.8 and @ffmpeg/core to 0.9.0 but I got ReferenceError: __dirname is not defined error :(

KazuyaFCB avatar Jan 02 '22 08:01 KazuyaFCB

Try corePath: "https://unpkg.com/@ffmpeg/[email protected]/dist/ffmpeg-core.js"

CrypticSignal avatar Jan 03 '22 00:01 CrypticSignal

Hello, using this versions in my package json it worked just fine:

"@ffmpeg/core": "^0.8.3",
"@ffmpeg/ffmpeg": "^0.9.4",

martins-a avatar Jan 07 '22 00:01 martins-a

I found a fix! You have to go to node_modules/@ffmpeg/core/dist/ and copy all the files from there and paste them in the root folder, preferrably in the public folder if you're using NextJS. Then use:

const ffmpeg = createFFmpeg({
  corePath: "http://localhost:3000/public/ffmpeg-core.js",
  // Use public address
  log: true,
});

This started to work for me in NextJS when I removed the http://localhost:3000/public part. I copied the node_modules/@ffmpeg/core/dist folder into public and renamed it ffmpeg_core_dist for clarity. Then

import { createFFmpeg, fetchFile } from "@ffmpeg/ffmpeg";
const ffmpeg = createFFmpeg({ log: true, corePath: "/ffmpeg_core_dist/ffmpeg-core.js" });

worked locally. But on production I got the error Uncaught (in promise) ReferenceError: SharedArrayBuffer is not defined which I was able to fix by adding these headers to next.config.js:

async headers() {
    return [
        {
            source: "/", // change to appropriate path
            headers: [
                {
                    key: "Cross-Origin-Embedder-Policy",
                    value: "require-corp",
                },
                {
                    key: "Cross-Origin-Opener-Policy",
                    value: "same-origin",
                },
            ],
        },
    ];
},

But this is not a perfect solution because SharedArrayBuffer is not defined still happens sometimes on page load and a reload is needed to get ffmpeg to load. Also it has other issues mentioned here. This all has to do with the security of SharedArrayBuffer that goes above my head.

discretegames avatar Jan 13 '22 22:01 discretegames

For anyone using react-scripts or create-react-app to run React, you can send headers manually by using a customized middleware (https://create-react-app.dev/docs/proxying-api-requests-in-development/#configuring-the-proxy-manually).

First, install http-proxy-middleware:

npm install http-proxy-middleware --save

or

yarn add http-proxy-middleware

Then, create src/setupProxy.js file and put our logic in there:

module.exports = function (app) {
    app.use(function (request, response, next) {
        response.setHeader("Cross-Origin-Opener-Policy", "same-origin");
        response.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
        next();
    });
};

votruongan avatar Mar 01 '22 05:03 votruongan

 corePath: "http://localhost:3000/ffmpeg-core.js",
``

hello sir, for many days i've been facing the same issue while working with nextjs. did u find the soulution? if u did I would be pleased if u share it with me.

JavokhirbekTukhtasinov avatar May 23 '22 06:05 JavokhirbekTukhtasinov

I had the same problem in React app, those version worked for me @ffmpeg/core: ^0.10.0 and @ffmpeg/ffmpeg: ^ 0.9.8

TheYass1n avatar Jun 04 '22 13:06 TheYass1n

Same problem here with "@ffmpeg/core": "^0.10.0" and "@ffmpeg/ffmpeg": "^0.10.1"

HinnyTsang avatar Jun 11 '22 13:06 HinnyTsang

Same problem here with "@ffmpeg/core": "^0.10.0" and "@ffmpeg/ffmpeg": "^0.10.1"

khantopa avatar Jun 30 '22 07:06 khantopa

I found a fix! You have to go to node_modules/@ffmpeg/core/dist/ and copy all the files from there and paste them in the root folder, preferrably in the public folder if you're using NextJS. Then use:

const ffmpeg = createFFmpeg({
  corePath: "http://localhost:3000/public/ffmpeg-core.js",
  // Use public address
  log: true,
});

This started to work for me in NextJS when I removed the http://localhost:3000/public part. I copied the node_modules/@ffmpeg/core/dist folder into public and renamed it ffmpeg_core_dist for clarity. Then

import { createFFmpeg, fetchFile } from "@ffmpeg/ffmpeg";
const ffmpeg = createFFmpeg({ log: true, corePath: "/ffmpeg_core_dist/ffmpeg-core.js" });

worked locally. But on production I got the error Uncaught (in promise) ReferenceError: SharedArrayBuffer is not defined which I was able to fix by adding these headers to next.config.js:

async headers() {
    return [
        {
            source: "/", // change to appropriate path
            headers: [
                {
                    key: "Cross-Origin-Embedder-Policy",
                    value: "require-corp",
                },
                {
                    key: "Cross-Origin-Opener-Policy",
                    value: "same-origin",
                },
            ],
        },
    ];
},

But this is not a perfect solution because SharedArrayBuffer is not defined still happens sometimes on page load and a reload is needed to get ffmpeg to load. Also it has other issues mentioned here. This all has to do with the security of SharedArrayBuffer that goes above my head.



It worked for me thank you @discretegames !

My setting:

  • Next.js (JavaScript)
  • @ffmpeg/core==0.10.0
  • @ffmpeg/ffmpeg==0.10.1
// index.jsx  (or some component .js/.jsx file)

const ffmpeg = createFFmpeg({
  corePath: 'https://unpkg.com/@ffmpeg/[email protected]/dist/ffmpeg-core.js',
  log: true,
});
// next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  async headers() {
    return [
      {
        source: '/',
        headers: [
          {
            key: 'Cross-Origin-Embedder-Policy',
            value: 'require-corp',
          },
          {
            key: 'Cross-Origin-Opener-Policy',
            value: 'same-origin',
          },
        ],
      },
    ];
  },
};

module.exports = nextConfig;

Thank you again :)

SeiwonPark avatar Jun 30 '22 09:06 SeiwonPark

Hi, I faced the same issues and found that there is an easy to use script for SharedArrayBuffer is not defined error. I added this script https://github.com/gzuidhof/coi-serviceworker to the head and it fixed the issue on Nuxt

katinas15 avatar Jul 07 '22 21:07 katinas15

I found a fix! You have to go to node_modules/@ffmpeg/core/dist/ and copy all the files from there and paste them in the root folder, preferrably in the public folder if you're using NextJS. Then use:

const ffmpeg = createFFmpeg({
  corePath: "http://localhost:3000/public/ffmpeg-core.js",
  // Use public address
  log: true,
});

This started to work for me in NextJS when I removed the http://localhost:3000/public part. I copied the node_modules/@ffmpeg/core/dist folder into public and renamed it ffmpeg_core_dist for clarity. Then

import { createFFmpeg, fetchFile } from "@ffmpeg/ffmpeg";
const ffmpeg = createFFmpeg({ log: true, corePath: "/ffmpeg_core_dist/ffmpeg-core.js" });

worked locally. But on production I got the error Uncaught (in promise) ReferenceError: SharedArrayBuffer is not defined which I was able to fix by adding these headers to next.config.js:

async headers() {
    return [
        {
            source: "/", // change to appropriate path
            headers: [
                {
                    key: "Cross-Origin-Embedder-Policy",
                    value: "require-corp",
                },
                {
                    key: "Cross-Origin-Opener-Policy",
                    value: "same-origin",
                },
            ],
        },
    ];
},

But this is not a perfect solution because SharedArrayBuffer is not defined still happens sometimes on page load and a reload is needed to get ffmpeg to load. Also it has other issues mentioned here. This all has to do with the security of SharedArrayBuffer that goes above my head.

It worked for me thank you @discretegames !

My setting:

  • Next.js (JavaScript)
  • @ffmpeg/core==0.10.0
  • @ffmpeg/ffmpeg==0.10.1
// index.jsx  (or some component .js/.jsx file)

const ffmpeg = createFFmpeg({
  corePath: 'https://unpkg.com/@ffmpeg/[email protected]/dist/ffmpeg-core.js',
  log: true,
});
// next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  async headers() {
    return [
      {
        source: '/',
        headers: [
          {
            key: 'Cross-Origin-Embedder-Policy',
            value: 'require-corp',
          },
          {
            key: 'Cross-Origin-Opener-Policy',
            value: 'same-origin',
          },
        ],
      },
    ];
  },
};

module.exports = nextConfig;

Thank you again :)

Thank you! this works for me as well!

Just want to add that you will also be able to use version 0.11.0

Next.js (JavaScript)

  • @ffmpeg/core==0.11.0
  • @ffmpeg/ffmpeg==0.11.6
const ffmpeg = createFFmpeg({
        corePath: 'https://unpkg.com/@ffmpeg/[email protected]/dist/ffmpeg-core.js',
        log: true,
      })

jereloh avatar Dec 07 '22 15:12 jereloh

Another solution, to utilize NextJS middle ware

create middleware.ts following: here

// eslint-disable-next-line @next/next/no-server-import-in-page
import { NextResponse } from 'next/server'
// eslint-disable-next-line @next/next/no-server-import-in-page
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
  const requestHeaders = new Headers(request.headers)

  //   if (request.nextUrl.pathname.startsWith('/')) { modify '/' accordingly
  const response = NextResponse.next({
    request: {
      // New request headers
      headers: requestHeaders,
    },
  })
  response.headers.set('Cross-Origin-Embedder-Policy', 'require-corp')
  response.headers.set('Cross-Origin-Opener-Policy', 'same-origin')
  return response
  //   }
}

jereloh avatar Dec 09 '22 10:12 jereloh

I'm getting this while running it within a Jest unit test... so I don't have the (easy) possibility to just put it at localhost somewhere. What's the recommended way to use ffmpeg.wasm with jest?

princefishthrower avatar Dec 22 '22 09:12 princefishthrower

can not get this to work once built on my domain but npm run dev works ok in webpack. Did anyone find out why this error is happening?

salivity avatar Feb 27 '23 20:02 salivity

I've found out whats causing it but don't know how to solve, when i disabled the terser mangler in webpack the module has no problems finding the createFFmpegCore function.

salivity avatar Feb 27 '23 22:02 salivity

I've found out whats causing it but don't know how to solve, when i disabled the terser mangler in webpack the module has no problems finding the createFFmpegCore function.

terser mangler renames the variables in a program in the form of "mangling"

you actually found the root cause of this and turning off terser is a temporary fix, but even i don't know a permanent fix for it, tho i think moving the ffmpeg-core.json file into the src directory might work

samudraraj avatar Apr 03 '23 17:04 samudraraj

Globalizing this script can solve this problem.

WXperia avatar Apr 14 '23 11:04 WXperia

Just do the simple try bring down the versions @ffmpeg/ffmpeg =>0.9.8 @ffmpeg/core =>0.8.3

now add log is true and corePath to the cdn link and now add source="/" in next.config.js and same in vite.config.js if you are using vite.

Devgrammer avatar Aug 18 '23 02:08 Devgrammer

headers: [ { key: 'Cross-Origin-Embedder-Policy', value: 'require-corp', }, { key: 'Cross-Origin-Opener-Policy', value: 'same-origin', }, ],

    how to add them in simple react project?

rabia-shafqat avatar Oct 16 '23 18:10 rabia-shafqat

Just do the simple try bring down the versions @ffmpeg/ffmpeg =>0.9.8 @ffmpeg/core =>0.8.3

now add log is true and corePath to the cdn link and now add source="/" in next.config.js and same in vite.config.js if you are using vite.

do u have example for vite.config.js ?

juanptoror avatar Dec 14 '23 21:12 juanptoror

For anyone using create-react-app: Remove ffmpeg and ffmpeg/core npm uninstall @ffmpeg/ffmpeg @ffmpeg/core

Run the code below to downgrade npm install @ffmpeg/[email protected] @ffmpeg/[email protected]

Then follow this to setup the headers

For anyone using react-scripts or create-react-app to run React, you can send headers manually by using a customized middleware (https://create-react-app.dev/docs/proxying-api-requests-in-development/#configuring-the-proxy-manually).

First, install http-proxy-middleware:

npm install http-proxy-middleware --save

or

yarn add http-proxy-middleware

Then, create src/setupProxy.js file and put our logic in there:

module.exports = function (app) {
    app.use(function (request, response, next) {
        response.setHeader("Cross-Origin-Opener-Policy", "same-origin");
        response.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
        next();
    });
};

asgomda avatar Dec 19 '23 11:12 asgomda

@rabia-shafqat https://github.com/ffmpegwasm/ffmpeg.wasm/issues/262#issuecomment-1862579923

asgomda avatar Dec 19 '23 11:12 asgomda