May 12, 2015 11:11 am

Bringing Node.js to Windows 10 IoT Core

This blog was written by Arunesh Chandra, Chakra Senior Program Manager, and Gaurav Seth, Chakra Principal PM Manager.

Microsoft is making big investments in the Internet of Things (IoT). Windows 10 IoT will power a range of intelligent, connected IoT devices. From small devices like gateways, to mobile point-of-sale to powerful industry devices like robotics and specialty medical devices. At the //build developer conference, we announced the availability of Windows 10 IoT Core Insider Preview with support for Raspberry Pi 2 and Intel’s Minnowboard Max, and talked about using Node.js for building IoT solutions on Window 10 IoT Core. There is an emerging trend of developers using Node.js for IoT scenarios, and we want to meet developers where they’re at and provide them the tools they need to be successful on the Windows 10 IoT Core platform.

Developers are currently unable to use Node.js on devices running Windows on ARM (WoA), which leaves a gap in their ability to target the entire family of Windows devices. We decided to fill this gap by allowing Node.js to take advantage of running with the Chakra JavaScript engine, which is part of the OS on Windows 10 and has been optimized to run efficiently on WoA devices since its inception. The use of built-in Chakra engine not only reduces the disk footprint impact when executing Node.js apps, but also enables us to bring first-class Universal Windows Platform (UWP) support to Node.js apps.

The project to bring this support to Windows 10 is in early stages of development. For now, the path we’ve chosen is to create a wrapper between the JSRT hosting APIs exposed by Chakra and the V8 hosting APIs that are currently used by Node.js (v0.12). The code for this wrapper is available publicly at github. These changes enable:

  • Node.js to run as a Classic Windows application on any device running Windows 10
  • Node.js to be hosted inside a Universal Windows application, which can run on all Windows10 devices
  • Full access to UWP APIs from Node.js applications
  • Visual Studio debugging of Node.js applications on Windows 10

We will be submitting a pull request to Node.js after stabilizing this code, fixing key gaps and responding to early community feedback.

The current set of changes not only enable developers to use Node.js on all devices running Windows 10, but also provide them an option to choose their preferred app model – Classic Windows v/s Universal Windows apps. Going forward, we plan to work closely with the Node Foundation, the Node.js Technical Committee(s), IO.js contributors and the community to discuss and participate in conversations around creating JavaScript engine agnostic hosting APIs for Node.js, which provide developers a choice of JavaScript engine that they would want to use in their Node.js workflow – all with the goal of making Node.js succeed across all platforms and devices.

Getting started with Node.js development on Windows IoT Core

The following section provides an overview of how to setup your development environment and pointers to sample code to help you get started with the changes to Node.js discussed above.

Setting up your development environment

Please make sure that you have signed up with our program on Microsoft Connect to get access to some of the downloads below.

Developing a Classic Windows application using Node.js

Classic Windows applications like a Win32 console application for Node.js are based on the typical Node.js programming model, which is known and familiar to Node.js developers today. This application model is probably the easiest way for current Node.js developers to try out Windows 10 IoT Core using their existing code.

Following is an example of a console application using Node.js to display memory usage of the system. The app uses a native addon to make low level system calls. While the details about building this sample can be found here, at a high level, it works just like any typical Node.js app.

Create a new file “server.js,” with the following content:

var addon = require("./MemoryStatusAddon");
 var http = require('http');
http.createServer(function (req, res) {
 var memObj = addon.GlobalMemoryStatusEx();
 res.writeHead(200, { 'Content-Type': 'text/plain' });
 res.write('*************************************************\n');
 res.write('Percent of memory in use: ' + memObj.load + '\n');
 res.write('KB of physical memory: ' + memObj.physKb + '\n');
 res.write('KB of free physical memory: ' + memObj.freePhysKb + '\n');
 res.write('KB of paging file: ' + memObj.pageKb + '\n');
 res.write('KB of free paging file: ' + memObj.freePageKb + '\n');
 res.write('KB of virtual memory: ' + memObj.virtualKb + '\n');
 res.write('KB of free virtual memory: ' + memObj.freeVirtualKb + '\n');
 res.write('KB of free extended memory: ' + memObj.freeExtKb + '\n');
 res.end('*************************************************\n');
 }).listen(1337);

