This how-to guide covers the steps, decisions, and implementation details thatare unique when crafting a Node-based snap. We’ll work through the aspectsunique to Node apps by examining an existing project.
The process of developing a snap for a Node app builds on top of npm andpackage.json manifests, making it possible to adapt or integrate an app’sexisting build tooling into the crafting process.
The following code comprises the project file of a Node tool,wethr. This project is a CLI tool for obtaininglocal weather information.
name:wethrversion:gitsummary:Command line weather tool.description:|Get current weather:-$ wethrGet current weather in metric units$ wethr --metricGet current weather in imperial units$ wethr --imperialconfinement:strictbase:core20apps:wethr:command:bin/wethrparts:wethr:source:.plugin:npmnpm-node-version:14.16.1
parts:wethr:source:.plugin:npmnpm-node-version:14.16.1
Node parts are built with theNPM Plugin.
To declare a Node part:
Declare the general part keys, such assource,override-build,build-packages, and so on.
Setplugin:npm.
Determine how npm is added to the snap:
If you want Snapcraft to download and pack npm, setnpm-include-node:true, and then setnpm-node-version tothe required NPM version.
If you’d rather manually pack npm into the snap, don’t set either ofthese keys. Instead, manually include a copy of npm in the files, anddeclare it in another part.
Setnpm-node-version to the required NPM version.