Axios file missing in production but not localhost

Here is the website: https://atlanticcabinetrefacing.netlify.app/

Here is my function that sends my emails through sendgrid. When I run it in local host, everything works perfectly fine.

import axios from 'axios';

export async function handler(event, context) {
  if (event.body === null) {
    return {
      statusCode: 400,
      body: JSON.stringify("Payload required"),
    };
  }

  const formData = JSON.parse(event.body);

  try {
    const response = await axios.post(`${process.env.URL}/.netlify/functions/emails/sendGridEmail`, {
      from: "placeholder@gmail.com",
      to: "placeholder@gmail.com",
      subject: "New Estimate Request",
      parameters: {
        name: formData.name,
        city: formData.city,
        state: formData.state,
        homePhone: formData.homePhone,
        cellPhone: formData.cellPhone,
        email: formData.email,
      },
    }, {
      headers: {
        "netlify-emails-secret": process.env.NETLIFY_EMAILS_SECRET,
      },
    });

    if (!response.status === 200) {
      return {
        statusCode: response.status,
        body: JSON.stringify(`Email failed to send: ${response.statusText}`),
      };
    }

    return {
      statusCode: 200,
      body: JSON.stringify("Email sent!"),
    };
  } catch (error) {
    return {
      statusCode: 500,
      body: JSON.stringify(`Server error: ${error}`)
    };
  }
};

I get this error

