Answers.Body: <p>Looks like your <code>web/index.php</code> file was created for an older version of the <code>vlucas/phpdotenv</code> library. The error method shows that you're initializing the library in a way that's <a href="https://github.com/vlucas/phpdotenv/tree/3.6" rel="nofollow noreferrer">compatible with version <code>^3.0.0</code></a>, but in your <code>composer.json</code>, you're requiring <code>^5.4.0</code>. In this version, the library is initialized in a different way, so you need to adjust your <code>web/index.php</code> accordingly.</p>
  <p>If you're looking for a good place to start, check out the way the library is initialized in the <a href="https://github.com/craftcms/craft/blob/5.x/bootstrap.php" rel="nofollow noreferrer">current version of the Craft starter project</a>:</p>
  <pre class="lang-php prettyprint-override"><code>// Load dotenv?
  if (class_exists(Dotenv\Dotenv::class)) {
      // By default, this will allow .env file values to override environment variables
      // with matching names. Use `createUnsafeImmutable` to disable this.
      Dotenv\Dotenv::createUnsafeMutable(CRAFT_BASE_PATH)-&gt;safeLoad();
  }
  </code></pre>
  <p>You're gonna have the same problem with the <code>craft</code> binary used for console commands. I recommend copying the following files from the starter project to your local project:</p>
  <ul>
  <li><code>bootstrap.php</code></li>
  <li><code>craft</code></li>
  <li><code>web/index.php</code></li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-11-06T15:30:03.590Z
Answers.Text: unfortunately the link does not work anymore and if I go to the 4.x version these files don't have any dotenv parts in them...
  Example: https://github.com/craftcms/craft/blob/4.x/craft
  do you know if things have further changed?
Answers.UserDisplayName: Merc
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-11-06T16:32:18.567Z
Answers.Text: @Merc The link is now up-to-date again! Didn't work any more because the `main` branch was replaced by a `5.x` branch. The `craft` binary is very minimal, because it shares the initialization code with `web/index.php`. The code you're looking for is in `bootstrap.php`. See the updated link above for an example.
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-29T08:53:43.510Z
Answers.LastActivityDate: 2024-11-06T16:30:40.880Z
Answers.LastEditDate: 2024-11-06T16:30:40.880Z
Answers.UserDisplayName: MoritzLost
Body: <p>Just updated to Craft 4, migrated my <code>/web</code> folder over as well as my <code>/assets</code> folder and trying to load the index page gives me the following error.</p>
  <p>Any help would be great as I can no longer access my <code>/admin</code></p>
  <pre><code>Fatal error: Uncaught TypeError: Dotenv\Dotenv::create(): Argument #1 ($repository) must be of type Dotenv\Repository\RepositoryInterface, string given, called in /app/web/index.php on line 15 and defined in /app/vendor/vlucas/phpdotenv/src/Dotenv.php:83 Stack trace: #0 /app/web/index.php(15): Dotenv\Dotenv::create('/app') #1 {main} thrown in /app/vendor/vlucas/phpdotenv/src/Dotenv.php on line 83
  </code></pre>
  <p>My composer.json looks like the following:</p>
  <pre><code>{
    &quot;require&quot;: {
      &quot;craftcms/cms&quot;: &quot;^4.2.0.2&quot;,
      &quot;craftcms/feed-me&quot;: &quot;5.0.4&quot;,
      &quot;craftcms/redactor&quot;: &quot;3.0.2&quot;,
      &quot;nystudio107/craft-seomatic&quot;: &quot;4.0.7&quot;,
      &quot;nystudio107/craft-similar&quot;: &quot;4.0.0&quot;,
      &quot;putyourlightson/craft-blitz&quot;: &quot;4.2.1&quot;,
      &quot;putyourlightson/craft-sprig&quot;: &quot;2.1.0&quot;,
      &quot;verbb/image-resizer&quot;: &quot;3.0.1&quot;,
      &quot;verbb/wishlist&quot;: &quot;2.0.0&quot;,
      &quot;vlucas/phpdotenv&quot;: &quot;^5.4.0&quot;
    },
    &quot;require-dev&quot;: {
      &quot;yiisoft/yii2-shell&quot;: &quot;^2.0.3&quot;
    },
    &quot;autoload&quot;: {
      &quot;psr-4&quot;: {
        &quot;modules\\&quot;: &quot;modules/&quot;
      }
    },
    &quot;config&quot;: {
      &quot;allow-plugins&quot;: {
        &quot;craftcms/plugin-installer&quot;: true,
        &quot;yiisoft/yii2-composer&quot;: true
      },
      &quot;sort-packages&quot;: true,
      &quot;optimize-autoloader&quot;: true,
      &quot;platform&quot;: {
        &quot;php&quot;: &quot;8.1.0&quot;
      }
    },
    &quot;scripts&quot;: {
      &quot;post-root-package-install&quot;: [
        &quot;@php -r \&quot;file_exists('.env') || copy('.env.example', '.env');\&quot;&quot;
      ]
    }
  }
  </code></pre>
  
