Hey,
I’ve created an Express JS app on netlify exactly how it’s written in the netlify expressjs guide, but it does not build whatsoever.
Ive tried:
Downgrading the Node version to 16 and to 14.18.1 via nvm
Adding NODE_OPTIONS='--openssl-legacy-provider' to my build script
Debugging with the netlify.toml
Searching through other forum posts, but no luck.
Please correct me if I’m missing anything or doing anything wrong, because i just can’t get it to work
My netlify.toml:
[functions]
external_node_modules = ["express"]
node_bundler = "esbuild"
[[redirects]]
force = true
from = "/api/*"
status = 200
to = "/.netlify/functions/api/:splat"
In the netlify.toml or the Netlify UI, set a placeholder build command to ensure Netlify builds your functions. For example, the command could be echo Building Functions.
The guide works when followed exactly as it is. I think it’s not the best idea to link us to a guide you said you followed, but present us with an error from a different guide. If you ran into an issue with the official guide, please provide us details about that error.
Sorry about that, i must’ve missed that part;
Having set the new command in the netlify.toml, the error still occurs for a reason that i’m not aware of. Is the build script in the package.json correct? I was told from another thread to add the NODE_OPTIONS='--openssl-legacy-provider' as a prefix before the script, but it had no effect, so I’m really not sure what to do now.
[build]
publish = "build" # This should be the path to your build directory
command = "echo Building Functions"
[functions]
directory = "netlify/functions" # This should be the path to your functions directory
[[redirects]]
from = "/api/*"
to = "/.netlify/functions/api/:splat"
status = 200
force = true
Failed Build Log
8:38:39 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
8:38:37 PM: Netlify Build
8:38:37 PM: ────────────────────────────────────────────────────────────────
8:38:37 PM:
8:38:37 PM: ❯ Version
8:38:37 PM: @netlify/build 29.39.1
8:38:37 PM:
8:38:37 PM: ❯ Flags
8:38:37 PM: baseRelDir: true
8:38:37 PM: buildId: 661c22822a2032a33ea4b212
8:38:37 PM: deployId: 661c22822a2032a33ea4b214
8:38:37 PM:
8:38:37 PM: ❯ Current directory
8:38:37 PM: /opt/build/repo
8:38:37 PM:
8:38:37 PM: ❯ Config file
8:38:37 PM: /opt/build/repo/netlify.toml
8:38:37 PM:
8:38:37 PM: ❯ Context
8:38:37 PM: production
8:38:37 PM:
8:38:37 PM: Build command from Netlify app
8:38:37 PM: ────────────────────────────────────────────────────────────────
8:38:37 PM:
8:38:37 PM: $ npm run build
8:38:37 PM: > LynexnServer@1.0.0 build
8:38:37 PM: > ./node_modules/.bin/netlify-lambda build src
8:38:38 PM: netlify-lambda: Building functions
8:38:39 PM: [
8:38:39 PM: Error: api.js from Terser
8:38:39 PM: Error: error:0308010C:digital envelope routines::unsupported
8:38:39 PM: at new Hash (node:internal/crypto/hash:69:19)
8:38:39 PM: at Object.createHash (node:crypto:133:10)
8:38:39 PM: at /opt/build/repo/node_modules/terser-webpack-plugin/dist/index.js:217:37
8:38:39 PM: at Array.forEach (<anonymous>)
8:38:39 PM: at TerserPlugin.optimizeFn (/opt/build/repo/node_modules/terser-webpack-plugin/dist/index.js:160:259)
8:38:39 PM: at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:7:1)
8:38:39 PM: at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
8:38:39 PM: at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1409:36
8:38:39 PM: at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
8:38:39 PM: at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
8:38:39 PM: at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1405:32
8:38:39 PM: at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
8:38:39 PM: at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
8:38:39 PM: at Compilation.seal (/opt/build/repo/node_modules/webpack/lib/Compilation.js:1342:27)
8:38:39 PM: at /opt/build/repo/node_modules/webpack/lib/Compiler.js:675:18
8:38:39 PM: at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1261:4
8:38:39 PM: at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:24:1)
8:38:39 PM: at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
8:38:39 PM: at Compilation.finish (/opt/build/repo/node_modules/webpack/lib/Compilation.js:1253:28)
8:38:39 PM: at /opt/build/repo/node_modules/webpack/lib/Compiler.js:672:17
8:38:39 PM: at eval (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
8:38:39 PM: at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1185:12
8:38:39 PM: at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1097:9
8:38:39 PM: at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
8:38:39 PM: at TerserPlugin.buildError (/opt/build/repo/node_modules/terser-webpack-plugin/dist/index.js:105:14)
8:38:39 PM: at /opt/build/repo/node_modules/terser-webpack-plugin/dist/index.js:224:48
8:38:39 PM: at Array.forEach (<anonymous>)
8:38:39 PM: at TerserPlugin.optimizeFn (/opt/build/repo/node_modules/terser-webpack-plugin/dist/index.js:160:259)
8:38:39 PM: at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:7:1)
8:38:39 PM: at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
8:38:39 PM: at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1409:36
8:38:39 PM: at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
8:38:39 PM: at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
8:38:39 PM: at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1405:32
8:38:39 PM: at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
8:38:39 PM: at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
8:38:39 PM: at Compilation.seal (/opt/build/repo/node_modules/webpack/lib/Compilation.js:1342:27)
8:38:39 PM: at /opt/build/repo/node_modules/webpack/lib/Compiler.js:675:18
8:38:39 PM: at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1261:4
8:38:39 PM: at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:24:1)
8:38:39 PM: at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
8:38:39 PM: at Compilation.finish (/opt/build/repo/node_modules/webpack/lib/Compilation.js:1253:28)
8:38:39 PM: at /opt/build/repo/node_modules/webpack/lib/Compiler.js:672:17
8:38:39 PM: at eval (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
8:38:39 PM: at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1185:12
8:38:39 PM: at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1097:9
8:38:39 PM: at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
8:38:39 PM: ]
8:38:39 PM:
8:38:39 PM: "build.command" failed
8:38:39 PM: ────────────────────────────────────────────────────────────────
8:38:39 PM:
8:38:39 PM: Error message
8:38:39 PM: Command failed with exit code 1: npm run build (https://ntl.fyi/exit-code-1)
8:38:39 PM:
8:38:39 PM: Error location
8:38:39 PM: In Build command from Netlify app:
8:38:39 PM: npm run build
8:38:39 PM:
8:38:39 PM: Resolved config
8:38:39 PM: build:
8:38:39 PM: command: npm run build
8:38:39 PM: commandOrigin: ui
8:38:39 PM: publish: /opt/build/repo
8:38:39 PM: publishOrigin: default
8:38:39 PM: functionsDirectory: /opt/build/repo/functions
8:38:39 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
8:38:39 PM: Failing build: Failed to build site
8:38:39 PM: Finished processing build request in 31.19s
Thanks for following up on this one, and apologies again for the issues here!
As Hrishikesh mentioned, your best bet as far as the optimal config here would be to follow the example in the docs precisely: you’ll want to remove that script from package.json, for example. For posterity, the only deps that get installed when running npm i express serverless-http @netlify/functions @types/express are:
[build]
command = "echo Building Functions"
[functions]
external_node_modules = ["express"]
node_bundler = "esbuild"
[[redirects]]
force = true
from = "/api/*"
status = 200
to = "/.netlify/functions/api/:splat"
If you've followed the docs exactly and this still isn't working, please provide a repo where this is replicable, and we'd be happy to take a look. Thanks again!