No such file or directory, open '/opt/build/repo/package.json' and Failed during stage 'building site': Build script returned non-zero exit code: 2

Hi, it is good to see you in this forum. I have some question with my project.

I want to build my website by using:

  • Ghost JS as main javascript platform
  • GitHub as repository
  • Netlify as hosting

When i tried to deploy, i got some error in there:

  • “’ build.command’ failed”
  • npm ERR! enoent ENOENT: no such file or directory, open ‘/opt/build/repo/package.json’

Here is my log:
7:46:08 PM: Build ready to start
7:46:12 PM: build-image version: d3293dd23e2cbd8877a64c48606c7cba6018f451 (focal)
7:46:12 PM: buildbot version: d3293dd23e2cbd8877a64c48606c7cba6018f451
7:46:12 PM: Building without cache
7:46:12 PM: Starting to prepare the repo for build
7:46:12 PM: No cached dependencies found. Cloning fresh repo
7:46:12 PM: git clone --filter=blob:none GitHub - myouiswan/blogku: test blog with ghostjs
7:46:13 PM: Preparing Git Reference refs/heads/main
7:46:40 PM: Parsing package.json dependencies
7:46:41 PM: Starting build script
7:46:41 PM: Installing dependencies
7:46:41 PM: Python version set to 2.7
7:46:41 PM: v16.19.1 is already installed.
7:46:42 PM: Now using node v16.19.1 (npm v8.19.3)
7:46:42 PM: Enabling Node.js Corepack
7:46:42 PM: Started restoring cached build plugins
7:46:42 PM: Finished restoring cached build plugins
7:46:42 PM: Attempting Ruby version 2.7.2, read from environment
7:46:43 PM: Using Ruby version 2.7.2
7:46:43 PM: Using PHP version 8.0
7:46:43 PM: Started restoring cached go cache
7:46:43 PM: Finished restoring cached go cache
7:46:43 PM: go version go1.19.7 linux/amd64
7:46:43 PM: Detected 0 framework(s)
7:46:43 PM: Section completed: initializing
7:46:44 PM: ​
7:46:44 PM: Netlify Build
7:46:44 PM: ────────────────────────────────────────────────────────────────
7:46:44 PM: ​
7:46:44 PM: ❯ Version
7:46:44 PM: @netlify/build 29.6.8
7:46:44 PM: ​
7:46:44 PM: ❯ Flags
7:46:44 PM: baseRelDir: true
7:46:44 PM: buildId: 64106c90a0d6550e87780f46
7:46:44 PM: deployId: 64106c90a0d6550e87780f48
7:46:44 PM: ​
7:46:44 PM: ❯ Current directory
7:46:44 PM: /opt/build/repo
7:46:44 PM: ​
7:46:44 PM: ❯ Config file
7:46:44 PM: No config file was defined: using default values.
7:46:44 PM: ​
7:46:44 PM: ❯ Context
7:46:44 PM: production
7:46:44 PM: ​
7:46:44 PM: 1. Build command from Netlify app
7:46:44 PM: ────────────────────────────────────────────────────────────────
7:46:44 PM: ​
7:46:44 PM: $ CI= npm run build
7:46:45 PM: npm ERR! code ENOENT
7:46:45 PM: npm ERR! syscall open
7:46:45 PM: npm ERR! path /opt/build/repo/package.json
7:46:45 PM: npm ERR! errno -2
7:46:45 PM: npm ERR! enoent ENOENT: no such file or directory, open ‘/opt/build/repo/package.json’
7:46:45 PM: npm ERR! enoent This is related to npm not being able to find a file.
7:46:45 PM: npm ERR! enoent
7:46:45 PM: npm ERR! A complete log of this run can be found in:
7:46:45 PM: npm ERR! /opt/buildhome/.npm/_logs/2023-03-14T12_46_45_154Z-debug-0.log
7:46:45 PM: ​
7:46:45 PM: “build.command” failed
7:46:45 PM: ────────────────────────────────────────────────────────────────
7:46:46 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)
7:46:45 PM: ​
7:46:45 PM: Error message
7:46:45 PM: Command failed with exit code 254: CI= npm run build (Search results for '"non-zero exit code: 254"' - Netlify Support Forums)
7:46:45 PM: ​
7:46:45 PM: Error location
7:46:45 PM: In Build command from Netlify app:
7:46:45 PM: CI= npm run build
7:46:45 PM: ​
7:46:45 PM: Resolved config
7:46:45 PM: build:
7:46:45 PM: command: CI= npm run build
7:46:45 PM: commandOrigin: ui
7:46:45 PM: environment:
7:46:45 PM: - CI
7:46:45 PM: publish: /opt/build/repo/build
7:46:45 PM: publishOrigin: ui
7:46:45 PM: Caching artifacts
7:46:45 PM: Started saving build plugins
7:46:45 PM: Finished saving build plugins
7:46:45 PM: Started saving pip cache
7:46:45 PM: Finished saving pip cache
7:46:45 PM: Started saving emacs cask dependencies
7:46:45 PM: Finished saving emacs cask dependencies
7:46:45 PM: Started saving maven dependencies
7:46:45 PM: Finished saving maven dependencies
7:46:45 PM: Started saving boot dependencies
7:46:45 PM: Finished saving boot dependencies
7:46:45 PM: Started saving rust rustup cache
7:46:45 PM: Finished saving rust rustup cache
7:46:45 PM: Started saving go dependencies
7:46:45 PM: Finished saving go dependencies
7:46:46 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
7:46:46 PM: Failing build: Failed to build site
7:46:46 PM: Finished processing build request in 34.158s

I already tried to make some variable environment like “CI”, but this doesn’t solve my issue.

If you know or experience with this issue, please let me know how to solve it.

Thank you.

@myouiswan Setting CI to false is done to prevent warnings from being treated as errors.
Since that isn’t your issue, it won’t have any impact (as you’ve already discovered).

Your build is running npm run build

7:46:44 PM: $ CI= npm run build

This will run the build script within the package.json file located in your Base directory.
The Base directory is set within the Basic build settings, and defaults to the root of the repository that you have linked.

There does not appear to be a package.json file in the root of the repository…

7:46:45 PM: npm ERR! code ENOENT
7:46:45 PM: npm ERR! syscall open
7:46:45 PM: npm ERR! path /opt/build/repo/package.json
7:46:45 PM: npm ERR! errno -2
7:46:45 PM: npm ERR! enoent ENOENT: no such file or directory, open ‘/opt/build/repo/package.json’
7:46:45 PM: npm ERR! enoent This is related to npm not being able to find a file.

Checking your repository confirms that…

It’s up to you how you structure your project, and what you configure your build settings to, but it looks like you’ll need to change your Base directory to one of those sub-directories, perhaps current.

1 Like