Now on the remote PowerShell session run the following command:

Node1

The above command creates and starts an HTTP server on the device, which can be accessed over the network using a browser and the IP address of the device. The result looks like below:

Node2

Developing a Universal Windows application using Node.js

Universal Windows applications allow Windows developers to build an app once and target a large variety of Windows 10 devices including PCs, Tablets, Mobile Phones, XBox, HoloLens, IoT devices etc. NTVS IoT Extension Beta provides a Visual Studio template, which can be used to build Universal Windows apps using Node.js. This extension will also install the version of Node.js that uses the Chakra JavaScript engine and the uwp npm package.

Using this template developers can build, deploy and remotely run the app on an IoT device all from within Visual Studio.

Node3

More detailed instructions on how to create a basic Universal Windows webserver using Node.js are available in this sample.

Accessing Universal Windows Platform in Node.js applications

Universal Windows apps are incomplete without full access to underlying native UWP APIs that are a part of the Windows10 platform. By using the npm module called uwp, developers can use the ‘require’ pattern to enable access to UWP namespace from inside of Node.js. The NTVS IoT Extension Beta installs the uwp npm module by default, but the usual npm install syntax is sufficient to install the uwp module separately, if node.exe is using Chakra JS Engine.

The code below shows the use of UWP APIs inside a Node.js application to toggle the LEDs on or off with each request made to the server via the browser. Detailed instructions to setup the IoT device with breadboard and LEDs and see this code in action are available in the Blinky sample.

var http = require('http');
 var uwp = require("uwp");
 uwp.projectNamespace("Windows.Devices");
var gpioController = Windows.Devices.Gpio.GpioController.getDefault();
 var pin = gpioController.openPin(6);
 pin.setDriveMode(Windows.Devices.Gpio.GpioPinDriveMode.output)
 var currentValue = Windows.Devices.Gpio.GpioPinValue.high;
 pin.write(currentValue);
http.createServer(function (req, res) {
 if (currentValue == Windows.Devices.Gpio.GpioPinValue.high) {
 currentValue = Windows.Devices.Gpio.GpioPinValue.low;
 } else {
 currentValue = Windows.Devices.Gpio.GpioPinValue.high;
 }
 pin.write(currentValue);
 res.writeHead(200, { 'Content-Type': 'text/plain' });
 res.end('LED value: ' + currentValue + '\n');
 }).listen(1337);

The sample above uses uwp.projectNamespace(“Windows.Devices”) to include a scoped UWP namespace. If several namespaces need to be accessed in the same script, one can always use a higher level namespace like uwp.projectNamespace(“Windows”).

Visual Studio debugging support

One of main efforts for this project has been to bring the power of Visual Studio debugging to Node.js apps running with Chakra on Windows 10. With the work that has been done in Chakra, it’s as simple as ensuring –debug flag is set, setting break points in their Node.js script and start debugging in Visual Studio using F5. Once the breakpoint hits, developers have access to all the usual debug information like local variables, call stack etc. One of the key highlights of the support available is the remote debugging feature, which also works out of the box without any extra setup. Windows 10 IoT Core comes preconfigured to allow remote debugging using Visual Studio.

To remote debug, specify the IP address of your IoT device in the Visual Studio project settings pane:

Node4

Hit F5 to build / deploy / launch the app on the device with debugger attached:

Node5

What’s next?

We are excited to bring Node.js to Windows 10 IoT Core and eager get your feedback. Given that this is early work, certain things do not work as expected for now. Some known issues are related to incomplete support for VM module and npm packages like serialport, firmata etc. Also, developers have to go through multiple steps listed in the readme.md to setup the node environment to build from raw sources.

