Javascript抛出“ReferenceError: require未定义”

时间:2022-11-06 14:44:50

I am running a piece of Javascript code inside my Python code using the js2py library.It works fine for all kinds of Javascript code thrown at it and processes the result.However I am encountering an error when a 'require' keyword is used in the Javascript code.Here is my code below

我正在使用js2py库在Python代码中运行一段Javascript代码。它适用于抛出的各种Javascript代码并处理结果。然而,当在Javascript代码中使用“require”关键字时,我遇到了一个错误。下面是我的代码

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)

Here jsonata is a javascript library for parsing JSON installed through npm.

这里jsonata是一个javascript库,用于解析通过npm安装的JSON。

Below is the error I receive

下面是我收到的错误

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

However when I put just the javascript code in a file and run it from the command line, it works.

但是,当我将javascript代码放在一个文件中并从命令行运行时,它就可以工作了。

jstest.js

jstest.js

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)

In the command line

在命令行

node jstest.js  --> gives 25

Now I did some investigation on the error and it turns out that 'require' is a browser requirement and is not for node.js. It also turns out there is an alternative for this called require.js that fixes this problem.So I went to their documentation but couldn't make much sense of how I could define require in my Javascript code.Also I pass jsonata as an argument to require which is a .js file.How do I use require.js framework to handle a jsonata.js file as opposed to function declaration given in the examples?I may be wrong in my understanding of the subject but I need to get a clarification on this?Is there any way I can make this work?

现在我对这个错误做了一些调查,结果发现“require”是一个浏览器需求,而不是node.js。还有一个替代方法叫做“需要”。这就解决了这个问题。所以我去看了他们的文档,但是我不知道如何在Javascript代码中定义require。我还将jsonata作为参数传递,以要求它是.js文件。我如何使用要求。用于处理jsonata的js框架。与示例中给出的函数声明相反的js文件?我对这个问题的理解可能是错误的,但我需要澄清一下。我有什么办法使它工作吗?

Note: I am running the code in the server side.

注意:我正在服务器端运行代码。

1 个解决方案

#1


2  

Please try the newest Js2Py that supports requiring npm modules.The issue with your code was just that require was not supported.Please add this statement after importing js2py.

请尝试最新的支持npm模块的Js2Py。您的代码的问题只是不支持需求。请在导入js2py后添加此语句。

from js2py import require

Please let me know if your code is working fine after the new addition.

请让我知道你的代码在新添加后是否正常工作。

#1


2  

Please try the newest Js2Py that supports requiring npm modules.The issue with your code was just that require was not supported.Please add this statement after importing js2py.

请尝试最新的支持npm模块的Js2Py。您的代码的问题只是不支持需求。请在导入js2py后添加此语句。

from js2py import require

Please let me know if your code is working fine after the new addition.

请让我知道你的代码在新添加后是否正常工作。