2

I have dart code that returns a string when it is ran

main(){
print("hello");
 return "hello";
}

Now i need to call this from js so i used dart2js to convert it to js.

How i call it from a node.js application:

var sudokuLib = require('./sudoku-lib');
console.log(require('./sudoku-lib'));

I am aware of the large amount of code created by dart2js and it is not a problem since whenever i run the converted js file or the above js code, hello is being printed in the console. But the problem is that I want the returned value to be stored in a variable in my js code, since i will be using the value in my node application.

Is there any way to do this??

2
  • 3
    Why does the function have a void type signature if it's supposed to return a string? Commented Jan 1, 2022 at 15:36
  • my bad, but even the editted code doesn't work @Pointy Commented Jan 1, 2022 at 15:51

1 Answer 1

1

I will assume that your dart code is now js code and runing fine :

For exemple like this sudoku-lib.js :

function main() {
    return "Hello"
}

// add the code below
module.exports = { main };

in a new js file "file.js" you can simply write :

const lib = require("./sudoku-lib");
// store it in result var
const result = lib.main();
console.log(`The result is: ${result}`);

Ref node-modules-import-and-use-functions-from-another-file

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.