1

I launch my script with:

node --inspect=1234 index.js

Then open Chrome dev tools Inspector and connect to it.

If I type console.log('hello') it works and outputs message to console.

However if I type any function or variable contained in my script, it throws an error:

Uncaught ReferenceError: "my func / var" is not defined(…)(anonymous function) @ VM107:1

How to make it see and enable to interact with the contents of my script?

1 Answer 1

5

Node.js treats every file as a CommonJS module. It means that everything you define in it is local to that module. When your run your script the function myFunc in the index.js is local to that module and is not available as a global object. Console works with global objects.

If you want to access the function from the console, you have to add it to the global object:

function f() {
    console.log('f');
}

global.f = f;
Sign up to request clarification or add additional context in comments.

5 Comments

This actually works, but it's going to be a pain since I have several functions.
but why do you need that? this is usually not the right way to do things
Previously my script has been written as a chrome extension and I'm very much used to the console where I could type my own commands, inspect vars and output objects. I'm desperately trying to gain this functionality and I don't know what to do.
I don't think there's anything you could do here. Besised node modules in Node.js, ES6 modules are already coming natively to browsers as well, so just probably start using module approach in your code and try to avoid everything global approach.
I would if it would be possible to do something like this: global=window;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.