Skip to content
/ napi-addon-template Public template

Template for building C++ addons for Node (N-API)

Notifications You must be signed in to change notification settings

Phamier/napi-addon-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

N-API addon

This is a project template for N-API addon. To create a new project based on this template using degit:

npx degit Phamier/napi-addon-template your-napi-project
cd your-napi-project

Note that you will need to have Node.js installed.

Getting started

Installing the depedencies.

npm install

Building in production

npm install automatically builds your addon after installing dependencies. There is no need to do anything else.

Development

Building

If you modify binding.gyp or add new source files (src/*.cpp), you should run ...

npm run rebuild:dev

If you modify an existing source file, npm run build:dev will do the job.

npm run build:dev

Also there is a cleaning command...

npm run clean

Declaration reference and JSDoc

You can add declarations to index.d.ts with JSDoc documentation comments for improved Intellisence and documentaion. Example:

/**
 * Hello World function
 * @returns {string} "HelloWorld"
 */
export function helloWorld(): string;

VS Code Tips and Tricks

Configure includePath for better IntelliSence results

If you are using Microsoft C/C++ extension, you can add custom includePath for your project by modifying c_cpp_properties.json :

"configurations": [{
    ...
    "includePath": [
        "${workspaceFolder}/node_modules/node-addon-api/",
        "${workspaceFolder}/include/**",
        "/usr/local/include/**"    
    ],
    ...

Debugging

Note: this method was proposed by Arul R. Basically, this template is an improved version his and I'm using the same debugging method as he does. Check out his blog post on meduza.com for more information. I will only provide my launch.json and tasks.json files.
launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type":"lldb",
            "request": "launch",
            "name": "Debug",
            "preLaunchTask": "npm rebuild",
            "program": "npm",
            "args": "run test"
        }
    ]
}

tasks.json:

{
    "version" : "2.0.0",
    "tasks": [
        {
            "type": "npm",
            "script": "rebuild:dev",
            "problemMatcher": [],
            "label": "npm rebuild",
            "detail": "node-gyp rebuild --debug"
        }
    ]
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

About

Template for building C++ addons for Node (N-API)

Resources

Stars

Watchers

Forks

Packages

No packages published