jangxyz / pyodide-to-esm

wrap pyodide as an esm module

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pyodide-to-esm

Even with node.js support in pyodide, I see some weird errors while importing pyodide, especially with ESM modules:

// dynamically importing pyodide
(await import('pyodide')).loadPyodide;
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/PATH_TO_YOUR_PROJECT/file:/PATH_TO_YOUR_PROJECT/node_modules/pyodide/pyodide.asm.js' imported from /PATH_TO_YOUR_PROJECT/node_modules/pyodide/pyodide.mjs

Seems like a mismatch between ESM and CJS modules regarding handling the import.meta.url. Strangely enough, require() works, but there are cases when you cannot change the module system by yourself.

This is an attempt to wrap a CJS-friendly pyodide module as a separate, ESM-friendly module.

Usage

Install:

npm install github:jangxyz/pyodide-to-esm

Usage:

import { loadPyodide } from 'pyodide-to-esm';

Obsolete

Found out that you can use this instead in an ESM environment:

const { createRequire } = await import('module');
const require = createRequire(import.meta.url);
const loadPyodide = require('pyodide').loadPyodide;

About

wrap pyodide as an esm module


Languages

Language:JavaScript 100.0%