Tags: <craft3><craft4>
Title: Uncaught TypeError Dotenv Create on updating to Craft CMS 4
ThreadText: # Uncaught TypeError Dotenv Create on updating to Craft CMS 4
  
  (Asked by: username_0 on 2022-07-28)
  
  Just updated to Craft 4, migrated my `/web` folder over as well as my `/assets` folder and trying to load the index page gives me the following error.
  
  Any help would be great as I can no longer access my `/admin`
  
  ```
  Fatal error: Uncaught TypeError: Dotenv\Dotenv::create(): Argument #1 ($repository) must be of type Dotenv\Repository\RepositoryInterface, string given, called in /app/web/index.php on line 15 and defined in /app/vendor/vlucas/phpdotenv/src/Dotenv.php:83 Stack trace: #0 /app/web/index.php(15): Dotenv\Dotenv::create('/app') #1 {main} thrown in /app/vendor/vlucas/phpdotenv/src/Dotenv.php on line 83
  
  ```
  
  My composer.json looks like the following:
  
  ```
  {
    "require": {
      "craftcms/cms": "^4.2.0.2",
      "craftcms/feed-me": "5.0.4",
      "craftcms/redactor": "3.0.2",
      "nystudio107/craft-seomatic": "4.0.7",
      "nystudio107/craft-similar": "4.0.0",
      "putyourlightson/craft-blitz": "4.2.1",
      "putyourlightson/craft-sprig": "2.1.0",
      "verbb/image-resizer": "3.0.1",
      "verbb/wishlist": "2.0.0",
      "vlucas/phpdotenv": "^5.4.0"
    },
    "require-dev": {
      "yiisoft/yii2-shell": "^2.0.3"
    },
    "autoload": {
      "psr-4": {
        "modules\\": "modules/"
      }
    },
    "config": {
      "allow-plugins": {
        "craftcms/plugin-installer": true,
        "yiisoft/yii2-composer": true
      },
      "sort-packages": true,
      "optimize-autoloader": true,
      "platform": {
        "php": "8.1.0"
      }
    },
    "scripts": {
      "post-root-package-install": [
        "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
      ]
    }
  }
  
  ```
  
  **Tags:** craft3, craft4
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-07-29. Score: 7 | Accepted answer
  
  Looks like your `web/index.php` file was created for an older version of the `vlucas/phpdotenv` library. The error method shows that you're initializing the library in a way that's [compatible with version `^3.0.0`](https://github.com/vlucas/phpdotenv/tree/3.6), but in your `composer.json`, you're requiring `^5.4.0`. In this version, the library is initialized in a different way, so you need to adjust your `web/index.php` accordingly.
  
  If you're looking for a good place to start, check out the way the library is initialized in the [current version of the Craft starter project](https://github.com/craftcms/craft/blob/5.x/bootstrap.php):
  
  ```php
  // Load dotenv?
  if (class_exists(Dotenv\Dotenv::class)) {
      // By default, this will allow .env file values to override environment variables
      // with matching names. Use `createUnsafeImmutable` to disable this.
      Dotenv\Dotenv::createUnsafeMutable(CRAFT_BASE_PATH)->safeLoad();
  }
  
  ```
  
  You're gonna have the same problem with the `craft` binary used for console commands. I recommend copying the following files from the starter project to your local project:
  
  * `bootstrap.php`
  * `craft`
  * `web/index.php`
  
  > **Comments:**
  > **username_2**: unfortunately the link does not work anymore and if I go to the 4.x version these files don't have any dotenv parts in them... Example: https://github.com/craftcms/craft/blob/4.x/craft do you know if things have further changed?  
  > **username_1**: @username_2 The link is now up-to-date again! Didn't work any more because the `main` branch was replaced by a `5.x` branch. The `craft` binary is very minimal, because it shares the initialization code with `web/index.php`. The code you're looking for is in `bootstrap.php`. See the updated link above for an example.  
  
  ---
  
  