Any fool can write code that a computer can understand. Good programmers write code that humans can understand. โ Martin Fowler
nodejs-addon is a template project for developing Node.js addons using C/C++. It includes support for formatting and linting both C/C++ and JavaScript code, tools for assuring code quality as well as instructions to code further using two of the most popular IDEs on the market. It can also be configured to use any of the three C/C++ APIs provided by Node.js.
Please download it and adapt it as you see fit.
Warning! This template is provided as is. If you feel you do not have enough documentation, you are always free to read its code. We are developers after all and we are forced daily to read other people's code.
The template is trying to cover multiple build systems as
- node-gyp (default), using Gyp build system
- cmake-js, using CMake build system
- xmake (under development)
- dependency-cruiser for code analisys, will check dependencies & how they're imported
- prettier for code formatting
- eslint for linting
- clang-format for code formatting
- clang-tidy for linting
- jscpd for code analisys as copy paste detector
- dependency-checker (designed by OWASP) for code analisys, will check dependencies & security
Git hooks are configured using husky
Found in .husky/pre-commit
, script will run
.scripts/configure.js
script, keeping your configuration stable, as well asca
andtest
scripts frompackage.json
.
Please take a look in the package.json
file and follow the two mentioned scripts to understand what they do and how they are called.
Found in .husky/commit-msg
, script will run a commitlint check. Please read more on the official page on how to customize commitlint config.
git
,gcc/g++
,make
clang-formatter
&&clang-tidy
- Python 3.6 or above.
- Depending on the build system, please install:
- NodeGyp: All requirements are set as default in the above list.
- CMake:
make
,cmake
- XMake:
make
,xmake
Do not forget NodeGyp is the main build system, so you need its requirements installed whatsoever.
# i.e ubuntu
PY_SUBVER=6 \
sudo apt-get install -y \
build-essential git make \
python3.$PY_SUBVER \
clang-format clang-tidy
# for CMake
sudo apt-get install cmake
# for XMake (see https://xmake.io/#/guide/installation)
bash <(curl -fsSL https://xmake.io/shget.text)
brew install git
brew install llvm && \
ln -s "$(brew --prefix llvm)/bin/clang-format" "/usr/local/bin/clang-format" && \
ln -s "$(brew --prefix llvm)/bin/clang-tidy" "/usr/local/bin/clang-tidy" && \
ln -s "$(brew --prefix llvm)/bin/clang-apply-replacements" "/usr/local/bin/clang-apply-replacements"
# for CMake
brew install cmake
# for XMake (see https://xmake.io/#/guide/installation)
bash <(curl -fsSL https://xmake.io/shget.text)
- git-scm tool.
make
- Make for Windows
- make from ezwinports
- From chocolatey, run
choco install make
- Python 3.6 or above
- Microsoft Build Tools 2017 or, at least Microsoft Visual Studio Community 2019
- Or, run
npm i -g windows-build-tools
(will silent install Microsoft Build Tools 2017)
- Or, run
- clang+llvm
- Or run
choco install llvm
- Or run
- For CMake
- cmake
- Or, run
choco install cmake --installargs 'ADD_CMAKE_TO_PATH=System'
- For XMake:
choco install git make python
# Visual Studio needs manual installation
choco install llvm
# for CMake
choco install cmake
# for XMake (see https://xmake.io/#/guide/installation)
Invoke-Expression (Invoke-Webrequest 'https://xmake.io/psget.text' -UseBasicParsing).Content
- Note that
node-gyp
doesn't support Python 2.7 anymore, so you'll need to install Python 3.6 or above. - #2 xmake is still under development and not compiling properly in debug mode
git clone https://github.com/templ-project/nodejs-addon your_project
cd your_project
rm -rf .git
git init
git add remote origin https://url/to/your/project/repository
git add .
git commit -am "init"
git push origin master
npm install
node .scripts/configure -a node-addon-api -x gyp -e vscode -ucl
Read about the configure command, integrated build systems, supported IDEs and NodeJs APIs.
Please install:
- NodeJs. We support version 12.x and above.
- A C++ IDE
- Visual Studio Code
- For Linux:
- gdb if you plan in using gdb as debug tool,
- if not, vadimcn.vscode-lldb will help you without any flaws
- For Linux:
- Jetbrains CLion
- Please help us supporting other IDEs as well
- Visual Studio Code
- A JavaScript/TypeScript IDE
- Visual Studio Code
- optional ITMCDev Node, Babel or TypeScript extension packs.
- Jetbrains WebStorm
- Please help us supporting other IDEs as well
- Visual Studio Code
Testing is done using mocha and chai.
Run unit tests using npm run test
.
Testing is currently set to use unittest.
We will try to provide a Jest implementation in the future. If you wish us to rush into it, please submit a ticket.
Run single unit tests file, by calling npm run test:single -- test/path/to/file.test.js
npm test:single -- test/index.test.js
Please check release-it for making releases to npmjs.com or any other repository tool, then run:
npm run release
- Dragos Cirjan - Initial work
Add a set of links to the issues page/website, so people can know where to add issues/bugs or ask for support.
(If the package is public, add licence) This project is licensed under the MIT License - see the LICENSE.md file for details