How to install Node.js library on Linux
First of all, you need to install Node.js.
Before updating Node.js, please be sure to remove old versions:
1
sudo apt-get purge node nodejs node.js -y
2
sudo apt-get autoremove
Copied!

Automatic Node.js installation

Add repositories:
1
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
Copied!
Install Node.js:
1
sudo apt-get update && sudo apt-get upgrade
2
sudo apt-get install build-essential nodejs -y
Copied!

Manual Node.js installation

Automatic install might not work for you, in this case you can perform manual installation. If uname -m gives you armv6l (on Raspberry Pi, usually), try this:
1
sudo su
2
3
cd /opt
4
5
wget https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-armv6l.tar.gz -O - | tar -xz
6
mv node-v6.9.5-linux-armv6l nodejs
7
8
apt-get update && apt-get upgrade
9
apt-get install build-essential
10
11
ln -s /opt/nodejs/bin/node /usr/bin/node
12
ln -s /opt/nodejs/bin/node /usr/bin/nodejs
13
ln -s /opt/nodejs/bin/npm /usr/bin/npm
14
15
exit
16
17
export PATH=$PATH:/opt/nodejs/bin/
Copied!

Check your Node.js and npm installation

1
[email protected]:/ $ node --version
2
v6.9.5
3
4
5
3.10.10
Copied!

Install Blynk globally

1
sudo npm install blynk-library -g
2
sudo npm install onoff -g
Copied!
Run default Blynk client (replace YourAuthToken):
1
export PATH=$PATH:/opt/nodejs/bin/
2
unset NODE_PATH
3
blynk-client YourAuthToken
Copied!

Creating a new Node.js project with Blynk

Installing Blynk globally may not work or can be undesired. In this case, you need to create a new Node.js module with local Blynk library dependency.
1
mkdir my-awesome-project
2
cd my-awesome-project
3
npm init
Copied!
It will prompt you for general information about your project and create a package.json file (project description). Next, add Blynk to your project:
1
npm install blynk-library --save
Copied!
You can also install onoff, if you want (allows direct pin operations):
1
npm install onoff --save
Copied!
Now create your main script file index.js (just replace YourAuthToken):
1
var Blynk = require('blynk-library');
2
3
var AUTH = 'YourAuthToken';
4
5
var blynk = new Blynk.Blynk(AUTH);
6
7
var v1 = new blynk.VirtualPin(1);
8
var v9 = new blynk.VirtualPin(9);
9
10
v1.on('write', function(param) {
11
console.log('V1:', param[0]);
12
});
13
14
v9.on('read', function() {
15
v9.write(new Date().getSeconds());
16
});
Copied!
This is it. Run your project:
1
node index.js
Copied!
You should see something like:
1
OnOff mode
2
Connecting to: blynk-cloud.com 8441
3
SSL authorization...
4
Connected
5
Authorized
Copied!
Write our own script based on examples!

What next?

Read about Virtual Pins concept and unleash full power of Blynk. Blynk supports huge amount of board types. Check if your favourite is on the list! Our library is Open Source Software. Give us a star on GitHub.

Troubleshooting

If you are trying to connect to Blynk cloud, and get an error like:
1
npm ERR! Error: SSL Error: CERT_NOT_YET_VALID
Copied!
you should use the date command to update current system time.

Further reading

Last modified 4mo ago