-
Notifications
You must be signed in to change notification settings - Fork 40
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
Lacking Support for Node.js 'process' Variable #267
Comments
Hi, @arvindavoudi! PythonMonkey is a library which links JavaScript to Python under the hood, using a bare-minimum JS host environment (imagine the browser without the DOM). It is not a plug-in replacement for NodeJS, although it could be used to write one (we plan to do that some day). You have at your disposal in PythonMonkey all of the JS language, plus a few extra globals that are modelled after what is available in the browser, excluding the DOM for obvious reasons
We have also implemented a CommonJS PythonMonkey does not support ES modules at this time. We are hoping to forge some kind of Python and ES module equivalence in the future. The JS context also has access to Python via the It would be relatively easy to create a import sys
import os
exports['argv0'] = sys.executable
exports['pid'] = os.getpid()
exports['env'] = os.environ Or perhaps you could find it easier to write python.exec(`
import pythonmonkey
import sys
import os
import inspect
`);
process.argv0 = python.eval('sys.executable');
process.argv = [ process.argv0 ].concat(python.eval('sys.argv'));
process.exit = function pm$$processExit(exitCode) {
if (arguments.length === 0)
exitCode = process.exitCode;
if (typeof exitCode !== 'number')
exitCode = !exitCode ? 0 : parseInt(exitCode) || 1;
python.eval('lambda x: exit(int(x))')(exitCode & 255);
}; Note - I suspect you will need a lot more "node" to run react-dom/server than just the process module. |
Thanks for your help, wesgarland. Your guidance was invaluable since there was no rich official documentation available. Upon a thorough examination of React and ReactDOM's source code, I identified the prevalent use of import pythonmonkey as pm
pm.globalThis['process'] = {'env': {'NODE_ENV': None}}
react = pm.require('react') # No Issue
reactdom_server = pm.require('react-dom/server') # Where we encountered errors Note: The But wait, we have more critical issues in PythonMonkey!! First Thing First, React-DOM Patching ProcessAfter using After resolving the issue for the undefined pythonmonkey.SpiderMonkeyError: Error in file /opt/homebrew/lib/python3.11/site-packages/pythonmonkey/node_modules/ctx-module/ctx-module.js, on line 236:
Error: module not found -- require('stream') from /my_project/node_modules/react-dom/cjs/react-dom-server-legacy.node.development.js It shows we need a standard library implemented in Node js named NoteThere is a package named ReactDOM Patching Has These Steps:1- In 2- Then open // Delete line 18 which is
var stream = require('stream'); // Change line 7032 from
}(stream.Readable);
// To
}; // Remove "(stream.Readable);" 3- Do the following changes in // Replace line 18 which is
var util = require('util');
// With
const TextEncodingPolyfill = require('text-encoding');
const util = {
TextEncoder: TextEncodingPolyfill.TextEncoder,
TextDecoder: TextEncodingPolyfill.TextDecoder,
}; These two patches resolve the issue of not being able to use the Testing Usability After Patches, But Is It Really Usable?Now that importing 1- The First Method:# /my_project/main.py
import pythonmonkey as pm
pm.globalThis['process'] = {'env': {'NODE_ENV': None}}
pm.require('./main_') # Leads to running the JS script written below // /my_project/main_.js
const React = require('react'); // Never had any issues
const ReactDOMServer = require('react-dom/server'); // No Issues in importing Anymore
function App() {
console.log("Component function is being run.")
return React.createElement("h1", {}, "Hello");
}
console.log(ReactDOMServer.renderToString(React.createElement(App))); Output of Component function is being run.
<h1>Hello</h1> 2- The Second Method:# /my_project/main.py
import pythonmonkey as pm
pm.globalThis['process'] = {'env': {'NODE_ENV': None}}
rendered_component = pm.require('./main_') # Leads to running the JS script written below
print(rendered_component['html_component']) // /my_project/main_.js
const React = require('react'); // Never had any issues
const ReactDOMServer = require('react-dom/server'); // No Issues in importing Anymore
function App() {
console.log("Component function is being run.")
return React.createElement("h1", {}, "Hello");
}
exports['html_component'] = ReactDOMServer.renderToString(React.createElement(App)); The output generated by this code is also perfectly fine: Component function is being run.
<h1>Hello</h1> 3- The Third Method:# /my_project/main.py
import pythonmonkey as pm
pm.globalThis['process'] = {'env': {'NODE_ENV': None}}
react = pm.require('react') # Never had any issues
reactdom_server = pm.require('react-dom/server') # No Issues in importing Anymore
app_component = pm.require('./main_')
print(
reactdom_server.renderToString(react.createElement(app_component))
) // /my_project/main_.js
const React = require('react');
module.exports = function App() {
console.log("Component function is being run.")
return React.createElement("h1", {}, "Hello");
} In this method, I have encountered some critical issues shown by the printed message below: zsh: segmentation fault python3 main.py 4- The Fourth Method:# /my_project/main.py
import pythonmonkey as pm
pm.globalThis['process'] = {'env': {'NODE_ENV': None}}
react = pm.require('react') # Never had any issues
reactdom_server = pm.require('react-dom/server') # No Issues in importing Anymore
def app_component():
return react.createElement("h1", {}, "Hello")
print(
reactdom_server.renderToString(react.createElement(app_component))
) Similar to the previous method, this approach can also lead to critical errors. I have encountered two different types of errors, which I will list below. They don't follow a certain pattern and I randomly see both of them: zsh: segmentation fault python3 main.py zsh: bus error python3 main.py I suspect that the issue may be caused by calling a JavaScript function inside a Python function and then returning that Pythonic function to a JavaScript function. Although I couldn't find any other evidence of this happening, this seems to be the most likely logic behind the problem. I would appreciate it if you could check it out. Additionally, if you want, I can upload the entire |
@arvindavoudi Hi, can you try again with our latest release? |
🚨 Problem with Node Modules Requiring Process Identification
📝 Description
The "process" variable is fundamental in Node.js environments, suggesting that
pythonmonkey
might not fully emulate or provide a Node.js-like environment for certain modules. Understanding how to properly bridge this gap or if there's a workaround would be crucial for progressing with my project.🔄 Steps to Reproduce
pythonmonkey
using the commandpip install pythonmonkey
.my_project/package.json
:Run
npm install
in the project directory to install the Node.js dependencies listed inmy_project/package.json
, specifically 'react' and 'react-dom'.Add the following Python code to
my_project/main.py
directory to use the "react-dom/server" module:my_project/main.py
using the commandpython3 main.py
.🌟 Expected Behavior
I expected to successfully import and use "react-dom/server" in my Python script for server-side rendering of React components, leveraging
pythonmonkey
for integration.🛑 Actual Behavior
Encountering an error when executing
my_project/main.py
, indicating a problem with recognizing the process variable, which is essential for some Node.js modules:ℹ️ Additional Information
PythonMonkey Installation Method:
Installed from pip
OS Platform and Distribution:
MacOS 14.2.1
Python Version (
python --version
):3.11.7
PythonMonkey Version (
pip show pythonmonkey
):0.3.0
The text was updated successfully, but these errors were encountered: