Chrome DevTools error: "Failed to save to temp variable."

node.jsGoogle ChromeGoogle Chrome-Devtools

node.js Problem Overview


I am using Node Monkey to debug my NodeJS app.

It happens frequently that when I click "Store as Global Variable" in my Chrome Console it says "Failed to save to temp variable."

enter image description here

console.log({why:'dont', you:'work?'})

It happens also in this jsfiddle

  1. Am I doing something wrong?

  2. Why this happens?

Chrome: 50.0.2661.102 (64-bit) OSX El Capitan 10.11.4

node.js Solutions


Solution 1 - node.js

I can see two reasons why Store As Global Variable wouldn't work:

1. Wrong Console Context Selected

This is arguably a Chrome bug, but you can only store an object as a global if the console is set to the same context as the code that logged the object.

In practice this means you might need to select the correct IFrame or web worker.

For example, on jsFiddle:

In the normal jsFiddle editor page context I'm getting an error. But it works if I change the context to the contents of the fiddle itself:

2. Garbage Collection

In order for Chrome to give you a reference to the object the object still has to be in memory. If that isn't the case it can only throw an error.

However, I'm pretty sure that being shown in the console forces V8 to keep a reference to the value.

Solution 2 - node.js

You need to create the object in the Console itself, as the reference to the object needs to be maintained by Chrome. Just put the following into the console instead:

{why:'dont', you:'work?'}

Console

If you check out this revision where the feature was added, it says:

> Adding ability to access objects from printed ObjectPropertySections > (console, scopes pane and etc.).

The problem, based on my understanding, is that console.log is outputting a string representation of the object, and just using object formatters to display it nicely. The object doesn't exist anymore. When you create an object through the console itself, Chrome is storing the object itself in memory. If you have paused on a breakpoint and have locally scoped variables, these can also be stored globally because they are also in memory.

One thing you could do in your code, if you haven't got circular references is:

console.log(JSON.stringify({why:'dont', you:'work?'}));
> {"why":"dont","you":"work?"}

In the Console, copy the output and paste it into a JSON.parse call:

JSON.parse('{"why":"dont","you":"work?"}');
> Object {why: "dont", you: "work?"}

The variable exists now in memory so you can store it.

Solution 3 - node.js

press Ctrl+Shift+C and select any element inside the frame that prints the object, then try again.

This will fix the problem.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDevid FarinelliView Question on Stackoverflow
Solution 1 - node.jsMatt ZeunertView Answer on Stackoverflow
Solution 2 - node.jsGideon PyzerView Answer on Stackoverflow
Solution 3 - node.jsManar GulView Answer on Stackoverflow