Code does not work in function node but works fine when run separately