Bundler & Minifier generating "Object reference not set to an instance of an object" error

Viewed 5599

I trying bundle and minify my CSS and JS files in ASP.NET Core 3.0 with BuildBundlerMinifier.Core. Also Installed Bundler & Minifier extension for Visual Studio 2019.

When I try Debug the project, it working correctly without error but when I rebuild it, I'm getting the following error;

Object reference not set to an instance of an object

enter image description here

I don't get an error when restart Visual Studio and I can debug the project again but If I rebuild it again, I'm getting the error.

It's bundleconfig.json file

[
  {
    "outputFileName": "wwwroot/assets/css/style.bundle.min.css",
    "inputFiles": [
      "wwwroot/assets/plugins/global/plugins.bundle.css",
      "wwwroot/assets/css/style.bundle.css",
      "wwwroot/assets/css/skins/header/base/light.css",
      "wwwroot/assets/css/skins/header/menu/light.css",
      "wwwroot/assets/css/skins/brand/light.css"
    ],
    "sourceMap": false,
    "sourceMapRootPath": null
  }
,
{
  "outputFileName": "wwwroot/assets/js/scripts.min.js",
  "inputFiles": [
    "wwwroot/assets/plugins/global/plugins.bundle.js",
    "wwwroot/assets/js/scripts.js",
    "wwwroot/assets/js/pages/dashboard.js",
    "wwwroot/content/js/mobil-detect.js",
    "wwwroot/content/js/lazy-img.js",
    "wwwroot/content/js/ziyaretci.js"
  ]
}
]

enter image description here

Visual Studio, Bundler & Minifier extension, BuildBundlerMinifier and BuildBundlerMinifier.Core are up to date How can I fix it ?

5 Answers

This can be an issue with the syntax in the source of one of your js files. In my case, using the nullish coalescing operator "??" was the issue. Replaced it with a simple "if" null check and the compilation error disappeared.

Solved by refactoring all "??" nullish coalescing operators with "?:" conditional (ternary) operators.

In my case it was because of the optional chaining operator, i.e. ?.

The project has BuildBundlerMinifier and BuildBundlerMinifier.Core. I removed BuildBundlerMinifier from nuget and now it works.

If you happen (like me) to have a hash (#) somewhere in your directory name minification will be broken with .NET Core 3.1 using BuildBundlerMinifier.

It took me some time to figure that out including uninstalling and reinstalling nuget packages and Visual Studio extensions.

I'm wondering if there are also other special characters causing the issue.

You can easily see this in bundleconfig.json.

Example folder structure:

C:\MyProjects\C#Test\MySolution\MyProject\wwwroot\js\

If broken it will look like:

[
  {
    "outputFileName": "#Test\\MySolution\\MyProject\\wwwroot\\js\\Math.min.js",
    "inputFiles": [
      "#Test\\MySolution\\MyProject\\wwwroot\\js\\Math.js"
    ]
  }
]

instead of

[
  {
    "outputFileName": "wwwroot/js/Math.min.js",
    "inputFiles": [
      "wwwroot/js/Math.js"
    ]
  }
]
Related