I'm building a web app with Vue, which will use Netlify Lambda functions and Express.js as the server-side. I have followed this repo's structure almost exactly, and I'm following the "How to run Express.js with Netlify functions" guide as well. I am also using the Netlify Lambda plugin for Vue CLI to integrate the lambda functions into my Vue build process.
I have a small lambda function called server.js. When this function is called, it will grab data from the Spotify API.
'use strict';
const serverless = require("serverless-http");
const SpotifyWebApi = require("spotify-web-api-node");
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const router = express.Router();
router.get("/search", (req, res) => {
// function that searches for a song with the Spotify API
});
app.use(bodyParser.json());
app.use('/.netlify/functions/server', router); // path must route to lambda
module.exports = app;
module.exports.handler = serverless(app);
This should work - it follows the pattern in the server.js file in this repo. I create an Express app, add a GET endpoint called search to the Express API, and then export the Express app as the handler for the Netlify Lambda functions.
However, I keep getting this cryptic, vague error whenever I navigate to the url for the Lambda function: TypeError: a is not a function. I have researched this error and tried a bunch of the hotfixes and workarounds, but they haven't fixed the issue. Here is the full stack trace:
{"errorMessage":"a is not a function","errorType":"TypeError","stackTrace":["i (/var/task/server.js:1:220)","Object.<anonymous> (/var/task/server.js:314:35760)","i (/var/task/server.js:1:220)","Object.e.exports.a.debug (/var/task/server.js:159:7043)","i (/var/task/server.js:1:220)","Object.<anonymous> (/var/task/server.js:159:5636)","i (/var/task/server.js:1:220)","Object.<anonymous> (/var/task/server.js:314:13004)","i (/var/task/server.js:1:220)"]}
If I run the server locally, I get this error instead:
Function invocation failed: TypeError: require is not a function
If this question does not have enough detail or you want to look at my config files or my exact function, feel free to explore the Github repo for my small web app: https://github.com/vintners/music-calc-app
Also, this is the URL for the server.js Lambda function on my Netlify site: https://happy-goodall-8bd7c9.netlify.com/.netlify/functions/server
Is my Lambda function set up incorrectly?