neverendingqs / netlify-express

Express.js hosted on Netlify
https://netlify-express.netlify.app/
Apache License 2.0
321 stars 341 forks source link

Function invocation failed: TypeError: require is not a function error #8

Closed jerhadf closed 5 years ago

jerhadf commented 5 years ago

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?

neverendingqs commented 5 years ago

I don't have an exact answer for you, and I only had a chance to take a quick look, but I noticed that your Vue code and your Express code are wrapped together. Those two should probably be separated, as the Vue code is for the front-end, and the Express code is for the back-end.

Also, npm run build only builds the Vue code. You need to build the lambda code as well by running netlify-lambda build at some point, and point your functions config in the toml file to the output of the lambda code build.