Consistently getting "sh: 1: > next: not found"

I’ve been trying to tackle this issue for a couple days now and I just cant figure out whats going on. Looks like the support team is also stumped so I though hey lets reach out to the community see what they think.

Here is my problem. I’m simply trying to deploy our nextjs site on netlify and I keep getting that annoying build error.

4:03:37 PM: $ npm run build
4:03:37 PM: > op22@0.0.1 build
4:03:37 PM: > next build
4:03:37 PM: sh: 1: next: not found

I’ve made sure 100% that nextjs is installed. I’ve confirmed that the node_modules does contain next. I event rolled back to older branches and those branches seemed to deploy just fine however, when we do a diff on what changed in the package.json, there is literally 0 changes.

Here is the full log of the failing build:

4:03:11 PM: Installing NPM modules using NPM version 8.19.2
4:03:22 PM: added 1002 packages, and audited 1003 packages in 11s
4:03:22 PM: 1 high severity vulnerability
4:03:22 PM: To address all issues, run:
4:03:22 PM: npm audit fix
4:03:22 PM: Run npm audit for details.
4:03:22 PM: NPM modules installed
4:03:22 PM: Creating package sha
4:03:23 PM: Started restoring cached go cache
4:03:23 PM: Finished restoring cached go cache
4:03:23 PM: Installing Go version 1.17 (requested 1.17)
4:03:27 PM: unset GOOS;
4:03:27 PM: unset GOARCH;
4:03:27 PM: export GOROOT=‘/opt/buildhome/.gimme/versions/go1.17.linux.amd64’;
4:03:27 PM: export PATH=“/opt/buildhome/.gimme/versions/go1.17.linux.amd64/bin:${PATH}”;
4:03:27 PM: go version >&2;
4:03:27 PM: export GIMME_ENV=“/opt/buildhome/.gimme/env/go1.17.linux.amd64.env”
4:03:27 PM: go version go1.17 linux/amd64
4:03:28 PM: Detected 1 framework(s)
4:03:28 PM: “next” at version “12.3.1”
4:03:28 PM: Installing missing commands
4:03:28 PM: Verify run directory
4:03:29 PM: ​
4:03:29 PM: ────────────────────────────────────────────────────────────────
4:03:29 PM: Netlify Build
4:03:29 PM: ────────────────────────────────────────────────────────────────
4:03:29 PM: ​
4:03:29 PM: ❯ Version
4:03:29 PM: @netlify/build 28.1.7
4:03:29 PM: ​
4:03:29 PM: ❯ Flags
4:03:29 PM: baseRelDir: true
4:03:29 PM: buildId: 63656ff3521dba0074df1182
4:03:29 PM: deployId: 63656ff3521dba0074df1184
4:03:29 PM: ​
4:03:29 PM: ❯ Current directory
4:03:29 PM: /opt/build/repo
4:03:29 PM: ​
4:03:29 PM: ❯ Config file
4:03:29 PM: /opt/build/repo/netlify.toml
4:03:29 PM: ​
4:03:29 PM: ❯ Context
4:03:29 PM: production
4:03:29 PM: ​
4:03:29 PM: ❯ Installing plugins
4:03:29 PM: - @netlify/plugin-nextjs@4.28.4
4:03:36 PM: ​
4:03:36 PM: ❯ Using Next.js Runtime - v4.28.4
4:03:37 PM: ​
4:03:37 PM: ────────────────────────────────────────────────────────────────
4:03:37 PM: 1. @netlify/plugin-nextjs (onPreBuild event)
4:03:37 PM: ────────────────────────────────────────────────────────────────
4:03:37 PM: ​
4:03:37 PM: No Next.js cache to restore.
4:03:37 PM: Netlify configuration property “build.environment.NEXT_PRIVATE_TARGET” value changed.
4:03:37 PM: ​
4:03:37 PM: (@netlify/plugin-nextjs onPreBuild completed in 17ms)
4:03:37 PM: ​
4:03:37 PM: ────────────────────────────────────────────────────────────────
4:03:37 PM: 2. build.command from netlify.toml
4:03:37 PM: ────────────────────────────────────────────────────────────────
4:03:37 PM: ​
4:03:37 PM: $ npm run build
4:03:37 PM: > op22@0.0.1 build
4:03:37 PM: > next build
4:03:37 PM: sh: 1: next: not found
4:03:38 PM: ​
4:03:38 PM: ────────────────────────────────────────────────────────────────
4:03:38 PM: “build.command” failed
4:03:38 PM: ────────────────────────────────────────────────────────────────
4:03:38 PM: ​
4:03:38 PM: Error message
4:03:38 PM: Command failed with exit code 127: npm run build (Search results for '"non-zero exit code: 127"' - Netlify Support Forums)
4:03:38 PM: ​
4:03:38 PM: Error location
4:03:38 PM: In build.command from netlify.toml:
4:03:38 PM: npm run build
4:03:38 PM: ​
4:03:38 PM: Resolved config
4:03:38 PM: build:
4:03:38 PM: command: npm run build
4:03:38 PM: commandOrigin: config
4:03:38 PM: environment:
4:03:38 PM: - CONTENTFUL_DELIVERY_TOKEN
4:03:38 PM: - CONTENTFUL_ENVIRONMENT
4:03:38 PM: - CONTENTFUL_PREVIEW_TOKEN
4:03:38 PM: - CONTENTFUL_SPACE_ID
4:03:38 PM: - NEXT_PRIVATE_TARGET
4:03:38 PM: publish: /opt/build/repo/.next
4:03:39 PM: Creating deploy upload records
4:03:38 PM: publishOrigin: config
4:03:38 PM: plugins:
4:03:38 PM: - inputs: {}
4:03:38 PM: origin: ui
4:03:39 PM: Failed during stage ‘building site’: Build script returned non-zero exit code: 2 (Search results for '"non-zero exit code: 2"' - Netlify Support Forums)
4:03:38 PM: package: ‘@netlify/plugin-nextjs’
4:03:38 PM: Caching artifacts
4:03:38 PM: Started saving node modules
4:03:38 PM: Finished saving node modules
4:03:38 PM: Started saving build plugins
4:03:38 PM: Finished saving build plugins
4:03:38 PM: Started saving pip cache
4:03:38 PM: Finished saving pip cache
4:03:38 PM: Started saving emacs cask dependencies
4:03:38 PM: Finished saving emacs cask dependencies
4:03:38 PM: Started saving maven dependencies
4:03:38 PM: Finished saving maven dependencies
4:03:38 PM: Started saving boot dependencies
4:03:38 PM: Finished saving boot dependencies
4:03:38 PM: Started saving rust rustup cache
4:03:38 PM: Finished saving rust rustup cache
4:03:38 PM: Started saving go dependencies
4:03:38 PM: Finished saving go dependencies
4:03:39 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
4:03:39 PM: Failing build: Failed to build site
4:03:39 PM: Finished processing build request in 35.306795596s

