tfjs: SelfieSegmentation is not a constructor

Hi, Using vuejs3+vite+ts I am getting this error Uncaught (in promise) TypeError: jle.SelfieSegmentation is not a constructor

Screenshot (230)

There was a discussion here https://github.com/vitejs/vite/issues/4680

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 21 (2 by maintainers)

Most upvoted comments

I found the solution and I am putting it here just as a reference for any one who crosses path here.

function mediapipe_workaround() {
  return {
    name: 'mediapipe_workaround',
    load(id: string) {
      if (basename(id) === 'selfie_segmentation.js') {
        let code = fs.readFileSync(id, 'utf-8');
        code += 'exports.SelfieSegmentation = SelfieSegmentation;';
        return { code };
      } else {
        return null;
      }
    },
  };
}

vite.config

...
    build: {
      rollupOptions: {
        plugins: [mediapipe_workaround()],
      },
    }

@Exlord your workaround work for me , can we use in production build

import path from "path";
import { fileURLToPath } from "url";

/** Define `__dirname` manually for ESM */
const __dirname = path.dirname(fileURLToPath(import.meta.url));

export default {
  webpack: (config) => {
    config.module.rules.push({
      test: /selfie_segmentation\.js$/,
      use: [
        {
          loader: path.resolve(__dirname, "mediapipe-loader.js"),
        },
      ],
    });

    return config;
  },
};

mediapipe-loader.js

module.exports = function (source) {
  if (!source.includes('exports.SelfieSegmentation')) {
    source += '\nexports.SelfieSegmentation = SelfieSegmentation;';
  }
  return source;
};

The above has worked with nextjs 15.2.1

Yes, this is a build time enchantment, its perfectly safe for production, I am using it in production.

Hi, @Exlord

I apologize for the delayed response. After reviewing the issue mentioned in @Bersaelor comment, it appears that the issue is with the MediaPipe package. Please refer to this comment for more details. I would recommend posting this issue in the MediaPipe repo so that the MediaPipe team can address it,please feel free to close this issue.

If Am I missing something here please let me know ? Thank you!