Website encounter error.

Created on 10 March 2025, 30 days ago

As we moved to development environment Drupal 10.2.4 with empower id integration.
Getting below error, after enabling any custom module.
The website encountered an unexpected error. Try again later.
Drupal\Core\DependencyInjection\ContainerNotInitializedException: \Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container. in Drupal::getContainer() (line 169 of /var/www/htmld10/core/lib/Drupal.php).
Only home page is accessible when custom module are disabled
Is there any help why it is happening?

install Drupal 10.2.4. Integrate emp id login. Create custom module. and enable it. it will show $container is not initialized error. any one facing similar error?

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

๐Ÿ’ฌ Support request
Status

Active

Version

10.2 โœจ

Component

install system

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @poonam11210
  • Drupal 10.2 is no longer supported. The best advice I can give is to try with a supported release. Additionally, there could be a coding problem with a custom module in the site.

  • Custom module working as expected in local and other higher environment. @cilefen, have you encountered similar error any time?

  • No. I suggest you search the Internet for "Drupal\Core\DependencyInjection\ContainerNotInitializedException: \Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container".

  • Already search and applied but it didn't helped. Hence created a new issue if I can get any relevant help.

  • Unfortunately nobody reading this will understand what "emp login" means because I don't know what that is. Is it part of the problem?

    Also, as I mentioned above, Drupal 10.2 is not supported here. Upgrade to a supported release. It's possible this has been fixed.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia vandanasom.123

    The error ContainerNotInitializedException occurs because the container is being accessed before it's fully initialized in Drupal.

    If your custom module or the empowerid integration is trying to access Drupal services (\Drupal::service(), \Drupal::entityTypeManager()) too early (before the container is fully initialized), this error will occur.

    Ensure youโ€™re not calling services in hook_enable(), hook_install(), or any code that runs too early in the lifecycle.

    Fix:

    1. Use Dependency Injection in your custom module instead of directly calling \Drupal::service() wherever possible.
    2. Make sure you're not accessing services before Drupal is fully bootstrapped (e.g., not in hook_enable()).
    3. Clear cache

  • Correct, I am using \Drupal::service however I wonder why it is working in one environment and not in another environment. Does it depends on PHP version as well? I am not using hook_install or hook_enable anywhere in custom module.
    I have gone through below threads as well.
    https://www.drupal.org/project/drupal/issues/3420704 ๐Ÿ› container not initialized yet Active
    https://www.drupal.org/project/upgrade_status/issues/3221550 ๐Ÿ› PHP Fatal error: ContainerNotInitializedException thrown while calling __toString on a TranslatableMarkup Postponed: needs info #7 says module_load_include()
    need to be replaced it with \Drupal::moduleHandler()->loadInclude()
    I am trying these solution.
    Thanks for the help.

Production build 0.71.5 2024