Runtime.ImportModuleError: Error: Cannot find module '/var/task/node_modules/axios/dist/node/axios.cjs'
    at _loadUserApp (file:///var/runtime/index.mjs:996:17)
    at async Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1031:21)
    at async start (file:///var/runtime/index.mjs:1194:23)
    at async file:///var/runtime/index.mjs:1200:1

However, it is clearly in my directory. The only thing that I think could be causing a problem would be that it starts the path with /var/task, and that is not in my normal directory. Either way, it is odd that it works through the netlify cli on my desktop and laptop but fails when it is live.

I have looked through this and this

However, this one may be the solution if I need to have a package.json inside of my functions folder since my directory looks like this currently

If I need to add a package.json, I’m not exactly sure how I would structure it.

I had also tried using node-fetch instead of axios, but I kept getting this error in production, which seemed to boil down to not being able to access fetch-blob. But this issue persisted even when I removed all references of fetch-blob from my files. So if there is a way to either alter it so that axios works or to go back to node-fetch that works without fetch-blob errors I would appreciate it greatly.

Apr 18, 05:08:14 PM: 257e3434 2023-04-18T21:08:14.830Z undefined ERROR Uncaught Exception {“errorType”:“TypeError”,“errorMessage”:“Class extends value # is not a constructor or null”,“stack”:[“TypeError: Class extends value # is not a constructor or null”," at Object. (/var/task/node_modules/fetch-blob/file.js:53:18)“,” at Module._compile (node:internal/modules/cjs/loader:1165:14)“,” at Object.Module._extensions…js (node:internal/modules/cjs/loader:1219:10)“,” at Module.load (node:internal/modules/cjs/loader:1043:32)“,” at Function.Module._load (node:internal/modules/cjs/loader:878:12)“,” at Module.require (node:internal/modules/cjs/loader:1067:19)“,” at require (node:internal/modules/cjs/helpers:103:18)“,” at Object. (/var/task/node_modules/formdata-polyfill/esm.min.js:29:27)“,” at Module._compile (node:internal/modules/cjs/loader:1165:14)“,” at Object.Module._extensions…js (node:internal/modules/cjs/loader:1219:10)“]}Apr 18, 05:08:14 PM: 257e3434 2023-04-18T21:08:15.111Z undefined ERROR Uncaught Exception {“errorType”:“TypeError”,“errorMessage”:“Class extends value # is not a constructor or null”,“stack”:[“TypeError: Class extends value # is not a constructor or null”,” at Object. (/var/task/node_modules/fetch-blob/file.js:53:18)“,” at Module._compile (node:internal/modules/cjs/loader:1165:14)“,” at Object.Module._extensions…js (node:internal/modules/cjs/loader:1219:10)“,” at Module.load (node:internal/modules/cjs/loader:1043:32)“,” at Function.Module._load (node:internal/modules/cjs/loader:878:12)“,” at Module.require (node:internal/modules/cjs/loader:1067:19)“,” at require (node:internal/modules/cjs/helpers:103:18)“,” at Object. (/var/task/node_modules/formdata-polyfill/esm.min.js:29:27)“,” at Module._compile (node:internal/modules/cjs/loader:1165:14)“,” at Object.Module._extensions…js (node:internal/modules/cjs/loader:1219:10)"]}Apr 18, 05:08:14 PM: 257e3434 Unknown application error occurredApr 18, 05:08:14 PM: 257e3434 Runtime.Unknown

Is axios in the package.json for the repository? Can you share a link to the public repository?

Axios is in package.json

Just checking in to see if you got a chance to look into this any more.

Could you try adding:

[functions]
  node_bundler = "esbuild"

to your netlify.toml?

That worked. Thank you!

i tried this but mine didnt work

4:32:29 PM: Netlify Build
4:32:29 PM: ────────────────────────────────────────────────────────────────
4:32:29 PM: ​
4:32:29 PM: ❯ Version
4:32:29 PM: @netlify/build 29.58.1
4:32:29 PM: ​
4:32:29 PM: ❯ Flags
4:32:29 PM: accountId: 6498d6a515317c5ab8be25c8
4:32:29 PM: baseRelDir: true
4:32:29 PM: buildId: 677aa5844f3ddf8db28228ca
4:32:29 PM: deployId: 677aa5844f3ddf8db28228cc
4:32:29 PM: ​
4:32:29 PM: ❯ Current directory
4:32:29 PM: /opt/build/repo/Frontend
4:32:29 PM: ​
4:32:29 PM: ❯ Config file
4:32:29 PM: /opt/build/repo/Frontend/netlify.toml
4:32:29 PM: ​
4:32:29 PM: ❯ Context
4:32:29 PM: production
4:32:29 PM: ​
4:32:29 PM: Build command from Netlify app
4:32:29 PM: ────────────────────────────────────────────────────────────────
4:32:29 PM: ​
4:32:29 PM: $ npm run build
4:32:29 PM: > frontend@0.0.0 build
4:32:29 PM: > vite build
4:32:29 PM: vite v5.4.10 building for production…
4:32:29 PM: transforming…
4:32:30 PM: ✓ 53 modules transformed.
4:32:30 PM: x Build failed in 622ms
4:32:30 PM: error during build:
4:32:30 PM: [vite]: Rollup failed to resolve import “axios” from “/opt/build/repo/admin/src/requestMethods.js”.
4:32:30 PM: This is most likely unintended because it can break your application at runtime.
4:32:30 PM: If you do want to externalize this module explicitly add it to
4:32:30 PM: build.rollupOptions.external
4:32:30 PM: at viteWarn (file:///opt/build/repo/Frontend/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:65589:17)
4:32:30 PM: at onwarn (file:///opt/build/repo/Frontend/node_modules/@vitejs/plugin-react/dist/index.mjs:280:9)
4:32:30 PM: at onRollupWarning (file:///opt/build/repo/Frontend/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:65619:5)
4:32:30 PM: at onwarn (file:///opt/build/repo/Frontend/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:65284:7)
4:32:30 PM: at file:///opt/build/repo/Frontend/node_modules/rollup/dist/es/shared/node-entry.js:19452:13
4:32:30 PM: at Object.logger [as onLog] (file:///opt/build/repo/Frontend/node_modules/rollup/dist/es/shared/node-entry.js:21178:9)
4:32:30 PM: at ModuleLoader.handleInvalidResolvedId (file:///opt/build/repo/Frontend/node_modules/rollup/dist/es/shared/node-entry.js:20067:26)
4:32:30 PM: at file:///opt/build/repo/Frontend/node_modules/rollup/dist/es/shared/node-entry.js:20025:26
4:32:30 PM: ​
4:32:30 PM: “build.command” failed
4:32:30 PM: ────────────────────────────────────────────────────────────────
4:32:30 PM: ​
4:32:30 PM: Error message
4:32:30 PM: Command failed with exit code 1: npm run build (Search results for '"non-zero exit code: 1"' - Netlify Support Forums)
4:32:30 PM: ​
4:32:30 PM: Error location
4:32:30 PM: In Build command from Netlify app:
4:32:30 PM: npm run build
4:32:30 PM: ​
4:32:30 PM: Resolved config
4:32:30 PM: build:
4:32:30 PM: base: /opt/build/repo/Frontend
4:32:30 PM: command: npm run build
4:32:30 PM: commandOrigin: ui
4:32:30 PM: publish: /opt/build/repo/Frontend/dist
4:32:30 PM: publishOrigin: ui
4:32:30 PM: functions:
4:32:30 PM: ‘*’:
4:32:30 PM: node_bundler: esbuild
4:32:30 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
4:32:30 PM: Failing build: Failed to build site
4:32:30 PM: Finished processing build request in 26.846s
4:32:30 PM: Failed during stage ‘building site’: Build script returned non-zero exit code: 2 (Search results for '"non-zero exit code: 2"' - Netlify Support Forums)

4:30:12 PM: Waiting for other deploys from your team to complete. Check the queue: Netlify

4:32:04 PM: build-image version: c3449036692ceba113a2b71302d561bccc8b6156 (focal)

4:32:04 PM: buildbot version: 6e5dfadf0eec598d7c9db5c1e005bfa46b6682c0

4:32:04 PM: Building without cache

4:32:04 PM: Starting to prepare the repo for build

4:32:04 PM: No cached dependencies found. Cloning fresh repo

4:32:04 PM: git clone --filter=blob:none GitHub - valanboy/sendit1: courier website

4:32:04 PM: Preparing Git Reference refs/heads/main

4:32:05 PM: Custom publish path detected. Proceeding with the specified path: ‘Frontend/dist’

4:32:06 PM: Starting to install dependencies

4:32:08 PM: mise python@3.13.1 install

4:32:08 PM: mise python@3.13.1 download cpython-3.13.1+20241219-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz

4:32:08 PM: mise python@3.13.1 extract cpython-3.13.1+20241219-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz

4:32:08 PM: mise python@3.13.1 python --version

4:32:08 PM: mise python@3.13.1 Python 3.13.1

4:32:08 PM: mise python@3.13.1 ✓ installed

4:32:08 PM: Python version set to 3.13

4:32:10 PM: Collecting pipenv

4:32:10 PM: Downloading pipenv-2024.4.0-py3-none-any.whl.metadata (19 kB)

4:32:10 PM: Collecting certifi (from pipenv)

4:32:10 PM: Downloading certifi-2024.12.14-py3-none-any.whl.metadata (2.3 kB)

4:32:11 PM: Collecting packaging>=22 (from pipenv)

4:32:11 PM: Downloading packaging-24.2-py3-none-any.whl.metadata (3.2 kB)

4:32:11 PM: Collecting setuptools>=67 (from pipenv)

4:32:11 PM: Downloading setuptools-75.6.0-py3-none-any.whl.metadata (6.7 kB)

4:32:11 PM: Collecting virtualenv>=20.24.2 (from pipenv)

4:32:11 PM: Downloading virtualenv-20.28.1-py3-none-any.whl.metadata (4.5 kB)

4:32:11 PM: Collecting distlib<1,>=0.3.7 (from virtualenv>=20.24.2->pipenv)

4:32:11 PM: Downloading distlib-0.3.9-py2.py3-none-any.whl.metadata (5.2 kB)

4:32:11 PM: Collecting filelock<4,>=3.12.2 (from virtualenv>=20.24.2->pipenv)

4:32:11 PM: Downloading filelock-3.16.1-py3-none-any.whl.metadata (2.9 kB)

4:32:11 PM: Collecting platformdirs<5,>=3.9.1 (from virtualenv>=20.24.2->pipenv)

4:32:11 PM: Downloading platformdirs-4.3.6-py3-none-any.whl.metadata (11 kB)

4:32:11 PM: Downloading pipenv-2024.4.0-py3-none-any.whl (3.0 MB)

4:32:11 PM: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.0/3.0 MB 35.6 MB/s eta 0:00:00

4:32:11 PM: Downloading packaging-24.2-py3-none-any.whl (65 kB)

4:32:11 PM: Downloading setuptools-75.6.0-py3-none-any.whl (1.2 MB)

4:32:12 PM: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 11.3 MB/s eta 0:00:00

4:32:12 PM: Downloading virtualenv-20.28.1-py3-none-any.whl (4.3 MB)

4:32:12 PM: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.3/4.3 MB 62.8 MB/s eta 0:00:00

4:32:12 PM: Downloading certifi-2024.12.14-py3-none-any.whl (164 kB)

4:32:12 PM: Downloading distlib-0.3.9-py2.py3-none-any.whl (468 kB)

4:32:12 PM: Downloading filelock-3.16.1-py3-none-any.whl (16 kB)

4:32:12 PM: Downloading platformdirs-4.3.6-py3-none-any.whl (18 kB)

4:32:12 PM: Installing collected packages: distlib, setuptools, platformdirs, packaging, filelock, certifi, virtualenv, pipenv

4:32:14 PM: Successfully installed certifi-2024.12.14 distlib-0.3.9 filelock-3.16.1 packaging-24.2 pipenv-2024.4.0 platformdirs-4.3.6 setuptools-75.6.0 virtualenv-20.28.1

4:32:14 PM: Attempting Ruby version 2.7.2, read from environment

4:32:15 PM: Using Ruby version 2.7.2

4:32:15 PM: Started restoring cached go cache

4:32:15 PM: Finished restoring cached go cache

4:32:18 PM: go version go1.19.13 linux/amd64

4:32:19 PM: Using PHP version 8.0

4:32:21 PM: v18.20.5 is already installed.

4:32:21 PM: Now using node v18.20.5 (npm v10.8.2)

4:32:21 PM: Enabling Node.js Corepack

4:32:21 PM: Started restoring cached build plugins

4:32:21 PM: Finished restoring cached build plugins

4:32:21 PM: Started restoring cached corepack dependencies

4:32:21 PM: Finished restoring cached corepack dependencies

4:32:21 PM: No npm workspaces detected

4:32:21 PM: Started restoring cached node modules

4:32:21 PM: Finished restoring cached node modules

4:32:21 PM: Installing npm packages using npm version 10.8.2

4:32:26 PM: added 424 packages, and audited 425 packages in 5s

4:32:26 PM: 143 packages are looking for funding

4:32:26 PM: run npm fund for details

4:32:26 PM: 2 vulnerabilities (1 low, 1 moderate)

4:32:26 PM: To address all issues, run:

4:32:26 PM: npm audit fix

4:32:26 PM: Run npm audit for details.

4:32:26 PM: npm packages installed

4:32:26 PM: Successfully installed dependencies

4:32:26 PM: Starting build script

4:32:28 PM: Detected 1 framework(s)

4:32:28 PM: “vite” at version “5.4.10”

4:32:28 PM: Section completed: initializing

Seems to be a common problem with Axios:

This doesn’t seem to be a Netlify issue. I also get this when running npm run build locally on your project. We can’t help you debug that.