2016-04-08 81 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請求調用服務來執行此類操作。

相關問題