In coming weeks and months, we will continue to enable more Node.js scenarios and will be working towards higher npm package compatibility, smoothing out the developer workflow, improving performance, supporting io.js and working with the Technical Committee and community for a JavaScript engine agnostic interface for Node.js. We will be sharing more details on our ongoing efforts as they become available. We are excited to have embarked on this journey and are eager to listen and learn from the community. We are seeking feedback on this effort and are committed to drive towards making Node.js on Windows to be the best in class. You can share your feedback in the comments section below or open an issue at our repo on github. You can also connect with us on twitter at @wpdev and tell us what you think about #ChakraNode.

We can’t wait to see what you will make.

Updated July 21, 2015 12:29 pm

Join the conversation

  1. Are you running node.js directly on top of Chakra or still using Chrome’s V8? It is not clear to me. If you are using Chakra, how to you ensure compatibility with the “original” branch of Node which deeply implements V8 (and possibly some “bug-features)?

    • Yes, this project uses Chakra JavaScript engine instead of v8. The wrapper to bridge the API differences is published at the repo – https://github.com/Microsoft/node/. While some amount of differences would exist, we are in early stages of understanding/bridging those compatibility gaps. The main goal for this project is to allow Node developers to use Node on Windows10 IOT devices running ARM, where Node doesn’t run today.

      — Gaurav Seth [MSFT]

      • pretty late in reading this. I am both confused and curious. How will this project make sense if people start porting V8 to other IOT and ARM platforms? Then, Nodejs will be readily available.

  2. What about npm modules that extend node using node-gyp/C++? They depend on v8 headers, etc.

  3. Great to see this on Win 10 IoT – and it sounds like eventually we will be able to use node with Chakra for desktop Win 10 and not V8?

    One issue with node and V8 is that making larger apps more resilient to errors is difficult due to node’s error management, and forces you to break up apps into child processes to isolate the impact of an error (especially difficult if there is a buggy NPM module that you didn’t write…), but each child is a new v8 instance that chews up CPU and memory (10Mb per child – which can add up on small constrained devices). If using the inbuilt Chakra engine instead then it may be possible to reduce the overheads of using node child processes??

    • Yes, while our focus is Win10 IoT Core, this should work across all Win10 flavors. Perf is often scenario specific and it’ll be hard to comment on potential savings without looking through the specific scenario. That said, using Chakra, which is a system component, should bring some disk footprint savings, which might matter on small constrained devices.

      — Gaurav Seth [MSFT]

  4. I’m a little confused. Is this looking at creating a Node-Webkit implementation of applications, or solely NodeJS services?

    • This is not about node-webkit, This project enables using Node.js either inside a Win32 app (think classic windows apps and services) or in Universal Windows apps (single app package that can be installed onto a wide range of devices running Win10) – with a focus on bringing Node to IoT devices running Windows10 IoT Core SKU.

  5. Can we switch on currently Experimental features of Chakra like asm.js for the runtime?

    • Sorry, not at the moment – but we are looking into adding this support. Stay tuned!

      — Arunesh Chandra [MSFT|

  6. This is big news!
    Must be pretty hard and a lot of work to make this wrapper/proxy.
    Looking forward to hear more about this in future!

  7. For beginners:
    You need the step “Visual Studio debugging support” to deploy the source, too.
    Just fill in the properties, after that, the start button says ‘remote machine’ and viola, you deployed your first code. 🙂

    Regards, Tobi

  8. I’m breaking my head over this error when I want to debug:
    Could not find SDK “Microsoft.VCLibs.AppLocal, Version=14.0”

    I can’t find anything to fix this.
    I tried adding VCLibs package with NuGet, but cant set my node project to default app.
    Anyone else having this problem?

  9. when I debbug I get this error : in the VSoutput El programa ‘[2928] backgroundTaskHost.exe’ terminó con código -1073740791 (0xc0000409).