4

I have a project where I am using composer.

However, I have lost my composer.json.

Is there a way to recreate composer.json from the content of the vendor directory?

localheinz
  • 9,179
  • 2
  • 33
  • 44
Kush
  • 101
  • 1
  • 3
  • 14

2 Answers2

12

Identifying the installed dependencies can be easily achieved by inspecting

vendor
└── composer
    └── installed.json

However, you will be faced with the following problems:

Direct and indirect dependencies

installed.json lists all installed dependencies, that is, both

  • direct and
  • indirect

dependencies.

Direct dependencies are dependencies that are explicitly listed in the require and require-dev sections.

Indirect dependencies are dependencies of your direct dependencies.

That is, while you could programmatically parse installed.json and collect the dependencies listed therein, you will need to decide for yourself whether these are direct or indirect dependencies, or in other words, whether you want to explicitly require these dependencies in composer.json.

Production and development dependencies

installed.json lists all installed dependencies, that is, depending on whether you ran

$ composer install

or

$composer install --no-dev

before you lost your composer.json, installed.json will contain direct and indirect dependencies listed in

  • require and require-dev or
  • require

sections, respectively.

That is, you will need to decide for yourself whether these are dependencies which should be listed in the

  • require or
  • require-dev

sections.

See

to find out more about the purpose and the differences between these sections.

Version Constraints

installed.json lists all installed dependencies with the exact versions installed.

However, the composer.json you lost in all likelihood did not list dependencies with exact versions, but using one of the many possibilities for specifying version constraints.

That is, you will need to decide for yourself whether you want to use exact versions, or relax the constraints, for example, by using

  • the ~ operator
  • the ^ operator
  • etc.

See

to find out more about version constraints.

localheinz
  • 9,179
  • 2
  • 33
  • 44
2

Hoppe!!

I had a similar problem with a Laravel project In order to solve it:

  1. Create a separate base project, with the same version of Laravel to work with, in case you don't work with Laravel, you can skip to step 3.
  2. Copy the contents of the file to have a stable working base
  3. Run the a python script gist , giving it the installed.json path, this will return the require and require-dev of the project
  4. Copy and paste the requirements inside composer.json. In case of working with Laravel, be careful not to affect the require brought from the base app.
  • Pido disculpas de antemano si por mi respuesta o mi forma de codificar en el github provisto ofendo a alguien, realmente no es mi intención, solo quiero ser de ayuda, gracias por su tiempo. – Yahir Eduardo Bravo Tafur May 19 '22 at 17:10