Javascript / Node / Twilio - ReferenceError:require未定义

时间:2022-06-16 07:53:32

I have installed Node from:

我从以下位置安装了Node:

Node

节点

and run this in cmd:

并在cmd中运行它:

npm install twilio

I then tried the example code provided by Twilio:

然后我尝试了Twilio提供的示例代码:

var accountSid = 'MyAccountSidHere';
var authToken = "MyAccountAuthTokenHere";
var client = require('twilio')(accountSid, authToken);

client.sms.messages.create({
    body: "Jenny please?! I love you <3",
    to: "SomeNumber",
    from: "MyNumber"
}, function(err, message) {
    process.stdout.write(message.sid);
});

Saved this to MyFile.js file and double clicked it.

将此保存到MyFile.js文件并双击它。

I get the error message:

我收到错误消息:

ReferenceError: require is not defined

This is my first encounter with JavaScript and I found a lot of similar questions, but have not been able to solve this.

这是我第一次遇到JavaScript,我发现了很多类似的问题,但一直无法解决这个问题。

I am to use this with QML, so I want to load it using:

我将它用于QML,所以我想使用以下方法加载它:

import "MyFile.js" as MyFile

then call the javascript code as a function.

然后将javascript代码作为函数调用。

1 个解决方案

#1


3  

I've read a little into QML and I don't see how you could use a node.js module in QML. QML is used as a language where QT is the JavaScript engine and node.js is a server-side Javascript engine.

我已经阅读了一些QML,我看不出你如何在QML中使用node.js模块。 QML用作QT是JavaScript引擎的语言,node.js是服务器端Javascript引擎。

The require() function is a core function of node.js which is part of the engine. It's not something language-specific just like the window object in browser-based Javascript is not something in the Javascript language.

require()函数是node.js的核心函数,它是引擎的一部分。这不是语言特定的东西,就像基于浏览器的Javascript中的窗口对象不是Javascript语言中的东西。

As I said in my comment, you should check out what node.js actually is: a server-side JavaScript engine, which executes JavaScript files. It is not a framework which you could load into another engine like QT.

正如我在评论中所说,您应该查看node.js实际上是什么:服务器端JavaScript引擎,它执行JavaScript文件。它不是一个可以加载到QT等其他引擎的框架。

Your code will run if you use it like this from the command-line:

如果您从命令行使用它,您的代码将运行:

node MyFile.js

I doubt this is helpful for your use-case as a QML import though.

我怀疑这对你的用例作为QML导入是有帮助的。

#1


3  

I've read a little into QML and I don't see how you could use a node.js module in QML. QML is used as a language where QT is the JavaScript engine and node.js is a server-side Javascript engine.

我已经阅读了一些QML,我看不出你如何在QML中使用node.js模块。 QML用作QT是JavaScript引擎的语言,node.js是服务器端Javascript引擎。

The require() function is a core function of node.js which is part of the engine. It's not something language-specific just like the window object in browser-based Javascript is not something in the Javascript language.

require()函数是node.js的核心函数,它是引擎的一部分。这不是语言特定的东西,就像基于浏览器的Javascript中的窗口对象不是Javascript语言中的东西。

As I said in my comment, you should check out what node.js actually is: a server-side JavaScript engine, which executes JavaScript files. It is not a framework which you could load into another engine like QT.

正如我在评论中所说,您应该查看node.js实际上是什么:服务器端JavaScript引擎,它执行JavaScript文件。它不是一个可以加载到QT等其他引擎的框架。

Your code will run if you use it like this from the command-line:

如果您从命令行使用它,您的代码将运行:

node MyFile.js

I doubt this is helpful for your use-case as a QML import though.

我怀疑这对你的用例作为QML导入是有帮助的。