question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot read property 'MutationObserver' of undefined

See original GitHub issue
nunjucks=2.0.3
node=4.0.0
webpack=1.12.2

Getting an error when importing nunjucks:

Uncaught TypeError: Cannot read property 'MutationObserver' of undefined

looks like this bit of code is triggering it:

var BrowserMutationObserver=global.MutationObserver || global.WebKitMutationObserver;

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ediblecodecommented, Dec 14, 2016

As a workaround, you could use string-replace-webpack-plugin and the following in webpack.config.js. Not ideal but it works:

postLoaders: [{
	test: /\.js$/,
	loader: StringReplacePlugin.replace({
		replacements: [
			// Fix for https://github.com/mozilla/nunjucks/issues/520
			// Because of https://github.com/mozilla/nunjucks/blob/master/browser/nunjucks-slim.js#L1212
			{
				pattern: /global\.MutationObserver/g,
				replacement: function () {
					return "window.MutationObserver";
				}
			},
			{
				pattern: /global\.WebKitMutationObserver/g,
				replacement: function () {
					return "window.WebKitMutationObserver";
				}
			}
		]
	})
}]
1reaction
fdintinocommented, May 18, 2019

I’ve looked into it more closely, and I think you are mistaken. This was fixed in asap 2.0.5 (https://github.com/kriskowal/asap/commit/c5b65d2c0a7dd62c0bd11990b8531f50290072ca), and that fix has been bundled with nunjucks since v3.0.1 (see here). Here it is in the 3.2.0 release:

https://github.com/mozilla/nunjucks/blob/d0ab8d6bf4d7b91fb2c2883f95b172f776d4e531/files/nunjucks-slim.js#L2032-L2033

(In a browser, self in the global scope returns window (MDN Window.self).

There are two possibilities: either you are using Nunjucks 2.x, or you have an old version of asap installed in your own project, and rollup is bundling that.

My motivation for investigating further is that I have been using nunjucks with rollup since 2017, when I took on maintenance of this project (and when 3.0.1 was released), and I’ve never needed a workaround.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read function of undefined - jquery - Stack Overflow
1 Answer 1 ... mutationLists is a property of $.event.special.changeElement which is a function. Having properties as functions of an object acts ...
Read more >
Uncaught TypeError: Cannot read property '$el' of undefined
I'm open to any suggestions. Uncaught TypeError: Cannot read property '$el' of undefined at e.handler (uikit.min.js:3) at uikit ...
Read more >
Cannot read property 'MutationObserver' of undefined?
uni-app,mui,nativejs,dcloud,HBuilder,HBuilderX,HTML5,流应用,数字天堂网络,数字天堂,web,前端开发,web开发,跨平台,开发工具,ide,css,javascript.
Read more >
MutationObserver.takeRecords() - Web APIs | MDN
The MutationObserver method takeRecords() returns a list of all matching DOM changes that have been detected but not yet processed by the ...
Read more >
Error "Cannot read properties of null (reading 'ActivityLogs ...
I'm having lots or problems deploying one of our projects to the TEST environment/server. Other projects are working fine including ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found