In OB2 add a Script block, then change the interpreter to NodeJS and finally paste your code inside. If you need npm packages you have to go to the Scripts folder (inside the OB2 root folder) and do
npm init
and install your packages in there.
Then run the script block and it will correctly work.