npm

Create and publish an npm module using C++.


Font_Awesome_5_brands_npm
 by Font Awesome is licensed under CC

Recursion

When a function is called, the computer must "remember" the place it was called from... so that it can return to that location with the result once the call is complete. Typically, this information is saved on the call stack... For tail calls, there is no need to remember the caller...

https://en.wikipedia.org/wiki/Tail_call

Some interpreters (and compilers) eliminate the stack frame creation and destruction work when they recognize tail recursion.

C++

Create a folder for the *new* node module.

$ mkdir haddley-factorial-cc

Navigate to the new folder

$ cd haddley-factorial-cc

Create a package.json file

$ npm init -y

addon.c
addon.js
binding.gyp

Mocha and Chai

Automated testing is added using mocha and chai.

$ npm i --save-dev mocha

$ npm i --save-dev chai

$ mkdir test

test/test.js

npm test

package.json

This is the final package.json file

package.json

Github actions

Github actions ensure that testing is performed using multiple versions of node.

node.js.yml

Tests are running

Code passed all of the provided tests

Test results for multiple versions of node

Publishing to npmjs.com

To publish the module to npmjs.com

$ node login

$ node publish

npm publish