ExpressJS Netlify App Cannot Build whatsoever

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"

My package.json:

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "NODE_OPTIONS='--openssl-legacy-provider' netlify-lambda build src"
  },  
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@netlify/functions": "^2.6.0",
    "@types/express": "^4.17.21",
    "express": "^4.19.2",
    "serverless-http": "^3.2.0"
  }
}

Failed Build Log:

7:46:29 PM: Netlify Build                                                 
7:46:29 PM: ────────────────────────────────────────────────────────────────
7:46:29 PM: ​
7:46:29 PM: ❯ Version
7:46:29 PM:   @netlify/build 29.39.1
7:46:29 PM: ​
7:46:29 PM: ❯ Flags
7:46:29 PM:   baseRelDir: true
7:46:29 PM:   buildId: 661c164c0db87598abab84dd
7:46:29 PM:   deployId: 661c164c0db87598abab84df
7:46:29 PM: ​
7:46:29 PM: ❯ Current directory
7:46:29 PM:   /opt/build/repo
7:46:29 PM: ​
7:46:29 PM: ❯ Config file
7:46:29 PM:   /opt/build/repo/netlify.toml
7:46:29 PM: ​
7:46:29 PM: ❯ Context
7:46:29 PM:   production
7:46:29 PM: ​
7:46:29 PM: Build command from Netlify app                                
7:46:29 PM: ────────────────────────────────────────────────────────────────
7:46:29 PM: ​
7:46:29 PM: $ npm run build
7:46:29 PM: > LynexnServer@1.0.0 build
7:46:29 PM: > ./node_modules/.bin/netlify-lambda build src
7:46:29 PM: netlify-lambda: Building functions
7:46:30 PM: [
7:46:30 PM:   Error: api.js from Terser
7:46:30 PM:   Error: error:0308010C:digital envelope routines::unsupported
7:46:30 PM:       at new Hash (node:internal/crypto/hash:69:19)
7:46:30 PM:       at Object.createHash (node:crypto:133:10)
7:46:30 PM:       at /opt/build/repo/node_modules/terser-webpack-plugin/dist/index.js:217:37
7:46:30 PM:       at Array.forEach (<anonymous>)
7:46:30 PM:       at TerserPlugin.optimizeFn (/opt/build/repo/node_modules/terser-webpack-plugin/dist/index.js:160:259)
7:46:30 PM:       at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:7:1)
7:46:30 PM:       at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
7:46:30 PM:       at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1409:36
7:46:30 PM:       at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
7:46:30 PM:       at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
7:46:30 PM:       at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1405:32
7:46:30 PM:       at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
7:46:30 PM:       at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
7:46:30 PM:       at Compilation.seal (/opt/build/repo/node_modules/webpack/lib/Compilation.js:1342:27)
7:46:30 PM:       at /opt/build/repo/node_modules/webpack/lib/Compiler.js:675:18
7:46:30 PM:       at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1261:4
7:46:30 PM:       at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:24:1)
7:46:30 PM:       at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
7:46:30 PM:       at Compilation.finish (/opt/build/repo/node_modules/webpack/lib/Compilation.js:1253:28)
7:46:30 PM:       at /opt/build/repo/node_modules/webpack/lib/Compiler.js:672:17
7:46:30 PM:       at eval (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
7:46:30 PM:       at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1185:12
7:46:30 PM:       at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1097:9
7:46:30 PM:       at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
7:46:30 PM:       at TerserPlugin.buildError (/opt/build/repo/node_modules/terser-webpack-plugin/dist/index.js:105:14)
7:46:30 PM:       at /opt/build/repo/node_modules/terser-webpack-plugin/dist/index.js:224:48
7:46:30 PM:       at Array.forEach (<anonymous>)
7:46:30 PM:       at TerserPlugin.optimizeFn (/opt/build/repo/node_modules/terser-webpack-plugin/dist/index.js:160:259)
7:46:30 PM:       at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:7:1)
7:46:30 PM:       at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
7:46:30 PM:       at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1409:36
7:46:30 PM:       at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
7:46:30 PM:       at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
7:46:30 PM:       at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1405:32
7:46:30 PM:       at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
7:46:30 PM:       at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
7:46:30 PM:       at Compilation.seal (/opt/build/repo/node_modules/webpack/lib/Compilation.js:1342:27)
7:46:30 PM:       at /opt/build/repo/node_modules/webpack/lib/Compiler.js:675:18
7:46:30 PM:       at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1261:4
7:46:30 PM:       at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:24:1)
7:46:30 PM:       at AsyncSeriesHook.lazyCompileHook (/opt/build/repo/node_modules/tapable/lib/Hook.js:154:20)
7:46:30 PM:       at Compilation.finish (/opt/build/repo/node_modules/webpack/lib/Compilation.js:1253:28)
7:46:30 PM:       at /opt/build/repo/node_modules/webpack/lib/Compiler.js:672:17
7:46:30 PM:       at eval (eval at create (/opt/build/repo/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
7:46:30 PM:       at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1185:12
7:46:30 PM:       at /opt/build/repo/node_modules/webpack/lib/Compilation.js:1097:9
7:46:30 PM:       at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
7:46:30 PM: ]
7:46:30 PM: ​
7:46:30 PM: "build.command" failed                                        
7:46:30 PM: ────────────────────────────────────────────────────────────────
7:46:30 PM: ​
7:46:30 PM:   Error message
7:46:30 PM:   Command failed with exit code 1: npm run build (https://ntl.fyi/exit-code-1)
7:46:30 PM: ​
7:46:30 PM:   Error location
7:46:30 PM:   In Build command from Netlify app:
7:46:30 PM:   npm run build
7:46:30 PM: ​
7:46:30 PM:   Resolved config
7:46:30 PM:   build:
7:46:30 PM:     command: npm run build
7:46:30 PM:     commandOrigin: ui
7:46:30 PM:     publish: /opt/build/repo
7:46:30 PM:     publishOrigin: default
7:46:30 PM:   functionsDirectory: /opt/build/repo/functions
7:46:31 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
7:46:31 PM: Failing build: Failed to build site
7:46:31 PM: Finished processing build request in 29.852s
7:46:31 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)

Why are you using a deprecated package? The guide that you linked doesn’t mention that.

It was one of the “fixes” i found somewhere, as the guide also doesnt provide the build script, but it didnt work anyway.

It does: Express on Netlify | Netlify Docs. Quoting:

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": "NODE_OPTIONS='--openssl-legacy-provider' build src"

New netlify.toml:

[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

Hi there,

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:

"dependencies": {
    
    
"@netlify/functions": "^2.6.0",
    
    
"@types/express": "^4.17.21",
    
    
"express": "^4.19.2",
    
    
"serverless-http": "^3.2.0"
    
}

Your toml file should then look something like:

[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!