Use a manually installed node in a Function node?

I am looking to translate text. Like many I installed the node-red-contrib-google-translate node with the Palette.
Then I wanted to go further and translate text into a Function node.
I found @vitalets/google-translate-api.
I was going to install it with npm, but first, out of curiosity, and because google-translate is already installed in the Palette, I went to my node_modules folder and saw that @vitalets/google-translate-api is already installed (you call it a dependency I think?)

My question: how can I use this module to translate text in a Function node?

In the Github of @vitalets/google-translate-api he describes a use with node.js:

import { translate } from '@vitalets/google-translate-api';
const { text } = await translate('Привет, мир! Как дела?', { to: 'en' });
console.log(text) // => 'Hello World! How are you?'

I replace console.log(text) by node.warn(text) But that's not enough :laughing:

so I ran into an alert: SyntaxError: Cannot use import statement outside a module (body:line 2)

Is there a setting to add in settings.js ? or another line of javascript code to change in my Function? (It may help me in the future, when I install other modules manually)

You can define it on the setup tab of the function.

Then use code:

//import { translate } from '@vitalets/google-translate-api';
const  text  = await translate_api.translate ('Привет, мир! Как дела?', { to: 'en' });
console.log(text) // => 'Hello World! How are you?'
msg.payload = text;
return msg;

[{"id":"7f41b7600903dcec","type":"inject","z":"31db2d7b1f86fabe","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":840,"y":120,"wires":[["b7e5e1dfedfd757a"]]},{"id":"0e3bc3f4aa7f06db","type":"debug","z":"31db2d7b1f86fabe","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1230,"y":120,"wires":[]},{"id":"b7e5e1dfedfd757a","type":"function","z":"31db2d7b1f86fabe","name":"","func":"//import { translate } from '@vitalets/google-translate-api';\nconst  text  = await translate_api.translate ('Привет, мир! Как дела?', { to: 'en' });\nconsole.log(text) // => 'Hello World! How are you?'\nmsg.payload = text;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"translate_api","module":"@vitalets/google-translate-api"}],"x":1020,"y":120,"wires":[["0e3bc3f4aa7f06db"]]},{"id":"c657668bfffb92d7","type":"comment","z":"31db2d7b1f86fabe","name":"","info":"","x":1030,"y":60,"wires":[]}]
1 Like

Great it works! THANKS

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.