question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Js2Py throws 'ReferenceError: require is not defined'

See original GitHub issue

I am using js2py to run Javascript code inside my Python code and it works fine for basic function calls or console outputs.However when I try to use an npm library jsonata inside the Javascript code, it throws an error.

Traceback (most recent call last):
  File "/home/souvik/PycharmProjects/ServiceHandler/Testjs.py", line 67, in <module>
    data = js2py.eval_js(data)
  File "/home/souvik/utorapp/lib/python3.5/site-packages/js2py/evaljs.py", line 113, in eval_js
    return e.eval(js)
  File "/home/souvik/utorapp/lib/python3.5/site-packages/js2py/evaljs.py", line 182, in eval
    self.execute(code, use_compilation_plan=use_compilation_plan)
  File "/home/souvik/utorapp/lib/python3.5/site-packages/js2py/evaljs.py", line 177, in execute
    exec(compiled, self._context)
  File "<EvalJS snippet>", line 2, in <module>
  File "/home/souvik/utorapp/lib/python3.5/site-packages/js2py/base.py", line 899, in __call__
    return self.call(self.GlobalObject, args)
  File "/home/souvik/utorapp/lib/python3.5/site-packages/js2py/base.py", line 1344, in call
    return Js(self.code(*args))
  File "/home/souvik/utorapp/lib/python3.5/site-packages/js2py/host/jseval.py", line 42, in Eval
    executor(py_code)
  File "/home/souvik/utorapp/lib/python3.5/site-packages/js2py/host/jseval.py", line 49, in executor
    exec(code, globals())
  File "<string>", line 2, in <module>
  File "/home/souvik/utorapp/lib/python3.5/site-packages/js2py/base.py", line 1079, in get
    raise MakeError('ReferenceError', '%s is not defined' % prop)
js2py.internals.simplex.JsException: ReferenceError: require is not defined

Here is my code below

Testjs.py

import js2py

data = '''var jsonata = require("jsonata");

var data = {
  example: [
    {value: 4},
    {value: 7},
    {value: 13}
  ]
};
var expression = jsonata("$sum(example.value)");
var result = expression.evaluate(data);
console.log(result)
'''

data = js2py.eval_js(data)
print(data)

Note: I have npm, node.js and jsonata installed

The weird part is if I run just the Javascript code saved to a file in the command line, it works just fine

Testjs2.js

var jsonata = require("jsonata");

var data = {
  example: [
    {value: 4},
    {value: 7},
    {value: 14}
  ]
};
var expression = jsonata("$sum(example.value)");
var result = expression.evaluate(data);
console.log(result) 

In the command line node Testjs2.js --> gives me 25

I investigated for a while and found this in js2py documentation

You can also import a big number of node modules as if they were written in Python! For example, here we import a pure JS library crypto-js:

    >>> CryptoJS = js2py.require('crypto-js')
    >>> data = [{'id': 1}, {'id': 2}]
    >>> JSON = js2py.eval_js('JSON')
    >>> ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data), 'secret key 123')
    >>> bytes  = CryptoJS.AES.decrypt(ciphertext.toString(), 'secret key 123')
    >>> decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8)).to_list()
    >>> decryptedData
    [{u'id': 1}, {u'id': 2}]

So I even added this line js2py.require('node') to my Testjs.py file but then it started installing some ‘Babel’ related files and then threw the same error.How do I run this piece of Javascript code in my Python file?

Additional resources:

jsonata documentation

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
Souvikraycommented, Mar 7, 2018

@Piotr any work around for this?

7reactions
Inthenewcommented, Jun 22, 2021

inside the translated file, right after it imports js2py you can add from js2py import require then for all the modules you want to import you put <module name> = require('<module name>') then you replace: var.put(u'<variable name>', var.get(u'require')(Js(u'<module name>'))) with: var.put(u'<variable name>', <module name>)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript throws 'ReferenceError: require is not defined'
I am running a piece of Javascript code inside my Python code using the js2py library.It works fine ...
Read more >
ReferenceError: require is not defined in JavaScript | bobbyhadz
To solve the "ReferenceError require is not defined" error, use the ES6 module import and export syntax. The require() function is Node.js specific...
Read more >
ReferenceError: require is not defined in JavaScript - Stack Diary
So, in this case, an error is thrown if you try to use the require function in a file that is intended to...
Read more >
How To Fix ReferenceError require is not defined in JavaScript
In Node.js, require is a function that is available to use JavaScript modules elsewhere. The syntax for using require is defined by the...
Read more >
Js2Py - Bountysource
JsException: ReferenceError: Promise is not defined. Ex in python: import js2py StackGPS = js2py.require('stacktrace-gps'); ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found