magento2: Magento2 cannot resolve path to pub/static JS files

Preconditions

  1. Magento ver. 2.1.3
  2. PHP 7.0
  3. MySQL 5.6

All of the above running in docker containers. Files are sync’ed using docker-sync utility, but I’ve also tried mounting volumes directly from my local host drive and it did not help.

I’ve confirmed that PHP is running as www-data user and that all project files have read & write permissions on every file and directory within the project.

I have previously tried deleting all manual content and running setup:static-content:deploy. Static files were re-created, but it didn’t help me to solve my problem.

Lastly, I’ve troubleshooted the code as far as I could and got to the point where in

vendor/magento/framework/View/Design/FileResolution/Fallback/Resolver/Simple.php file

protected function resolveFile(RuleInterface $fallbackRule, $file, array $params = [])
    {
        //->getPatternDirs return Array ( [0] => /var/www/html/lib/web )
        // given that nginx doc root path is /var/www/html
        foreach ($fallbackRule->getPatternDirs($params) as $dir) {
            $path = "{$dir}/{$file}";
            $dirRead = $this->readFactory->create($dir);
            if ($dirRead->isExist($file) && $this->checkFilePathAccess($file, $path)) {
                return $path;
            }
        }
        return false;
    }

Error details:

Unable to resolve the source file for '_requirejs/adminhtml/Magento/backend/en_US/requirejs-config.js'
#0 /var/www/html/vendor/magento/framework/App/StaticResource.php(97): Magento\Framework\View\Asset\File->getSourceFile()
#1 /var/www/html/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\StaticResource->launch()
#2 /var/www/html/pub/static.php(13): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\StaticResource))
#3 {main}

Also, I have confirmed that these files actually exist, but Magento is unable to work out their paths correctly.

Steps to reproduce

  1. Open management page or default Luma page

Expected result

  1. All files are loaded and all Javascript on the page is working

Actual result

  1. The files below can not be loaded, thus no JS is working on the page

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 14
  • Comments: 52 (11 by maintainers)

Commits related to this issue

Most upvoted comments

I ran into the same issue with the upgrade, and the solution is updating the nginx or apache configuration to fix the rewriting issues. The /static/ section of Nginx should be as follows to be able to get it working properly.

 location /static/ {
    if ($MAGE_MODE = "production") {
      expires max;
    }

    # Remove signature of the static files that is used to overcome the browser cache
    location ~ ^/static/version {
      rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last;
    }

    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
      add_header Cache-Control "public";
      add_header X-Frame-Options "SAMEORIGIN";
      expires +1y;

      if (!-f $request_filename) {
        rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
      }
    }

    location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
      add_header Cache-Control "no-store";
      add_header X-Frame-Options "SAMEORIGIN";
      expires off;

      if (!-f $request_filename) {
         rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
      }
    }

    if (!-f $request_filename) {
      rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
    }

    add_header X-Frame-Options "SAMEORIGIN";
  }

I have the same issue after upgrading from Magento 2.1.2 (Enterprise) to Magento 2.1.3 using a composer upgrade.

I think it’s due to changes made in static files - see release notes:

  • We’ve corrected a problem with _requirejs asset retrieval via static.php in static content versioning.
  • Versioning of static files (including CSS, JS, font, and image files) is now enabled by default.

I am using example nginx configuration, and have tried switching to both developer and production modes. Request reaches static.php correctly which then returns a 404 header to nginx. The referenced files exist on disk and I have verified file permissions.

I used the following workaround to disable static file signing, which has corrected the 404 for me:

INSERT INTO `core_config_data` (`scope`, `scope_id`, `path`, `value`) VALUES ('default', 0, 'dev/static/sign', '0');

You will need to clear cache after this, and also check for any entries with the same config path if you have previously modified this setting.

This isn’t a very good workaround as it would break cache-busting when using Varnish or any CDNs etc, but at least assets do not 404.

You can disable option in your admin Stores>Configuration>Advanced>Developer>Sign Static Files (dev_static_sign)

