alexa-js / alexa-app-server

An Alexa app server for alexa-app.
MIT License
401 stars 116 forks source link

`TypeError: undefined is not a function` on express.post #82

Closed xMTinkerer closed 7 years ago

xMTinkerer commented 7 years ago

Ok, so I have alexa-app-server server.js like so:

var AlexaAppServer = require('alexa-app-server');

var instance = AlexaAppServer.start({
  server_root: __dirname,     // Path to root
  public_html: "public_html", // Static content
  app_dir: "apps",            // Location of alexa-app modules
  app_root: "/alexa/",        // Service root
  port: 8080,                  // Port to use

  httpsPort: 8081,
  httpsEnabled: true,
  privateKey: 'private-key.pem',
  certificate: 'cert.cer',

  debug: true,
  verify: false
});

And then in ./apps/hello_world/index.js, I have:

var alexa = require('alexa-app');

// Allow this module to be reloaded by hotswap when changed
module.change_code = 1;

// Define an alexa-app
var app = new alexa.app('hello_world');
app.launch(function(req, res) {
  res.say("Hello World!!");
});

app.intent('NameIntent', {
  "slots": { "NAME": "LITERAL", "AGE": "NUMBER" },
  "utterances": ["{My name is|my name's} {matt|bob|bill|jake|nancy|mary|jane|NAME} and I am {1-100|AGE}{ years old|}"]
}, function(req, res) {
  res.say('Your name is ' + req.slot('NAME') + ' and you are ' + req.slot('AGE') + ' years old');
});

app.intent('AgeIntent', {
  "slots": { "AGE": "NUMBER" },
  "utterances": ["My age is {1-100|AGE}"]
}, function(req, res) {
  res.say('Your age is ' + req.slot('AGE'));
});

module.exports = app;

But when I start the server I get this stack trace complaining about express.post:

serving static content from: /home/ec2-user/Tinkering/xAlexa/public_html
loading server-side modules from: /home/ec2-user/Tinkering/xAlexa/server
   loaded /home/ec2-user/Tinkering/xAlexa/server/login.js
