Embed Node.js natively in Python with seamless integration.
pythonodejs lets you harness the power of the Node.js ecosystem directly within your Python applications. Run JavaScript code, use NPM packages, and leverage the best of both worlds in a single application.
pip install pythonodejsfrom pythonodejs import node_eval
# Execute JavaScript directly
node_eval("console.log('Hello from Node.js in Python!')")
# > Hello from Node.js in Python!
# Create and return JavaScript functions
read_file = node_eval("""
const fs = require('fs');
function readFile(filePath) {
return fs.readFileSync(filePath, 'utf8');
}
readFile; // Return the function
""")
# Call JavaScript functions from Python
content = read_file("example.txt")
print(content)More Examples
npm init -y
npm install lodash express axiosfrom pythonodejs import node_eval
result = node_eval("""
const _ = require('lodash');
const numbers = [1, 2, 3, 4, 5];
_.sum(numbers);
""")
print(f"Sum: {result}")
node_eval("""
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello from Express running in Python!');
});
app.listen(3000, () => {
console.log('Express server running on port 3000');
});
""")Working with JS Objects
person = node_eval("""
const person = {
name: 'John',
age: 30,
greet: function() {
return `Hello, my name is ${this.name}`;
}
};
person;
""")
print(person['name'])
print(person['greet']())Passing Python → JS
process_data = node_eval("""
function processData(data) {
return {
processed: true,
items: data.items.map(x => x * 2),
message: `Processed ${data.items.length} items`
};
}
processData;
""")
result = process_data({'items': [1, 2, 3, 4, 5]})
print(result)- Stable release
- Loading ES Modules
- Automatic npm install on dependency detection
- Better error logging
- Install SConstruct and Clang
- Run
pip install -r requirements.txt - Run
scons
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License.
A special thanks to M-logique for their contribution to this project.