Here is the successful build:

4:05:54 PM: Installing NPM modules using NPM version 8.19.2
4:05:59 PM: npm WARN deprecated source-map-url@0.4.1: See GitHub - lydell/source-map-url: [DEPRECATED] Tools for working with sourceMappingURL comments.
4:06:00 PM: npm WARN deprecated source-map-resolve@0.5.3: See GitHub - lydell/source-map-resolve: [DEPRECATED] Resolve the source map and/or sources for a generated file.
4:06:00 PM: npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
4:06:00 PM: npm WARN deprecated uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See There’s Math.random(), and then there’s Math.random() · V8 for details.
4:06:00 PM: npm WARN deprecated stable@0.1.8: Modern JS already guarantees Array#sort() is a stable sort, so this library is deprecated. See the compatibility table on MDN: Array.prototype.sort() - JavaScript | MDN
4:06:00 PM: npm WARN deprecated source-map-resolve@0.6.0: See GitHub - lydell/source-map-resolve: [DEPRECATED] Resolve the source map and/or sources for a generated file.
4:06:00 PM: npm WARN deprecated sane@4.1.0: some dependency vulnerabilities fixed, support for node < 10 dropped, and newer ECMAScript syntax/features added
4:06:04 PM: npm WARN deprecated @types/cypress@1.1.3: This is a stub types definition for cypress (https://cypress.io). cypress provides its own type definitions, so you don’t need @types/cypress installed!
4:06:05 PM: npm WARN deprecated chokidar@2.1.8: Chokidar 2 does not receive security updates since 2019. Upgrade to chokidar 3 with 15x fewer dependencies
4:06:07 PM: npm WARN deprecated urix@0.1.0: Please see GitHub - lydell/urix: [DEPRECATED] Makes Windows-style paths more unix and URI friendly.
4:06:07 PM: npm WARN deprecated resolve-url@0.2.1: GitHub - lydell/resolve-url: [DEPRECATED] Like Node.js’ `path.resolve`/`url.resolve` for the browser.
4:06:09 PM: npm WARN deprecated core-js-pure@3.19.2: core-js-pure@<3.23.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Some versions have web compatibility issues. Please, upgrade your dependencies to the actual version of core-js-pure.
4:06:09 PM: npm WARN deprecated core-js@3.21.1: core-js@<3.23.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Some versions have web compatibility issues. Please, upgrade your dependencies to the actual version of core-js.
4:06:20 PM: added 2391 packages, and audited 2392 packages in 25s
4:06:20 PM: 301 packages are looking for funding
4:06:20 PM: run npm fund for details
4:06:20 PM: 22 high severity vulnerabilities
4:06:20 PM: To address issues that do not require attention, run:
4:06:20 PM: npm audit fix
4:06:20 PM: To address all issues (including breaking changes), run:
4:06:20 PM: npm audit fix --force
4:06:20 PM: Run npm audit for details.
4:06:20 PM: NPM modules installed
4:06:20 PM: Creating package sha
4:06:20 PM: Started restoring cached go cache
4:06:20 PM: Finished restoring cached go cache
4:06:20 PM: Installing Go version 1.17 (requested 1.17)
4:06:24 PM: unset GOOS;
4:06:24 PM: unset GOARCH;
4:06:24 PM: export GOROOT=‘/opt/buildhome/.gimme/versions/go1.17.linux.amd64’;
4:06:24 PM: export PATH=“/opt/buildhome/.gimme/versions/go1.17.linux.amd64/bin:${PATH}”;
4:06:24 PM: go version >&2;
4:06:24 PM: export GIMME_ENV=“/opt/buildhome/.gimme/env/go1.17.linux.amd64.env”
4:06:24 PM: go version go1.17 linux/amd64
4:06:25 PM: Detected 1 framework(s)
4:06:25 PM: “next” at version “12.2.2”
4:06:25 PM: Installing missing commands
4:06:25 PM: Verify run directory
4:06:26 PM: ​
4:06:26 PM: ────────────────────────────────────────────────────────────────
4:06:26 PM: Netlify Build
4:06:26 PM: ────────────────────────────────────────────────────────────────
4:06:26 PM: ​
4:06:26 PM: ❯ Version
4:06:26 PM: @netlify/build 28.1.7
4:06:26 PM: ​
4:06:26 PM: ❯ Flags
4:06:26 PM: baseRelDir: true
4:06:26 PM: buildId: 636570968c469c006c48a679
4:06:26 PM: deployId: 636570968c469c006c48a67b
4:06:26 PM: ​
4:06:26 PM: ❯ Current directory
4:06:26 PM: /opt/build/repo
4:06:26 PM: ​
4:06:26 PM: ❯ Config file
4:06:26 PM: No config file was defined: using default values.
4:06:26 PM: ​
4:06:26 PM: ❯ Context
4:06:26 PM: production
4:06:26 PM: ​
4:06:26 PM: ❯ Installing plugins
4:06:26 PM: - @netlify/plugin-nextjs@4.28.4
4:06:34 PM: ​
4:06:34 PM: ❯ Using Next.js Runtime - v4.28.4
4:06:35 PM: ​
4:06:35 PM: ────────────────────────────────────────────────────────────────
4:06:35 PM: 1. @netlify/plugin-nextjs (onPreBuild event)
4:06:35 PM: ────────────────────────────────────────────────────────────────
4:06:35 PM: ​
4:06:35 PM: No Next.js cache to restore.
4:06:35 PM: Netlify configuration property “build.environment.NEXT_PRIVATE_TARGET” value changed.
4:06:35 PM: ​
4:06:35 PM: (@netlify/plugin-nextjs onPreBuild completed in 14ms)
4:06:35 PM: ​
4:06:35 PM: ────────────────────────────────────────────────────────────────
4:06:35 PM: 2. Build command from Netlify app
4:06:35 PM: ────────────────────────────────────────────────────────────────
4:06:35 PM: ​
4:06:35 PM: $ npm run build
4:06:35 PM: > op22@0.0.1 build
4:06:35 PM: > next build
4:06:36 PM: warn - You have enabled experimental feature (images) in next.config.js.
4:06:36 PM: warn - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.
4:06:36 PM: warn - No build cache found. Please configure build caching for faster rebuilds. Read more: no-cache | Next.js
4:06:36 PM: info - Linting and checking validity of types…
4:06:39 PM: info - Creating an optimized production build…
4:06:45 PM: Browserslist: caniuse-lite is outdated. Please run:
4:06:45 PM: npx browserslist@latest --update-db
4:06:45 PM: Why you should do it regularly: GitHub - browserslist/browserslist: 🦔 Share target browsers between different front-end tools, like Autoprefixer, Stylelint and babel-preset-env
4:06:45 PM: warn - You have enabled the JIT engine which is currently in preview.
4:06:45 PM: warn - Preview features are not covered by semver, may introduce breaking changes, and can change at any time.
4:06:49 PM: info - Compiled successfully
4:06:49 PM: info - Collecting page data…
4:06:51 PM: info - Generating static pages (0/12)
4:06:51 PM: info - Generating static pages (3/12)
4:06:51 PM: info - Generating static pages (6/12)
4:06:51 PM: info - Generating static pages (9/12)
4:06:51 PM: info - Generating static pages (12/12)
4:06:52 PM: info - Finalizing page optimization…
4:06:52 PM: Page Size First Load JS
4:06:52 PM: ┌ ● / 2.46 kB 140 kB
4:06:52 PM: ├ /_app 0 B 93.2 kB
4:06:52 PM: ├ ○ /404 329 B 93.5 kB
4:06:52 PM: ├ ● /500 329 B 93.5 kB
4:06:52 PM: ├ λ /api/hello 0 B 93.2 kB
4:06:52 PM: ├ ● /business 4.94 kB 142 kB
4:06:52 PM: ├ ● /families 4.52 kB 130 kB
4:06:52 PM: └ ● /playground 956 B 115 kB
4:06:52 PM: + First Load JS shared by all 93.2 kB
4:06:52 PM: ├ chunks/framework-5f4595e5518b5600.js 42 kB
4:06:52 PM: ├ chunks/main-c6969d79aeee037e.js 31.5 kB
4:06:52 PM: ├ chunks/pages/_app-e7b1ecce7e93e9f1.js 18.8 kB
4:06:52 PM: ├ chunks/webpack-69bfa6990bb9e155.js 769 B
4:06:52 PM: └ css/75f85463b352f34f.css 5.67 kB
4:06:52 PM: λ (Server) server-side renders at runtime (uses getInitialProps or getServerSideProps)
4:06:52 PM: ○ (Static) automatically rendered as static HTML (uses no initial props)
4:06:52 PM: ● (SSG) automatically generated as static HTML + JSON (uses getStaticProps)
4:06:52 PM: ​
4:06:52 PM: (build.command completed in 16.6s)
4:06:52 PM: ​
4:06:52 PM: ────────────────────────────────────────────────────────────────
4:06:52 PM: 3. @netlify/plugin-nextjs (onBuild event)
4:06:52 PM: ────────────────────────────────────────────────────────────────

────────────────────────────────────────────────────────────────
4:07:11 PM: Netlify Build Complete
4:07:11 PM: ────────────────────────────────────────────────────────────────
4:07:11 PM: ​
4:07:11 PM: (Netlify Build completed in 44.5s)
4:07:11 PM: Caching artifacts
4:07:11 PM: Started saving node modules
4:07:11 PM: Finished saving node modules
4:07:11 PM: Started saving build plugins
4:07:11 PM: Finished saving build plugins
4:07:11 PM: Started saving pip cache
4:07:11 PM: Finished saving pip cache
4:07:11 PM: Started saving emacs cask dependencies
4:07:11 PM: Finished saving emacs cask dependencies
4:07:11 PM: Started saving maven dependencies
4:07:11 PM: Finished saving maven dependencies
4:07:11 PM: Started saving boot dependencies
4:07:11 PM: Finished saving boot dependencies
4:07:11 PM: Started saving rust rustup cache
4:07:11 PM: Finished saving rust rustup cache
4:07:11 PM: Started saving go dependencies
4:07:11 PM: Finished saving go dependencies
4:07:12 PM: Build script success
4:07:14 PM: Uploading Cache of size 394.7MB
4:07:16 PM: Finished processing build request in 1m30.222418352s
4:07:17 PM: Site is live :sparkles:

I want to deploy nextjs using ISR which I KNOW we can do… I did it on a separate project for POC purposes (that has nearly the same package.json)… but netlify wont let me deploy :confused: any suggestions?

Btw… my toml file only has :

[build]
  command = "npm run build"
  publish = ".next"

I figured it out… it was something i’d never expect to happen … essentially netlify defaults to node 16 where as our project is built using node 14… because of the mismatch in node versions packages were being installed properly…

anyway i changed the node version and boom worked.