vercel: Serverless Function crashing due to `Cannot find module '/var/task/backend/node_modules/axios/dist/node/axios.cjs'`

Discussed in https://github.com/vercel/vercel/discussions/9637

<div type='discussions-op-text'>

Originally posted by cmgchess March 9, 2023 Hello 👋 I have a serverless function and it works locally using vercel dev but fails when I deploy it.
Here is the related repo. I’m getting the error

Cannot find module '/var/task/backend/node_modules/axios/dist/node/axios.cjs'
Did you forget to add it to "dependencies" in `package.json`?
RequestId: f00c415d-275c-4c1e-b6a0-e9546687763e Error: Runtime exited with error: exit status 1
Runtime.ExitError

I have the following dependencies including axios

  "devDependencies": {
    "@vercel/node": "^2.9.12"
  },
  "dependencies": {
    "axios": "^1.3.3",
    "cheerio": "^1.0.0-rc.12"
  }

I use Node 18 on Vercel. I’m not sure what’s causing this since I can find the axios.cjs file when I check my node_modules folder Also created a reproducible demo https://github.com/cmgchess/vercel-serverless-axios-issue

Help much appreciated 🙏

image image image

</div>

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 2
  • Comments: 15 (5 by maintainers)

Most upvoted comments

OK, I’ve done some investigation. axios@1.3.3 is an ESM module which requires you to add "type": "module" to the package.json. That should fix deploying.

vc dev support for ESM is a little spotty. I have a PR #9692 that will address all known issues with using ESM dependencies. I’m hoping to land that PR “soon”.

Hello, I’m sorry but I don’t understand how this is a solution. This would require me to change imports throughout the whole app, wouldn’t it?

Btw. I still have this error on my Node.js API

Cannot find module '/var/task/node_modules/axios/dist/node/axios.cjs' Did you forget to add it to "dependencies" in package.json? RequestId: ab697e49-3105-49cf-a678-11d1c20b30ef Error: Runtime exited with error: exit status 1 Runtime.ExitError

OK, I’ve done some investigation. axios@1.3.3 is an ESM module which requires you to add "type": "module" to the package.json. That should fix deploying.

vc dev support for ESM is a little spotty. I have a PR https://github.com/vercel/vercel/pull/9692 that will address all known issues with using ESM dependencies. I’m hoping to land that PR “soon”.

I’m able to reproduce. Thank you for the test case. I’ve asked the team to see what they make of the issue. Stay tuned.

Reopening to further investigate 😃