2016-04-08 79 views
0

我正在构建一个Web应用程序,允许用户输入电话号码并通过Twilio API发送文本消息。我已经在一个文件中构建了这个功能,如下所示。如果我将此文件cd并运行node twilioActions.js,则会发送文本消息。ModuleParse失败或未找到

var client = require('twilio')(CLIENT_ID, CLIENT_SECRET); 

// ideally, I'd like to send the message using this method and call this from other JavaScript files 
export const sendMessage =() => { 

} 

// the following code works when I run 'node twilioActions.js' 
client.sendMessage({ 
    to:'...', 
    from: '...', 
    body: 'Text message test.' 
}, function(err, responseData) { 
    if (!err) { 
    console.log(responseData.from); // outputs "+14506667788" 
    console.log(responseData.body); // outputs "word to your mother." 
    } 
}); 

但是,我想从另一个React文件中调用sendMessage方法。下面是它:

import * as twilioActions from './twilioActions'; 

class PhoneView extends React.Component{ 
    // other methods are hidden obviously, the one below is called when a button is pressed to send a message. 
    sendMessage() { 
    twilioActions.sendMessage(); 
    } 
} 

当我尝试生成项目,我收到以下错误:

ERROR in ./~/twilio/package.json 
Module parse failed:/Users/Felix/Desktop/ECE590/node_modules/twilio/package.json Line 2: Unexpected token : 
You may need an appropriate loader to handle this file type. 
| { 
| "_args": [ 
|  [ 
|  "twilio", 
@ ./~/twilio/lib/Client.js 5:17-43 

ERROR in ./~/request/lib/har.js 
Module not found: Error: Cannot resolve module 'fs' in /Users/Felix/Desktop/ECE590/node_modules/request/lib 
@ ./~/request/lib/har.js 3:9-22 

ERROR in ./~/tunnel-agent/index.js 
Module not found: Error: Cannot resolve module 'net' in /Users/Felix/Desktop/ECE590/node_modules/tunnel-agent 
@ ./~/tunnel-agent/index.js 3:10-24 

ERROR in ./~/tunnel-agent/index.js 
Module not found: Error: Cannot resolve module 'tls' in /Users/Felix/Desktop/ECE590/node_modules/tunnel-agent 
@ ./~/tunnel-agent/index.js 4:10-24 

我觉得我做一个简单的错误,也许我没有使用正确的库或包括适当的参考。有人能够指导我如何实现这一目标的正确方向吗?非常感谢!

+0

你用什么来建立这个项目?的WebPack?这听起来像你没有像Babel这样的转译器,并且已经配置好在你的构建步骤中处理ES2015。 – Sean

回答

0

Twilio开发者传道士在这里。

twilio npm模块不是建造或推荐用于前端。最主要的是,您需要在您的网站的前端代码中公开您的帐户凭证。这是一种安全风险,因为这意味着恶意攻击者可能会获取您的凭据并滥用您的Twilio帐户。

我建议在服务器端创建一个服务,您可以通过AJAX请求调用服务来执行此类操作。

相关问题