loading apps from: /home/ec2-user/Tinkering/xAlexa/apps
/home/ec2-user/Tinkering/xAlexa/apps/hello_world/node_modules/alexa-app/index.js:491
    express.post(endpoint, function(req, res) {
            ^
TypeError: undefined is not a function
    at Object.express (/home/ec2-user/Tinkering/xAlexa/apps/hello_world/node_modules/alexa-app/index.js:491:13)
    at /home/ec2-user/Tinkering/xAlexa/node_modules/alexa-app-server/index.js:122:11
    at Array.forEach (native)
    at Object.self.load_apps (/home/ec2-user/Tinkering/xAlexa/node_modules/alexa-app-server/index.js:82:30)
    at Object.self.start (/home/ec2-user/Tinkering/xAlexa/node_modules/alexa-app-server/index.js:188:12)
    at Function.appServer.start (/home/ec2-user/Tinkering/xAlexa/node_modules/alexa-app-server/index.js:295:21)
    at Object.<anonymous> (/home/ec2-user/Tinkering/xAlexa/server.js:3:31)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

I tried installing express then updating ./apps/hello_world/index.js to be:

var express = require( 'express' );
var alexa = require('alexa-app');
var express_app = express();

// Allow this module to be reloaded by hotswap when changed
module.change_code = 1;

// Define an alexa-app
var app = new alexa.app('hello_world');
app.launch(function(req, res) {
  res.say("Hello World!!");
});

app.intent('NameIntent', {
  "slots": { "NAME": "LITERAL", "AGE": "NUMBER" },
  "utterances": ["{My name is|my name's} {matt|bob|bill|jake|nancy|mary|jane|NAME} and I am {1-100|AGE}{ years old|}"]
}, function(req, res) {
  res.say('Your name is ' + req.slot('NAME') + ' and you are ' + req.slot('AGE') + ' years old');
});

app.intent('AgeIntent', {
  "slots": { "AGE": "NUMBER" },
  "utterances": ["My age is {1-100|AGE}"]
}, function(req, res) {
  res.say('Your age is ' + req.slot('AGE'));
});

app.express({ expressApp: express_app });
module.exports = app;

And this starts the server, but the app does not load:

serving static content from: /home/ec2-user/Tinkering/xAlexa/public_html
loading server-side modules from: /home/ec2-user/Tinkering/xAlexa/server
   loaded /home/ec2-user/Tinkering/xAlexa/server/login.js
loading apps from: /home/ec2-user/Tinkering/xAlexa/apps
   error loading app [/home/ec2-user/Tinkering/xAlexa/apps/hello_world/index.js]: TypeError: undefined is not a function
enabling https
listening on https port 8081
listening on http port 8080

Any ideas?

dblock commented 7 years ago

If you're using alexa-app server, remove all references to express in the function/app itself. I think the call to app.express() causes app.express to be redefined or something like that.

Reopen if that doesn't solve it.

xMTinkerer commented 7 years ago

hmm, no dice. This is my entire server.js:

var AlexaAppServer = require('alexa-app-server');

var instance = AlexaAppServer.start({
  server_root: __dirname,     // Path to root
  public_html: "public_html", // Static content
  app_dir: "apps",            // Location of alexa-app modules
  app_root: "/alexa/",        // Service root
  port: 8080,                  // Port to use

  httpsPort: 8081,
  httpsEnabled: true,
  privateKey: 'private-key.pem',
  certificate: 'cert.cer',

  debug: true,
  verify: false
});
dblock commented 7 years ago

Your ./apps/hello_world/index.js is what needs to change. Post the whole code if you can't make it work.

xMTinkerer commented 7 years ago

Ah, ok, sorry I misunderstood. This is ./apps/hello_world/index.js:

var alexa = require('alexa-app');

// Allow this module to be reloaded by hotswap when changed
module.change_code = 1;

// Define an alexa-app
var app = new alexa.app('hello_world');
app.launch(function(req, res) {
  res.say("Hello World!!");
});

app.intent('NameIntent', {
  "slots": { "NAME": "LITERAL", "AGE": "NUMBER" },
  "utterances": ["{My name is|my name's} {matt|bob|bill|jake|nancy|mary|jane|NAME} and I am {1-100|AGE}{ years old|}"]
}, function(req, res) {
  res.say('Your name is ' + req.slot('NAME') + ' and you are ' + req.slot('AGE') + ' years old');
});

app.intent('AgeIntent', {
  "slots": { "AGE": "NUMBER" },
  "utterances": ["My age is {1-100|AGE}"]
}, function(req, res) {
  res.say('Your age is ' + req.slot('AGE'));
});

module.exports = app;

This results in:

serving static content from: /home/ec2-user/Tinkering/xAlexa/public_html
loading server-side modules from: /home/ec2-user/Tinkering/xAlexa/server
   loaded /home/ec2-user/Tinkering/xAlexa/server/login.js
loading apps from: /home/ec2-user/Tinkering/xAlexa/apps
/home/ec2-user/Tinkering/xAlexa/apps/hello_world/node_modules/alexa-app/index.js:491
    express.post(endpoint, function(req, res) {
            ^
TypeError: undefined is not a function
    at Object.express (/home/ec2-user/Tinkering/xAlexa/apps/hello_world/node_modules/alexa-app/index.js:491:13)
    at /home/ec2-user/Tinkering/xAlexa/node_modules/alexa-app-server/index.js:122:11
    at Array.forEach (native)
    at Object.self.load_apps (/home/ec2-user/Tinkering/xAlexa/node_modules/alexa-app-server/index.js:82:30)
    at Object.self.start (/home/ec2-user/Tinkering/xAlexa/node_modules/alexa-app-server/index.js:188:12)
    at Function.appServer.start (/home/ec2-user/Tinkering/xAlexa/node_modules/alexa-app-server/index.js:295:21)
    at Object.<anonymous> (/home/ec2-user/Tinkering/xAlexa/server.js:3:31)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

Sorry, I must be missing something else.

dblock commented 7 years ago

Can you put this project on Github pls, I can take a look.

xMTinkerer commented 7 years ago

I ended up going with lambda. This is more of a research project, so I can stick with that for now. If I need to I'll pick this back up and put it on github per your request. Thanks for your help!

guikubivan commented 5 years ago

I had the same problem, and it was because I was symlinking the package.json file in the app folder to the parent alexa-app-server folder (still not sure how to use only one package.json file). At some point, I had ran npm install from the app directory, so the app was trying to use old versions of alexa-app/-server from the node_modules of that directory. To fix, just wipe node_modules from the app directory.