Serverless function can't be deployed

Since rewriting my proxy has not been sucessful, I have tried to proxy this API with a small serverless function.

This trows an error:

6:25:00 PM:   Error message
6:25:00 PM:   A Netlify Function failed to require one of its dependencies.
6:25:00 PM:   Please make sure it is present in the site's top-level "package.json".
​
6:25:00 PM:   In file "/opt/build/repo/functions/api/api.js"
6:25:00 PM:   Cannot find module 'accepts'

This is the function.

'use strict';
const express = require('express');
const axios = require('axios').default;
const serverless = require('serverless-http');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.json());
const router = express.Router();

const api = process.env.VITE_APP_API;
const apiToken = process.env.VITE_APP_API_TOKEN;

router.use('/', async (req, res) => {

  const options = {
    method: 'GET',
    url: `/api${req.path}`,
    baseURL: api,
    headers: {
      'Authorization': `Bearer ${apiToken}`
    }
  }

  const response = await axios(options)


  res.json(response.data);
});

app.use(`/api`, router); // this is because we use a rewrite in the config

module.exports = app;
module.exports.handler = serverless(app);

Everything goes smooth, but trying to deploy the function, this happens:

1:59:31 PM: ────────────────────────────────────────────────────────────────
11:59:31 PM:   2. Functions bundling                                         
11:59:31 PM: ────────────────────────────────────────────────────────────────
11:59:31 PM: ​
11:59:31 PM: Packaging Functions from functions directory:
11:59:31 PM:  - api/api.js
11:59:31 PM: ​
11:59:31 PM: ​
11:59:31 PM: ────────────────────────────────────────────────────────────────
11:59:31 PM:   Dependencies installation error                               
11:59:31 PM: ────────────────────────────────────────────────────────────────
11:59:31 PM: ​
11:59:31 PM:   Error message
11:59:31 PM:   A Netlify Function failed to require one of its dependencies.
11:59:31 PM:   Please make sure it is present in the site's top-level "package.json".
​
11:59:31 PM:   In file "/opt/build/repo/functions/api/api.js"
11:59:31 PM:   Cannot find module 'accepts'
11:59:31 PM:   Require stack:
11:59:31 PM:   - /opt/buildhome/node-deps/node_modules/@netlify/zip-it-and-ship-it/dist/runtimes/node/bundlers/zisi/resolve.js
11:59:31 PM: ​

Dependency list:

  "dependencies": {
    "@types/markdown-it": "^12.2.3",
    "@vueuse/core": "^8.6.0",
    "@vueuse/head": "^0.7.6",
    "axios": "^0.27.2",
    "body-parser": "^1.20.0",
    "bootstrap-icons-vue": "^1.8.1",
    "express": "^4.18.1",
    "markdown-it": "^13.0.1",
    "serverless-http": "^3.0.1",
    "vite-plugin-md": "^0.14.0",
    "vue": "^3.2.36",
    "vue-matomo": "^4.1.0",
    "vue-router": "^4.0.15"
  },

PS: local netlify dev works

Hey @campino2k

Are you trying to proxy to another service via a function? What I see is a function which will get deployed to /.netlify/function/api which then makes a GET request to the path /api. If /api is pointing to another service with a redirect in either a _redirects or netlify.toml file, I see the function as simply adding another unnecessary layer.

If I am incorrect can you explain further the what and how.

Thanks for replying so quick. I had tried to achieve proxying my API and /api exists on target Now I have used a function to achieve this and local everything works like a charm but deployment fails.

A far simpler method is to proxy to another server with [custom headers](custom headers in proxy redirects) like in your previous post e.g.

[[redirects]]
  from = "/api/*"
  to = "https://my-secret-api.tld/:splat"
  status = 200
  force = true
  headers = {Authorization = "Bearer <SOME_TOKEN>"}

In your app you use a standard async fetch() request to /api e.g.

async fetch("/api/end/point").then(...)

Sure. But the headers were not sent correctly. Just as described in my other post. I’m really annoyed. Proxy Settings work local as they should, per lambda as well as proxy setting via netlify.toml

Build preview fails on deployment in both cases.

If you are using the aforementioned rewrite proxy, you don’t need the internal Vue/Vite proxy outlined in your previous thread. Have you tried removing it?