Command failed with exit code 127: npm run build (Hugo + NextJS Plugin)

I have struggled with deployment for past 3 days and I tried everything I could. My site on hugo worked fine and I wanted to add NextJS to be able to link a form to a code which generates Word file. It has been a few days and I’m a total newbie but the flow seems to be HTML > NextJS > JavaScript > Python code generates Word file.

The latest error is this:

4:29:47 PM: $ npm run build
4:29:47 PM: > website_vf_2024_updated@1.0.0 build
4:29:47 PM: > next build && hugo
4:29:47 PM: sh: 1: next: not found
4:29:47 PM: ​
4:29:47 PM: “build.command” failed
4:29:47 PM: ────────────────────────────────────────────────────────────────
4:29:47 PM: ​
4:29:47 PM: Error message
4:29:47 PM: Command failed with exit code 127: npm run build (Search results for '"non-zero exit code: 127"' - Netlify Support Forums)
4:29:47 PM: ​
4:29:47 PM: Error location
4:29:47 PM: In build.command from netlify.toml:
4:29:47 PM: npm run build
4:29:47 PM: ​
4:29:47 PM: Resolved config
4:29:47 PM: build:
4:29:47 PM: command: npm run build
4:29:47 PM: commandOrigin: config
4:29:47 PM: environment:
4:29:47 PM: - HUGO_VERSION
4:29:47 PM: - NETLIFY_NEXT_PLUGIN_SKIP
4:29:47 PM: - NEXT_PRIVATE_TARGET
4:29:47 PM: publish: /opt/build/repo/.next
4:29:47 PM: publishOrigin: config
4:29:47 PM: functionsDirectory: /opt/build/repo/netlify/functions
4:29:47 PM: headers:
4:29:48 PM: - for: /*
values:
Access-Control-Allow-Origin: “*”
headersOrigin: config
plugins:
- inputs: {}
origin: config
package: “@netlify/plugin-nextjs”
4:29:48 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
4:29:48 PM: Failing build: Failed to build site
4:29:48 PM: Finished processing build request in 1m3.108s
4:29:48 PM: Failed during stage “building site”: Build script returned non-zero exit code: 2

My netlify.toml is:
[build]
command = “npm run build”
publish = “.next” # Should match the distDir in next.config.js

[[plugins]]
package = “@netlify/plugin-nextjs”

[dev]
functions = “netlify/functions” # The directory containing your serverless functions for local development

[[headers]]
for = “/"
[headers.values]
Access-Control-Allow-Origin = "

My package.json is:
{
“name”: “website_vf_2024_updated”,
“version”: “1.0.0”,
“description”: “website”,
“main”: “next.config.js”,
“dependencies”: {
[REDACTED TO REDUCE SPACE]
},
“devDependencies”: {},
“scripts”: {
“test”: “echo "Error: no test specified" && exit 1”,
“hugo”: “hugo”,
“build”: “next build && hugo”,
“export”: “next export”
},
“repository”: {
“type”: “git”,
“url”: “git+https://github.com/[redacted]”
},
“keywords”: [
“website”
],
“author”: “AT”,
“license”: “ISC”,
“bugs”: {
“url”: “https://github.com/[redacted]
},
“homepage”: “https://github.com/[redacted]#readme
}

@aetop The error is as it says:

4:29:47 PM: sh: 1: next: not found

It cannot find next and you should ensure that it is in your package.json so that it’s installed by Netlify.

While you’ve supplied your package.json contents, redacting the dependencies has unfortunately removed the only piece of information that was necessary to check in this case.

Hi Nathan, thank you so much for your help.

I did it and it didn’t seem to work. I asked ChatGPT and it told me that if I want to have Hugo and Next pages separately as folders (e.g. contact.html / contact.js, about.html, about.js and so on).

It’s such a mess and I really have no idea how to fix it. Should I just start again from scratch?

Latest error:

10:25:15 PM: Error: > Couldn"t find any pages or app directory. Please create one under the project root
at findPagesDir (/opt/build/repo/node_modules/next/dist/lib/find-pages-dir.js:42:15)
10:25:15 PM: at /opt/build/repo/node_modules/next/dist/build/index.js:406:73
10:25:15 PM: at async Span.traceAsyncFn (/opt/build/repo/node_modules/next/dist/trace/trace.js:151:20)
10:25:15 PM: at async build (/opt/build/repo/node_modules/next/dist/build/index.js:374:9)
10:25:15 PM: at async main (/opt/build/repo/node_modules/next/dist/bin/next:155:5)
10:25:15 PM: ​
10:25:15 PM: “build.command” failed
10:25:15 PM: ────────────────────────────────────────────────────────────────
10:25:15 PM: ​
10:25:15 PM: Error message
10:25:15 PM: Command failed with exit code 1: npm run build (Search results for '"non-zero exit code: 1"' - Netlify Support Forums)
10:25:15 PM: ​
10:25:15 PM: Error location
10:25:15 PM: In build.command from netlify.toml:
10:25:15 PM: npm run build
10:25:15 PM: ​
10:25:15 PM: Resolved config
10:25:15 PM: build:
10:25:15 PM: command: npm run build
10:25:15 PM: commandOrigin: config
10:25:15 PM: environment:
10:25:15 PM: - HUGO_VERSION
10:25:15 PM: - NETLIFY_NEXT_PLUGIN_SKIP
10:25:15 PM: - NEXT_PRIVATE_TARGET
10:25:15 PM: publish: /opt/build/repo/.next
10:25:15 PM: publishOrigin: config
10:25:15 PM: functionsDirectory: /opt/build/repo/netlify/functions
10:25:15 PM: headers:
10:25:16 PM: Failed during stage “building site”: Build script returned non-zero exit code: 2
10:25:16 PM: - for: /*
values:
Access-Control-Allow-Origin: “*”
headersOrigin: config
plugins:
- inputs: {}
origin: config
package: “@netlify/plugin-nextjs”
10:25:16 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
10:25:16 PM: Failing build: Failed to build site
10:25:16 PM: Finished processing build request in 57.615s

I can’t really advise on the accuracy of ChatGPT’s recommendation or if “starting again” would be appropriate as I don’t work with Hugo or NextJS.

One key thing to remember though is that if you cannot get your Build command (not your Develop command) to work locally, then it also won’t work on Netlify.

You should get your project working correctly then deploy to Netlify.

The error that you’re getting now is:

10:25:15 PM: Error: > Couldn"t find any pages or app directory. Please create one under the project root
at findPagesDir

It contains instruction on how to fix.