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

error when using with python in a directory that is declared to be a js-module #149

Open
spike-rabbit opened this issue Nov 21, 2024 · 4 comments

Comments

@spike-rabbit
Copy link

spike-rabbit commented Nov 21, 2024

Reproduction Repo: https://github.com/spike-rabbit/python-js-module-bug
The important part is, that the venv are in the same directory or below the projects package.json.

Error:

[JSE] ReferenceError: require is not defined in ES module scope, you can use import instead

[JSE] This file is being treated as an ES module because it has a '.js' file extension and '/home/***/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

[JSE]     at file:///home/***/.venv/lib/python3.13/site-packages/javascript/js/bridge.js:9:14

[JSE]     at ModuleJob.run (node:internal/modules/esm/module_job:218:25)

[JSE]     at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)

[JSE]     at async loadESM (node:internal/process/esm_loader:28:7)

[JSE]     at async handleMainPromise (node:internal/modules/run_main:113:12)

Potential solutions

Sure, I could move things around in my project to get rid of the module problem.

But the much nicer solutions would be either:

  • .js files provided by the javascript module all have .cjs ending to ensure they are always treated as commonjs
  • add a package.json inside the javascript module that explicitly declare {"type": "commonjs"}
@extremeheat
Copy link
Owner

Can you please elaborate more on how you got this error (and your proposed fix?)

@spike-rabbit
Copy link
Author

Thx for replying and taking care.

Sorry for the initial incomplete description. I now added a reproduction repo and slightly improved solution suggestions.
I can create a PR once you decided for a preferred solution approach.

@extremeheat
Copy link
Owner

Can't reproduce on a new github codespace:

 /workspaces/python-js-module-bug (main) $ poetry install
Creating virtualenv python-js-module-WaJCDlOP-py3.12 in /home/codespace/.cache/pypoetry/virtualenvs
Installing dependencies from lock file

Package operations: 1 install, 0 updates, 0 removals

  - Installing javascript (1!1.2.1)
 /workspaces/python-js-module-bug (main) $ poetry run python python-that-imports-js.py
[
  '.git',
  '.gitignore',
  'README.md',
  'module-based-js-file.js',
  'package-lock.json',
  'package.json',
  'poetry.lock',
  'pyproject.toml',
  'python-that-imports-js.py'
]
 /workspaces/python-js-module-bug (main) $ node --version
v20.17.0
 /workspaces/python-js-module-bug (main) $ python --version
Python 3.12.1

@spike-rabbit
Copy link
Author

This works as your .venv is not in /workspaces/python-js-module-bug.

So the projects package.json is not interfering with the .venv js files.

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

No branches or pull requests

2 participants