|
| 1 | + |
| 2 | +## Minimal Example .NET AOT NPM Package |
| 3 | +The `lib/Example.cs` class defines a Node.js add-on module that is AOT-compiled, so that it does not |
| 4 | +depend on the .NET runtime. The AOT module is then packaged as an npm package. The `app/example.js` |
| 5 | +script loads that _native_ module via its npm package and calls a method on it. The script has |
| 6 | +access to type definitions and doc-comments for the module's APIs via the auto-generated `.d.ts` |
| 7 | +file that was included in the npm package. |
| 8 | + |
| 9 | +| Command | Explanation |
| 10 | +|-------------------------------|-------------------------------------------------- |
| 11 | +| `dotnet pack ../..` | Build Node API .NET packages. |
| 12 | +| `cd lib`<br/>`dotnet publish` | Install Node API .NET packages into lib project; build lib project and compile to native binary; pack npm package. |
| 13 | +| `cd app`<br/> `npm install` | Install lib project npm package into app project. |
| 14 | +| `node example.js` | Run example JS code that calls the library API. |
| 15 | + |
| 16 | +### Building multi-platform npm packages with platform-specific AOT binaries |
| 17 | +Native AOT binaries are platform-specific. The `dotnet publish` command above creates a package |
| 18 | +only for the current OS / CPU platform (aka .NET runtime-identifier). To create a multi-platform |
| 19 | +npm package with Native AOT binaries, run `dotnet publish` separately for each runtime-identifier, |
| 20 | +and only create the package on the last one: |
| 21 | +``` |
| 22 | +dotnet publish -r:win-x64 -p:PackNpmPackage=false |
| 23 | +dotnet publish -r:win-arm64 -p:PackNpmPackage=true |
| 24 | +``` |
| 25 | + |
| 26 | +To create a fully cross-platform packatge, it will be necessary to compile on each targeted OS |
| 27 | +(Windows, Mac, Linux), then copy the outputs into a shared directory before creating the final |
| 28 | +npm package. |
0 commit comments