Or simply insert this row in your core_config_data table insert core_config_data (config_id, scope, scope_id, path, value) values (null, 'default', 0, 'dev/static/sign', 0);

Hey everyone, I think I got a fix for you:

I had this same issue on a new magento install. There are two issues here at play: Malformed apache configuration and non-deployed static content (at least for me). It is not mentioned at http://devdocs.magento.com/guides/v2.1/install-gde/install/web/install-web_6-install.html, but you need to run the command bin/magento setup:static-content:deploy for pub/static/ to be populated with the static content. (Huge thanks to Anil Suthar (Stack Overflow))

I wouldn’t recommend changing permissions to 777, what I did was do it as single user setup and set permissions to web server user, default is www-data:chown -R www-data:www-data /. (Note: this StackOverflow post has some good points about permissions)

Lastly, what’s up with the folder versionXXXXXXXXXX? Well, if you look at .htaccess in pub/static/ you’ll see two Rewrite Rules:

RewriteRule ^version.+?/(.+)$ $1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule .* ../static.php?resource=$0 [L]

(The second one triggers if the requested resource is not found)

The first rule removes the version folder in the request and serves instead frontend/ or adminhtml/ the resource asks for.

Check what apache2.conf says in /etc/apache2/ (if Ubuntu), it’s possible that /var/www/ directory has AllowOverride None set. It should be AllowOverride All, so that .htaccess files can override directives.

All of that fixed it for me, I hope this helps anyone else!

… Sigh, Post Factum I’ve found out that this is problem is mentioned in the verify installation page. Here’s the link that basically mimics what I wrote here: http://devdocs.magento.com/guides/v2.1/install-gde/trouble/tshoot_no-styles.html

having same problem after upgrading to magento 2.4.1

Setting dev/static/sign to 0 is NOT a proper solution to this. It will make the browser caches of visitors assume, that they have your script and other static files stored. This can lead to problems, when you update and rebuild your static files.

I have the same issue after upgrading from Magento 2.1.2 (Enterprise) to Magento 2.1.3 using a composer upgrade.

I think it’s due to changes made in static files - see release notes:

  • We’ve corrected a problem with _requirejs asset retrieval via static.php in static content versioning.
  • Versioning of static files (including CSS, JS, font, and image files) is now enabled by default.

I am using example nginx configuration, and have tried switching to both developer and production modes. Request reaches static.php correctly which then returns a 404 header to nginx. The referenced files exist on disk and I have verified file permissions.

I used the following workaround to disable static file signing, which has corrected the 404 for me:

INSERT INTO `core_config_data` (`scope`, `scope_id`, `path`, `value`) VALUES ('default', 0, 'dev/static/sign', '0');

You will need to clear cache after this, and also check for any entries with the same config path if you have previously modified this setting.

This isn’t a very good workaround as it would break cache-busting when using Varnish or any CDNs etc, but at least assets do not 404.

Thanks @careys7 : This solution worked.

I must have erased the .htaccess file in /pub/static by accident…I’ve been looking for the answer for 3 hours!

So if you’re missing your theme on your admin and your store… check for a versionXXXXXXXXXX in the firebug console. if so recreate the .htaccess file with the above 4 lines in it.

Thank you so much!

Saem problem version 2.4.0

Still happening on 2.3.5 with nginx.conf.sample

@zucler, we are closing this issue due to inactivity. If you’d like to update it, please reopen the issue.

I am using Fedora 25 , having Apache/2.4.25 (Fedora) PHP Version 7.0.18 MySql 5.7.18

I have just installed magneto 2.1.6 , after successfull setup run, its getting css and js files not found issue and they are loading from pub/static//version1494143163/frontend/Magento/luma/en_US/css/styles-m.css version1494143163 is not even existed. Beside this the admin url is also not accessible .

Can anyone can help me

Thanks @jmonschke - my nginx file was missing the fix added in #2323. Adding this works for me.

It seems that the other version regexes are redundant (or were broken prior to #2323). I think these should just be removed (see #7921).