yunabe / tslab

Interactive JavaScript and TypeScript programming with Jupyter
Apache License 2.0
700 stars 44 forks source link

Error [ERR_REQUIRE_ESM]: require() of ES Module not supported. #72

Open shotexa opened 2 years ago

shotexa commented 2 years ago

I'm getting the error when trying to import a library in my code using the following syntax

import {
  SomeObject
} from 'library';

the error looks like this:

node:internal/modules/cjs/loader:1150
      throw err;
      ^

Error [ERR_REQUIRE_ESM]: require() of ES Module <Folder where notebook is located>/node_modules/<library>/dist/index.js from <Folder where notebook is located>/src.js not supported.
Instead change the require of index.js in <Folder where notebook is located>/src.js to a dynamic import() which is available in all CommonJS modules.
    at Object.apply (/usr/local/lib/node_modules/tslab/dist/executor.js:63:24)
    at evalmachine.<anonymous>:22:26
    at evalmachine.<anonymous>:25:3
    at sigintHandlersWrap (node:vm:268:12)
    at Script.runInThisContext (node:vm:127:14)
    at Object.runInThisContext (node:vm:305:38)
    at Object.execute (/usr/local/lib/node_modules/tslab/dist/executor.js:162:38)
    at JupyterHandlerImpl.handleExecuteImpl (/usr/local/lib/node_modules/tslab/dist/jupyter.js:219:38)
    at /usr/local/lib/node_modules/tslab/dist/jupyter.js:177:57
    at async JupyterHandlerImpl.handleExecute (/usr/local/lib/node_modules/tslab/dist/jupyter.js:177:21)
    at async ZmqServer.handleExecute (/usr/local/lib/node_modules/tslab/dist/jupyter.js:375:25)
    at async ZmqServer.handleShellMessage (/usr/local/lib/node_modules/tslab/dist/jupyter.js:320:21) {
  code: 'ERR_REQUIRE_ESM'
}

If that is a problem with my library and not with tslab kernel itself, could you tell me what it is?

Node - 16.7.0 jupyter --version

IPython          : 8.4.0
ipykernel        : 6.15.0
ipywidgets       : 7.7.1
jupyter_client   : 7.3.4
jupyter_core     : 4.10.0
jupyter_server   : not installed
jupyterlab       : not installed
nbclient         : 0.6.6
nbconvert        : 6.5.0
nbformat         : 5.4.0
notebook         : 6.4.12
qtconsole        : 5.3.1
traitlets        : 5.3.0

OS - MacOS Monterey 12.4

devYonz commented 1 year ago

Did you figure it out? I'm expecting that tslab is wrapping around node and doesn't support esm and maybe something like this could work https://github.com/standard-things/esm

Context about ESM and CommonJS : https://formidable.com/blog/2021/node-esm-and-exports/

The dynamic import() which is available in all CommonJS modules seems to give away that the imports are trying to be loaded in a commonJS format

KhalfaniW commented 1 year ago

tslab runs in a node vm and esm cannot run in a vm

There is a similar problem in ijavascript

However they found a workaround: require("esm-hook"); then whatever require whatever package was needed.