Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Composer says there is no composer.json in this folder

ErrorComposer could not find a composer.json file in /var/www/my-app

Short answer

Composer reads composer.json from the working directory, and stops when the file is not there. The message names the folder it checked. Either you are in the wrong folder, the file has another name or never reached the server, or the project has none yet and needs composer init.

  • Applies to Composer 2, PHP 8.3, Ubuntu 24.04, macOS
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the folder named in the message, then list that folder. Composer reads composer.json from the directory it runs in.

    pwd
    ls -la composer.json composer.lock
  2. 2Match what you see, then jump to its fix

    1. 1The path in the message is a parent or sibling of your projectYou ran Composer from the wrong folderFix
    2. 2No composer.json anywhere in the folder treeThe project does not have a composer.json yetFix
    3. 3composer.json exists on your laptop but not after a deploy, zip upload or Docker buildThe file was never copied to the serverFix
    4. 4The project uses composer-other.json or similar, and a COMPOSER variable may be setThe environment points Composer at another file nameFix

What does the error look like?

  • Composer could not find a composer.json file in /var/www/my-app
  • To initialize a project, please create a composer.json file.

Cause 1 of 4

You ran Composer from the wrong folder

1Check

Composer reads composer.json from the working directory, so a parent or sibling folder does not count.

find . -maxdepth 3 -name composer.json -not -path "*/vendor/*"

2Fix

Change into the folder that holds composer.json, or leave the folder alone and pass it with the working-dir option.

cd /var/www/my-app && composer install
# or, without cd:
composer install --working-dir=/var/www/my-app

3Verify

The install starts and writes into vendor in the project folder.

ls vendor/autoload.php

Confirmed by getcomposer.org

Did this fix it?

Cause 2 of 4

The project does not have a composer.json yet

1Check

A folder of plain PHP files has no composer.json. Composer needs that file to know what to install.

ls -la

2Fix

Create one with the init command, which asks a few questions, or pre-fill the answers with options. Then add packages with require.

composer init --name="vendor/my-app" --no-interaction
composer require monolog/monolog

3Verify

The file exists and lists the package.

cat composer.json

Confirmed by getcomposer.org

Did this fix it?

Cause 3 of 4

The file was never copied to the server

1Check

composer.json belongs in the top folder of the project and in version control. A deploy that copies only src or public folders leaves it out, and so does a Docker COPY that skips it.

git ls-files composer.json composer.lock
ls -la

2Fix

Commit composer.json and composer.lock, and make the deploy or the Docker COPY step include them before composer install runs.

COPY composer.json composer.lock ./
RUN composer install --no-dev --no-interaction

3Verify

Rebuild or redeploy, then run the install in the target folder.

ls composer.json && composer validate

Confirmed by getcomposer.org

Did this fix it?

Cause 4 of 4

The environment points Composer at another file name

1Check

The COMPOSER environment variable changes the file name Composer reads, and the lock file takes the same base name.

echo "$COMPOSER"

2Fix

Unset the variable to use composer.json, or keep it and make sure the named file exists in the working directory.

unset COMPOSER
composer install

3Verify

Composer reads composer.json and finds composer.lock.

composer validate

Confirmed by getcomposer.org

Did this fix it?

How do I stop it coming back?

  • Keep composer.json and composer.lock in the top folder of the repository and commit both.
  • In scripts and CI, use --working-dir instead of relying on the current folder.
  • Print pwd at the start of a deploy script, so a wrong folder is obvious in the log.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

CodeIgniter 4 cannot write its cache files

CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/

CodeIgniter 4's file cache needs a cache directory that the web server can really write to.

2 causes, each with a fix

Back end

CodeIgniter 3 loads the model but the property is undefined

The model name you are loading is the name of a resource that is already being used: user

CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.