roc/examples/nodejs-interop/native-c-api
dependabot[bot] 3200d042cf
Bump ip from 2.0.0 to 2.0.1 in /examples/nodejs-interop/native-c-api
Bumps [ip](https://github.com/indutny/node-ip) from 2.0.0 to 2.0.1.
- [Commits](https://github.com/indutny/node-ip/compare/v2.0.0...v2.0.1)

---
updated-dependencies:
- dependency-name: ip
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-02-20 22:40:03 +00:00
..
platform Split nodejs wasm and napi examples 2023-05-01 15:54:53 -04:00
addon.d.ts Split nodejs wasm and napi examples 2023-05-01 15:54:53 -04:00
binding.gyp Split nodejs wasm and napi examples 2023-05-01 15:54:53 -04:00
demo.c switch dbg arg ordering to make it backwards compatible 2023-12-02 21:18:32 -08:00
hello.ts Split nodejs wasm and napi examples 2023-05-01 15:54:53 -04:00
main.roc update to new interpolation syntax 2024-02-02 13:39:10 +01:00
package-lock.json Bump ip from 2.0.0 to 2.0.1 in /examples/nodejs-interop/native-c-api 2024-02-20 22:40:03 +00:00
package.json Split nodejs wasm and napi examples 2023-05-01 15:54:53 -04:00
README.md Split nodejs wasm and napi examples 2023-05-01 15:54:53 -04:00

TypeScript Interop

This is an example of calling Roc code from TypeScript on Node.js.

Installation

You'll need to have a C compiler installed, but most operating systems will have one already. (e.g. macOS has clang installed by default, Linux usually has gcc by default, etc.) All of these commands should be run from the same directory as this README file.

First, run this to install Node dependencies and generate the Makefile that will be used by future commands. (You should only need to run this once.)

npm install
npx node-gyp configure

Building the Roc library

First, cd into this directory and run this in your terminal:

roc build --lib

This compiles your Roc code into a shared library in the current directory. The library's filename will be libhello plus an OS-specific extension (e.g. libhello.dylib on macOS).

Next, run this to rebuild the C sources.

npx node-gyp build

Finally, run this to copy the generated TypeScript type definitions into the build directory:

cp addon.d.ts build/Release/

You can verify that TypeScript sees the correct types with:

npx tsc hello.ts

Try it out!

Now that everything is built, you should be able to run the example with:

npx ts-node hello.ts

To rebuild after changing either the demo.c file or any .roc files, run:

roc build --lib && npx node-gyp build

About this example

This was created by following the NodeJS addons tutorial and switching from C++ to C, then creating the addon.d.ts file to add types to the generated native Node module.