pixiedust / pixiedust_node

Jupyter magic to allow Node.js code to run in a notebook
https://medium.com/ibm-watson-data-lab/running-node-js-notebooks-in-watson-studio-a8f6545d8299
Apache License 2.0
213 stars 24 forks source link

TypeError: Converting circular structure to JSON at JSON.stringify #32

Open chriszrc opened 6 years ago

chriszrc commented 6 years ago

I'm attempting to use the node-postgres library from pixiedust_node. The following simple setup fails:

import pixiedust_node
npm.install(('node-fetch', 'pg')) 
var { Pool } = require('pg');
var pool = new Pool({
  user: 'congress23',
  host: 'localhost',
  database: 'vol_congress23',
  password: '',
  port: 5431,
});
pool.query('SELECT NOW()', (err, res) => {
    console.log(err,res);
});

Oddly, the error occurs even when trying to console.log("it works") instead of the results, so I'm not even sure where/what is causing the circular reference error. The same code executes just fine when run directly from node. Here's the stacktrace:

TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
at globalVariableChecker (/anaconda3/lib/python3.6/site-packages/pixiedust_node/pixiedustNodeRepl.js:26:22)
at REPLServer.writer (/anaconda3/lib/python3.6/site-packages/pixiedust_node/pixiedustNodeRepl.js:67:5)
at finish (repl.js:512:38)
at REPLServer.defaultEval (repl.js:279:5)
at bound (domain.js:301:14)
at REPLServer.runBound [as eval] (domain.js:314:12)
at REPLServer.onLine (repl.js:468:10)
at emitOne (events.js:116:13)
at REPLServer.emit (events.js:211:7)
er
/anaconda3/lib/python3.6/site-packages/pixiedust_node/pixiedustNodeRepl.js:26
const j = JSON.stringify(r.context[v]);
^
TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
at Timeout.globalVariableChecker [as _onTimeout] (/anaconda3/lib/python3.6/site-packages/pixiedust_node/pixiedustNodeRepl.js:26:22)
at ontimeout (timers.js:482:11)
at Timer.unrefdHandle (timers.js:595:5)
chriszrc commented 6 years ago

Ah, looking at the pixiedustNodeRepl source code, I see that global variables are converted for sending to python. Looks like maybe a circular reference check needs to be put in place here? I can confirm that if I use let instead of var, I don't get the error-

shmuel-web commented 4 years ago

@chriszrc I experienced the same issue as well

and removing the variable from the global scope and moving it into a function scope solved it for me

thanks!