nfc-pcsc
Easy reading and writing NFC tags and cards in Node.js
Built-in support for auto-reading card UIDs and reading tags emulated with Android HCE.
NOTE: Reading tag UID and methods for writing and reading tag content depend on NFC reader commands support. It is tested to work with ACR122 USB reader but it should work with all PC/SC compliant devices.
When detecting tags does not work see Alternative usage.
This library uses pcsclite native bindings pokusew/node-pcsclite under the hood.
Psst! Problems upgrading to 0.6.0? Check out this migration note.
Content
- Installation
- Flow of handling tags
- Basic usage
- Alternative usage
- Reading and writing data
- More examples
- FAQ
- Migration from older versions to 0.6.0
- Can I use this library in my Electron app?
- Can I use this library in my angular-electron app?
- Do I have to use Babel in my app too?
- Which Node.js versions are supported?
- How do I require/import this library?
- Can I read a NDEF formatted tag?
- Can I use this library in my React Native app?
- Frequent errors
- License
Installation
Requirements: at least Node.js 8 or newer (see this FAQ for more info)
Note: This library can be used only in Node.js environments on Linux/UNIX, macOS and Windows. Read why here.
-
Node Native Modules build tools
Because this library (via pokusew/node-pcsclite under the hood) uses Node Native Modules (C++ Addons), which are automatically built (using node-gyp) when installing via npm or yarn, you need to have installed C/C++ compiler toolchain and some other tools depending on your OS.
Please refer to the node-gyp > Installation for the list of required tools depending on your OS and steps how to install them.
-
PC/SC API in your OS
On macOS and Windows you don't have to install anything, pcsclite API is provided by the OS.
On Linux/UNIX you'd probably need to install pcsclite library and daemon**.
For example, in Debian/Ubuntu:
apt-get install libpcsclite1 libpcsclite-devTo run any code you will also need to have installed the pcsc daemon:
apt-get install pcscd -
Once you have all needed libraries, you can install nfc-pcsc using npm:
npm install nfc-pcsc --saveor using Yarn:
yarn add nfc-pcsc
Flow of handling tags
When a NFC tag (card) is attached to the reader, the following is done:
-
it tries to find out the standard of card (
TAG_ISO_14443_3
orTAG_ISO_14443_4
) -
it will connect to the card, so any other card specific commands could be send
-
handling of card
-
when
autoProcessing
is true (default value) it will handle card by the standard:TAG_ISO_14443_3
(MIFARE Ultralight, 1K ...): sends GET_DATA command to retrieve card UID
TAG_ISO_14443_4
(e.g.: Android HCE): sends SELECT_APDU command to retrieve data by filethen
card
event is fired, for which you can listen and then you can read or write data on the card
see Basic usage how to do it -
when
autoProcessing
is false (default value) it will only firecard
event
then you can send whatever commands you want usingreader.transmit
method
see Alternative usage how to do it
-
-
you can read data, write data and send other commands
Basic usage
Running examples locally
If you want see it in action, clone this repository, install dependencies with npm and run
npm run example
. Of course, instead of npm you can Yarn if you want. See scripts section of package.json for all available examples run commands.git clone https://github.com/pokusew/nfc-pcsc.gitcd nfc-pcscnpm installnpm run example
You can use this library in any Node.js 8+ environment (even in an Electron app).
// in ES6; // without Babel in ES2015const NFC = ; const nfc = ; // optionally you can pass logger nfc; nfc;
Alternative usage
You can disable auto processing of tags and process them yourself. It may be useful when you are using other than ACR122 USB reader or non-standard tags.
// in ES6; // without Babel in ES2015const NFC = ; const nfc = ; // optionally you can pass logger nfc; nfc;
Reading and writing data
You can read from and write to numerous NFC tags including MIFARE Ultralight (tested), MIFARE Classic, MIFARE DESFire, ...
Actually, you can even read/write any possible non-standard NFC tag and card, via sending APDU commands according card's technical documentation via
reader.transmit
.
Here is a simple example showing reading and writing data to simple card without authenticating (e.g. MIFARE Ultralight):
See Basic usage how to set up reader or look here for full code
reader;
More examples
📦📦📦 You can find more examples in examples folder, including:
- read-write.js – detecting, reading and writing cards standard ISO/IEC 14443-3 cards (NTAG, MIFARE Ultralight, ...)
- mifare-classic.js – authenticating, reading and writing MIFARE Classic cards
- mifare-desfire.js – authenticating and accessing data on MIFARE DESFire cards
- mifare-ultralight-ntag.js – an example implementation of Mifare Ultralight EV1 and NTAG specific commands
- basic.js – reader events explanation
- led.js – controlling LED and buzzer of ACR122U reader
- uid-logger.js – logs uid when a card is detected
Feel free to open pull request, if you have any useful example, that you'd like to add.
FAQ
Migration from older versions to 0.6.0
There was a breaking change in 0.6.0, as the default export was removed (because of non-standard behaviour of ES6 modules in ES5 env (see #12 and v0.6.0 release changelog)).
You have to update all requires or imports of this library to the following (note the brackets around NFC):
// in ES6 environment; // in ES2015 environmentconst NFC = ;
Electron app?
Can I use this library in myYes, you can! It works well.
But please note, that this library uses Node Native Modules (underlying library pokusew/node-pcsclite which provides access to PC/SC API).
Read carefully Using Native Node Modules guide in Electron documentation to fully understand the problematic.
Note, that because of Node Native Modules, you must build your app on target platform (you must run Windows build on Windows machine, etc.).
You can use CI/CD server to build your app for certain platforms.
For Windows, I recommend you to use AppVeyor.
For macOS and Linux build, there are plenty of services to choose from, for example CircleCI, Travis CI CodeShip.
angular-electron app?
Can I use this library in myYes, you can! But as this library uses Node Native Modules, you must change some config in package.json
and webpack.config.js
as described in this comment.
Do I have to use Babel in my app too?
No, you don't have to. This library works great in any Node.js 8+ environment (even in an Electron app).
Psst! Instead of using async/await (like in examples), you can use Promises.
reader .read(...) .then(data => ...) .catch(err => ...))
Babel is used under the hood to transpile features, that are not supported in Node.js 8 (for example ES6 modules – import/export, see .babelrc for list of used plugins). The transpiled code (in the dist folder) is then published into npm and when you install and require the library, the transpiled code is used, so you don't have to worry about anything.
Which Node.js versions are supported?
nfc-pcsc officially supports the following Node.js versions: 8.x, 9.x, 10.x, 11.x, 12.x, 13.x.
How do I require/import this library?
// in ES6 environment; // in ES2015 environmentconst NFC = ;
If you want to import uncompiled source and transpile it yourself (not recommended), you can do it as follows:
;
Can I read a NDEF formatted tag?
Yes, you can! You can read raw byte card data with reader.read
method, and then you can parse it with any NDEF parser, e.g. TapTrack/NdefJS.
Psst! There is also an example (ndef.js), but it is not finished yet. Feel free to contribute.
Can I use this library in my React Native app?
Short answer: NO
Explanation: Mobile support is virtually impossible because nfc-pcsc uses Node Native Modules to access system PC/SC API (actually under the hood, the pcsclite native binding is implemented in @pokusew/pcsclite). So the Node.js runtime and PC/SC API are required for nfc-pcsc to run. That makes it possible to use it on the most of OS (Windows, macOS, Linux) directly in Node.js or in Electron.js and NW.js desktop apps.
Frequent errors
TypeError: NFC is not a constructor
No worry, just check that you import/require the library like this (note the brackets around NFC):
// in ES6 environment; // in ES2015 environmentconst NFC = ;
Take a a look at How do I require/import this library? section for more info.
Note, that
const NFC = require('nfc-pcsc');
orimport NFC from 'nfc-pcsc'
(NFC without brackets) won't work, because there is no default export.
It was removed for non-standard behaviour of ES6 modules in ES5 env (see #12 and v0.6.0 release changelog)
CONNECT_MODE_DIRECT
Transaction failed error when using No worry, just needs a proper configuration, see explanation and instructions here.
MIFARE Classic: Authentication Error after Multiple Writes
No worry, you have probably modified a sector trailer instead of a data block, see explanation and instructions here.
Elsys.se sensors
Reading data from a type 4 tags inside aAccording to @martijnthe's findings, it seems to be necessary to change the CLASS of READ BINARY APDU command
from the default value of 0xFF
to 0x00
in order to make a successful read.
If you experience the same problems, you can try setting the fourth argument (readClass) of the
reader.read(blockNumber, length, blockSize, packetSize, readClass)
method to value 0x00
.
Relevant conversation: https://github.com/pokusew/nfc-pcsc/pull/55#issuecomment-450120232