I encountered the same issue. The code causing the error is:
however the same code runs well in truffle console.
There is also a warning before the error, as this:
No web3 detected. Falling back to http://localhost:8545. …
I don’t know if it’s wrong cause according to the tutorial my network is running on this address.
Do anyone has any idea conserning this?