11

In my Laravel project the composer.json and composer.lock files were deleted and I don't know how to rebuild the composer.json file from an existing vendor directory .

Amine
  • 133
  • 1
  • 2
  • 9

1 Answers1

20

Note

downside of using this method is that the main composer.json file would include some of the packages that have been required by existing or sub packages or multiple packages. but don't worry it would be installed only one time. but there might be problem with versions.

Advice

I recommend you use some version control like git to overcome such situations in future.

can't guarantee about the following method i am going to present as its not a usual case. but might work.

create a new composer.json file like this in project root. you could use composer init command if you wish.

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.6.4",
        "laravel/framework": "5.3.*"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "5.6",
        "symfony/css-selector": "3.1.*",
        "symfony/dom-crawler": "3.1.*"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "Blog\\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}
  • go to every package folder inside vendor folder and find the package's composer.json file. from that file find the "name" of that package from that composer.json file.
  • add that name of the package to the main composer.json file's "require" part. if you want to add that package to development add it to "require-dev" part.

  • composer install or composer update

  • composer dump-autoload
aimme
  • 6,385
  • 7
  • 48
  • 65
  • 2
    Happened to me with a legacy project just before I went to initiate Git. Great advice. What I did additionally is after adding all `"name"`s from each vendor/subfolder/composer.json I Ctrl+Shift+F searched the vendor folder for each of those names including double quotes. Then if I found it listed under `"require"` of another package (ignoring `installed.json`) and not just under `"name"`, I removed it. Backed up before `composer update`, and it went perfectly. – s3c Mar 20 '21 at 14:21