-
Install the .NET SDK for macOS:
-
Download and install the .NET SDK for macOS.
-
After installation, you can verify it by running:
dotnet --version
-
-
Create a New Class Library Project:
-
Open the Terminal, navigate to your desired directory, and create a new class library project by running:
dotnet new classlib -o MyDotNetLibrary
-
This creates a folder named
MyDotNetLibrarywith a basic class library template.
-
-
Edit the Code:
-
Navigate to the project folder and open the generated
Class1.csfile with your preferred code editor (VS Code, Sublime, etc.):cd MyDotNetLibrary -
Replace the content with your code:
using System; using NodeApi.DotNet; namespace MyDotNetLibrary { public class MyMathClass { public int Add(int a, int b) => a + b; [JSExport] public static string SayHello(string name) => $"Hello, {name} from .NET!"; } }
-
-
Build the DLL:
-
Build the project to produce the
.dllfile:dotnet build
-
This generates a
.dllfile in thebin/Debug/net6.0/directory by default.
-
-
Locate the DLL File:
- You can find
MyDotNetLibrary.dllin thebin/Debug/net6.0/directory within the project folder.
- You can find
-
Use the DLL in Node.js:
- With the
.dllfile ready, you can now load and interact with it in a Node.js application using thenode-api-dotnetpackage.
- With the
import dotnet from 'node-api-dotnet/net6.0';
dotnet.load('path/to/MyDotNetLibrary.dll');
const MyMathClass = dotnet.MyDotNetLibrary.MyMathClass;
const instance = new MyMathClass();
console.log(instance.Add(3, 5)); // Example usage