Cloudinary + netlify functions — `ENOENT bridge.js`

This is in local dev, I haven’t pushed it to netlify yet.

See also: Unknown bridge.js error when using Cloudinary in a Netlify function

Dev command:

  "scripts": {
    "start": "concurrently --kill-others \"NODE_ENV=dev vite\" \"NODE_ENV=dev npx ntl functions:serve --port=9999 --debug\"",
}
errorMessage":"ENOENT: no such file or directory, open '/Users/nick/code/real-hermes/.netlify/functions-serve/cloudinary/src/netlify/functions/cloudinary/bridge.js'","errorType":"Error","level":"error","stackTrace":["Object.openSync (node:fs:589:3)","Object.readFileSync (node:fs:457:35)","node_modules/vm2/lib/vm.js (/Users/nick/code/real-hermes/node_modules/vm2/lib/vm.js:146:60)","__require (/Users/nick/code/real-hermes/.netlify/functions-serve/cloudinary/src/netlify/functions/cloudinary/cloudinary.js:25:50)","node_modules/vm2/lib/main.js (/Users/nick/code/real-hermes/node_modules/vm2/lib/main.js:11:5)","__require (/Users/nick/code/real-hermes/.netlify/functions-serve/cloudinary/src/netlify/functions/cloudinary/cloudinary.js:25:50)","node_modules/vm2/index.js (/Users/nick/code/real-hermes/node_modules/vm2/index.js:3:18)","__require (/Users/nick/code/real-hermes/.netlify/functions-serve/cloudinary/src/netlify/functions/cloudinary/cloudinary.js:25:50)","node_modules/degenerator/dist/src/index.js (/Users/nick/code/real-hermes/node_modules/degenerator/src/index.ts:6:1)","__require (/Users/nick/code/real-hermes/.netlify/functions-serve/cloudinary/src/netlify/functions/cloudinary/cloudinary.js:25:50)"]}

Confusing because I don’t see any reference to bridge.js in cloudinary or my own code.

As in the referenced issue, we see this crash any time we call cloudinary.config

This is a new issue that started today, unrelated to any changes that I see.

If we look at vm2/lib/vm.js:146 like it says in the error message, we see

const bridgeScript = compileScript(`${__dirname}/bridge.js`,
	`(function(global) {"use strict"; const exports = {};${fs.readFileSync(`${__dirname}/bridge.js`, 'utf8')}\nreturn exports;})`);

This part stands out

fs.readFileSync(`${__dirname}/bridge.js`

In the error message we see

ENOENT: no such file or directory, open
'/Users/nick/code/real-hermes/.netlify/functions-serve/cloudinary/src/netlify/functions/cloudinary/bridge.js'

The folder

.netlify/functions-serve/cloudinary/src/netlify/functions/cloudinary

does exist, but there is no file bridge.js inside it.

However, there is a bridge.js in node_modules/vm2/lib/. It looks like it is using the wrong __dirname.

Looks like it was an issue with a dependency not being installed. The error message was confusing.

@nichoth How did you solve this?

I solved this by adding “vm2” to external_node_modules in my netlify.toml file

[functions]
    external_node_modules = ["express", "vm2"]
    node_bundler = "esbuild"
[[redirects]]
    force = true
    from = "/api/*"
    status = 200
    to = "/.netlify/functions/api/:splat"
1 Like

thank you for sharing your solution! This is definitely helpful for other users :+1:t4:

I think it was just a matter of doing npm i again in the directory.