Table of Contents

Node api

As of CSharpToJavaScript 0.2.3

There are a couple of ways to call node js using attributes, but the simplest that I found is to use ImportAttribute with IgnoreAttribute and ValueAttribute.

cstojs_options.xml:

<ProjectOptions>
  <Output Folder="./Output" />
  <File Source="./Program.cs" />
</ProjectOptions>

Program.cs:

using CSharpToJavaScript.Utils;
namespace N;

[Import("scheduler", "node:timers/promises")]
[Value("scheduler")]
[Ignore]
public static class Scheduler
{
    [Value("wait")]
    public static Task<bool> Wait(int delay) { throw new System.NotImplementedException(); }
}

public class Main
{
    static Main()
    {
        Main.StartPause();
    }

    public async static void StartPause()
    {
        Console.WriteLine("Before the pause!");
        await Scheduler.Wait(1000);
        Console.WriteLine("After the pause!");
    }
}

Translated js:

import { scheduler } from 'node:timers/promises';

class Main
{
    static
    {
        Main.StartPause();
    }

    static async StartPause()
    {
        console.log("Before the pause!");
        await scheduler.wait(1000);
        console.log("After the pause!");
    }
}

See the full source code: https://github.com/TiLied/CSTOJS_Pages/tree/main/tutorials/NodeApi_Example