Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JupyterLab packages in node #12518

Open
davidbrochart opened this issue May 3, 2022 · 2 comments
Open

Use JupyterLab packages in node #12518

davidbrochart opened this issue May 3, 2022 · 2 comments

Comments

@davidbrochart
Copy link
Contributor

Problem

I would like to use JupyterLab packages in node. But if I run node with the following file (after installing @jupyterlab/shared-models with npm):

import { YNotebook } from '@jupyterlab/shared-models'

I get:

(node:151939) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/david/github/davidbrochart/jupyter_ydoc/tests/foo.js:1
import { YNotebook } from '@jupyterlab/shared-models'
^^^^^^

SyntaxError: Cannot use import statement outside a module

If I add "type": "module" in package.json, I get:

import { YNotebook } from '@jupyterlab/shared-models'
         ^^^^^^^^^
SyntaxError: Named export 'YNotebook' not found. The requested module '@jupyterlab/shared-models' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@jupyterlab/shared-models';
const { YNotebook } = pkg;

If I change the file with:

import pkg from '@jupyterlab/shared-models'

I get:

(node:151807) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/david/github/davidbrochart/jupyter_ydoc/tests/node_modules/@jupyterlab/shared-models/lib/index.js:9
export * from './api';
^^^^^^

SyntaxError: Unexpected token 'export'

If I add "type": "module" in node_modules/@jupyterlab/shared-models/package.json, I get:

node:internal/errors:465
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/david/github/davidbrochart/jupyter_ydoc/tests/node_modules/@jupyterlab/shared-models/lib/api' imported from /home/david/github/davidbrochart/jupyter_ydoc/tests/node_modules/@jupyterlab/shared-models/lib/index.js
    at new NodeError (node:internal/errors:372:5)
    at finalizeResolution (node:internal/modules/esm/resolve:405:11)
    at moduleResolve (node:internal/modules/esm/resolve:964:10)
    at defaultResolve (node:internal/modules/esm/resolve:1173:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:604:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:318:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:80:40)
    at link (node:internal/modules/esm/module_job:78:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

If I change node_modules/@jupyterlab/shared-models/lib/index.js to the following (adding .js):

export * from './api.js';
export * from './ymodels.js';
export * from './utils.js';

I get:

import pkg from '@jupyterlab/shared-models'
       ^^^
SyntaxError: The requested module '@jupyterlab/shared-models' does not provide an export named 'default'

If I change back to:

import { YNotebook } from '@jupyterlab/shared-models'

It works!

Proposed Solution

It looks like the needed changes were:

  • add "type": "module" to each package's package.json
  • add .js to every export * from './x';

But I don't know what I'm doing 😄

@jupyterlab-probot jupyterlab-probot bot added the status:Needs Triage Applied to new issues that need triage label May 3, 2022
@fcollonval fcollonval removed the status:Needs Triage Applied to new issues that need triage label May 4, 2022
@jtpio
Copy link
Member

jtpio commented Dec 4, 2023

For reference this should work out of the box with Bun, a new JavaScript runtime which was recently a few months ago: https://bun.sh/

image

@Falven
Copy link

Falven commented Dec 11, 2023

For reference this should work out of the box with Bun, a new JavaScript runtime which was recently a few months ago: https://bun.sh/

image

Good observation, but bun is far from stable for production use.

Additionally, all of these modules should be properly configured to build as esm and cjs.

If you are looking for a way to streamline this I'd be happy to help.
You can refer to my Open Data Analysis repo, I have an ESBuild script and configuration setup to build for ESM, CJS and shared Types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants