質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
LINE Messaging API

LINE Messaging APIは、メッセージの送信・返信ができるAPIです。Web APIを経由しアプリケーションサーバとLINEのAPIでやり取りが可能。複数のメッセージタイプや分かりやすいAPIリファレンスを持ち、グループチャットにも対応しています。

Alexa

Alexa(アレクサ)は、米アマゾンが開発したクラウドベースのAIアシスタント。Amazon EchoやEcho dotに搭載され、話かけると音楽を再生したり、天気予報やスケジュールなど様々な情報を提供します。

AWS Lambda

AWS Lambdaは、クラウド上でアプリを実行できるコンピューティングサービス。サーバーのプロビジョニングや管理を要せず複数のイベントに対してコードを実行します。カスタムロジック用いた他AWSサービスの拡張やAWSの規模やパフォーマンスを用いたバックエンドサービスを作成できます。

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Q&A

0回答

2565閲覧

Lambdaでのエラー、「Unable to import module 'index'」の原因が分からない

yuta_kg

総合スコア24

LINE Messaging API

LINE Messaging APIは、メッセージの送信・返信ができるAPIです。Web APIを経由しアプリケーションサーバとLINEのAPIでやり取りが可能。複数のメッセージタイプや分かりやすいAPIリファレンスを持ち、グループチャットにも対応しています。

Alexa

Alexa(アレクサ)は、米アマゾンが開発したクラウドベースのAIアシスタント。Amazon EchoやEcho dotに搭載され、話かけると音楽を再生したり、天気予報やスケジュールなど様々な情報を提供します。

AWS Lambda

AWS Lambdaは、クラウド上でアプリを実行できるコンピューティングサービス。サーバーのプロビジョニングや管理を要せず複数のイベントに対してコードを実行します。カスタムロジック用いた他AWSサービスの拡張やAWSの規模やパフォーマンスを用いたバックエンドサービスを作成できます。

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

0グッド

1クリップ

投稿2019/09/12 13:43

編集2022/01/12 10:55

以下の記事を参考に、LINEログインとMessaging APIを使って、AlexaスキルからLINE botに通知するスキルを開発しています。
記事:https://qiita.com/imajoriri/items/0e975b15b9023cafce24

外部モジュールを使うので、ローカルにインストールしzipで圧縮しLambdaにアップロードしたのですが、以下のエラーがでました。

Unable to import module 'index': Error at Function.Module._resolveFilename (module.js:547:15) at Function.Module._load (module.js:474:25) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at Object.<anonymous> (/var/task/index.js:4:15) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3)

このエラーはLambdaの設定で、ハンドラの名前がindex.handlerになってないこと(index.jsの場合)が原因らしいのですが、index.handlerに設定してあります。

このエラーの原因が分かる方は教えてください。

また、zipファイルの内部は以下になります。
index.js
node_modules

以下、index.jsのコードとAlexa Developer Consoleでテストした時のレスポンスです。

javascript

1/* eslint-disable func-names */ 2/* eslint-disable no-console */ 3 4const Alexa = require('ask-sdk'); 5const rp = require('request-promise'); 6const line = require('@line/bot-sdk'); 7 8const client = new line.Client({ 9 // Lambdaの環境変数よりMessagingAPIのチャネルアクセストークンを取得 10 channelAccessToken: process.env["channelAccessToken"] 11}); 12 13const LaunchRequestHandler = { 14 canHandle(handlerInput) { 15 return handlerInput.requestEnvelope.request.type === 'LaunchRequest'; 16 }, 17 async handle(handlerInput) { 18 // Userごとのアクセストークン 19 const accessToken = handlerInput.requestEnvelope.session.user.accessToken; 20 console.log(accessToken); 21 22 var options = { 23 method: 'GET', 24 uri: 'https://api.line.me/v2/profile', 25 headers: { 26 'Authorization': `Bearer ${accessToken}` 27 } 28 }; 29 // アクセストークンより、LINEのUser情報を取得 30 var lineData = JSON.parse(await rp(options)); 31 32 const message = { 33 type: 'text', 34 text: 'スキルが起動されました。' 35 }; 36 37 const userId = lineData.userId; 38 39 // botに通知を送信 40 await client.pushMessage(userId, message); 41 42 var msg = 'Botにメッセージが送信されました'; 43 44 return handlerInput.responseBuilder 45 .speak(msg) 46 .reprompt(msg) 47 .getResponse(); 48 } 49}; 50 51// const GetNewFactHandler = { 52// canHandle(handlerInput) { 53// const request = handlerInput.requestEnvelope.request; 54// return request.type === 'LaunchRequest' 55// || (request.type === 'IntentRequest' 56// && request.intent.name === 'GetNewFactIntent'); 57// }, 58// handle(handlerInput) { 59// const factArr = data; 60// const factIndex = Math.floor(Math.random() * factArr.length); 61// const randomFact = factArr[factIndex]; 62// const speechOutput = GET_FACT_MESSAGE + randomFact; 63 64// return handlerInput.responseBuilder 65// .speak(speechOutput) 66// .withSimpleCard(SKILL_NAME, randomFact) 67// .getResponse(); 68// }, 69// }; 70 71const HelpHandler = { 72 canHandle(handlerInput) { 73 const request = handlerInput.requestEnvelope.request; 74 return request.type === 'IntentRequest' 75 && request.intent.name === 'AMAZON.HelpIntent'; 76 }, 77 handle(handlerInput) { 78 return handlerInput.responseBuilder 79 .speak(HELP_MESSAGE) 80 .reprompt(HELP_REPROMPT) 81 .getResponse(); 82 }, 83}; 84 85const ExitHandler = { 86 canHandle(handlerInput) { 87 const request = handlerInput.requestEnvelope.request; 88 return request.type === 'IntentRequest' 89 && (request.intent.name === 'AMAZON.CancelIntent' 90 || request.intent.name === 'AMAZON.StopIntent'); 91 }, 92 handle(handlerInput) { 93 return handlerInput.responseBuilder 94 .speak(STOP_MESSAGE) 95 .getResponse(); 96 }, 97}; 98 99const SessionEndedRequestHandler = { 100 canHandle(handlerInput) { 101 const request = handlerInput.requestEnvelope.request; 102 return request.type === 'SessionEndedRequest'; 103 }, 104 handle(handlerInput) { 105 console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`); 106 107 return handlerInput.responseBuilder.getResponse(); 108 }, 109}; 110 111const ErrorHandler = { 112 canHandle() { 113 return true; 114 }, 115 handle(handlerInput, error) { 116 console.log(`Error handled: ${error.message}`); 117 118 return handlerInput.responseBuilder 119 .speak('Sorry, an error occurred.') 120 .reprompt('Sorry, an error occurred.') 121 .getResponse(); 122 }, 123}; 124 125const SKILL_NAME = 'Space Facts'; 126const GET_FACT_MESSAGE = 'Here\'s your fact: '; 127const HELP_MESSAGE = 'You can say tell me a space fact, or, you can say exit... What can I help you with?'; 128const HELP_REPROMPT = 'What can I help you with?'; 129const STOP_MESSAGE = 'Goodbye!'; 130 131const data = [ 132 'A year on Mercury is just 88 days long.', 133 'Despite being farther from the Sun, Venus experiences higher temperatures than Mercury.', 134 'Venus rotates counter-clockwise, possibly because of a collision in the past with an asteroid.', 135 'On Mars, the Sun appears about half the size as it does on Earth.', 136 'Earth is the only planet not named after a god.', 137 'Jupiter has the shortest day of all the planets.', 138 'The Milky Way galaxy will collide with the Andromeda Galaxy in about 5 billion years.', 139 'The Sun contains 99.86% of the mass in the Solar System.', 140 'The Sun is an almost perfect sphere.', 141 'A total solar eclipse can happen once every 1 to 2 years. This makes them a rare event.', 142 'Saturn radiates two and a half times more energy into space than it receives from the sun.', 143 'The temperature inside the Sun can reach 15 million degrees Celsius.', 144 'The Moon is moving approximately 3.8 cm away from our planet every year.', 145]; 146 147const skillBuilder = Alexa.SkillBuilders.standard(); 148 149exports.handler = skillBuilder 150 .addRequestHandlers( 151 LaunchRequestHandler, 152 // GetNewFactHandler, 153 HelpHandler, 154 ExitHandler, 155 SessionEndedRequestHandler 156 ) 157 .addErrorHandlers(ErrorHandler) 158 .lambda();

レスポンス

{ "version": "1.0", "session": { "new": false, "sessionId": "amzn1.echo-api.session.c6c70564-b3a6-483b-9fb1-3a5c81bacccc", "application": { "applicationId": "amzn1.ask.skill.0069cc67-8e5a-4305-9f9d-5c3d433d96d6" }, "user": { "userId": "個人情報なので省略", "accessToken": "個人情報なので省略" } }, "context": { "System": { "application": { "applicationId": "amzn1.ask.skill.0069cc67-8e5a-4305-9f9d-5c3d433d96d6" }, "user": { "userId": "個人情報なので省略", "accessToken": "個人情報なので省略" }, "device": { "deviceId": "個人情報なので省略", "supportedInterfaces": {} }, "apiEndpoint": "https://api.fe.amazonalexa.com", "apiAccessToken": "個人情報なので省略" }, "Viewport": { "experiences": [ { "arcMinuteWidth": 246, "arcMinuteHeight": 144, "canRotate": false, "canResize": false } ], "shape": "RECTANGLE", "pixelWidth": 1024, "pixelHeight": 600, "dpi": 160, "currentPixelWidth": 1024, "currentPixelHeight": 600, "touch": [ "SINGLE" ], "video": { "codecs": [ "H_264_42", "H_264_41" ] } } }, "request": { "type": "SessionEndedRequest", "requestId": "amzn1.echo-api.request.e911134b-5bf9-416e-b594-33fe204658eb", "timestamp": "2019-09-13T00:21:58Z", "locale": "ja-JP", "reason": "ERROR", "error": { "type": "INVALID_RESPONSE", "message": "An exception occurred while dispatching the request to the skill." } } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2019/09/12 13:46

コードを提示しましょう
papinianus

2019/09/12 23:11

> このエラーはLambdaの設定で、ハンドラの名前がindex.handlerになってないこと(index.jsの場合)が原因らしい これ出典教えて下さい。むしろexports.handlerにしてないから発生したエラーに見えますが。
yuta_kg

2019/09/13 02:26 編集

index.jsのコードを追記しました。 index.handlerにする、と書いてあったのは以下の記事にです。 https://qiita.com/tom_furu/items/1750cc5ca28b76da11f1 また、exports.handlerにして実行してみたのですが同様のエラーが発生しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問