Bindings over pcsclite to access Smart Cards. It works in Linux, macOS and Windows.
📌 Looking for library to work easy with NFC tags?
Then take a look at nfc-pcsc which offers an easy to use high level API for detecting / reading and writing NFC tags and cards.
- Installation
- Example
- Behavior on different OS
- API
- FAQ
- Frequent errors
- License
Requirements: at least Node.js 8 or newer (see this FAQ for more info)
-
Pre-built binaries
This library now provides pre-built binaries for Windows, macOS, and Linux, so you don't need to have a C/C++ compiler installed in most cases. The pre-built binaries are automatically downloaded during installation.
-
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-dev pcscd
-
Install node-pcsclite using npm or yarn:
npm install @printags/node-pcsclite --save
or using Yarn:
yarn add @printags/node-pcsclite
-
Building from source (if needed)
If a pre-built binary for your platform is not available, the library will automatically build from source using node-gyp. In this case, you'll need to have a C/C++ compiler and the PC/SC development libraries installed.
Please refer to the node-gyp > Installation for the list of required tools depending on your OS.
👉 If you'd prefer an easy to use high level API for detecting / reading and writing NFC tags and cards, take a look at nfc-pcsc.
const pcsclite = require('@printags/node-pcsclite');
const pcsc = pcsclite();
pcsc.on('reader', (reader) => {
console.log('New reader detected', reader.name);
reader.on('error', err => {
console.log('Error(', reader.name, '):', err.message);
});
reader.on('status', (status) => {
console.log('Status(', reader.name, '):', status);
// check what has changed
const changes = reader.state ^ status.state;
if (!changes) {
return;
}
if ((changes & reader.SCARD_STATE_EMPTY) && (status.state & reader.SCARD_STATE_EMPTY)) {
console.log("card removed");
reader.disconnect(reader.SCARD_LEAVE_CARD, err => {
if (err) {
console.log(err);
return;
}
console.log('Disconnected');
});
}
else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {
console.log("card inserted");
reader.connect({ share_mode: reader.SCARD_SHARE_SHARED }, (err, protocol) => {
if (err) {
console.log(err);
return;
}
console.log('Protocol(', reader.name, '):', protocol);
reader.transmit(Buffer.from([0x00, 0xB0, 0x00, 0x00, 0x20]), 40, protocol, (err, data) => {
if (err) {
console.log(err);
return;
}
console.log('Data received', data);
reader.close();
pcsc.close();
});
});
}
});
reader.on('end', () => {
console.log('Reader', reader.name, 'removed');
});
});
pcsc.on('error', err => {
console.log('PCSC error', err.message);
});
TODO document
The PCSCLite object is an EventEmitter that notifies the existence of Card Readers.
- err
Error Object
. The error.
- reader
CardReader
. A CardReader object associated to the card reader detected
Emitted whenever a new card reader is detected.
It frees the resources associated with this PCSCLite instance. At a low level it
calls SCardCancel
so it stops watching for new readers.
An object containing all detected readers by name. Updated as readers are attached and removed.
The CardReader object is an EventEmitter that allows to manipulate a card reader.
- err
Error Object
. The error.
Emitted when the card reader has been removed.
- status
Object
.- state The current status of the card reader as returned by
SCardGetStatusChange
- atr ATR of the card inserted (if any)
- state The current status of the card reader as returned by
Emitted whenever the status of the reader changes.
- options
Object
Optional- share_mode
Number
Shared mode. Defaults toSCARD_SHARE_EXCLUSIVE
- protocol
Number
Preferred protocol. Defaults toSCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1
- share_mode
- callback
Function
called when connection operation ends- error
Error
- protocol
Number
Established protocol to this connection.
- error
Wrapper around SCardConnect
.
Establishes a connection to the reader.
- disposition
Number
. Reader function to execute. Defaults toSCARD_UNPOWER_CARD
- callback
Function
called when disconnection operation ends- error
Error
- error
Wrapper around SCardDisconnect
.
Terminates a connection to the reader.
- input
Buffer
input data to be transmitted - res_len
Number
. Max. expected length of the response - protocol
Number
. Protocol to be used in the transmission - callback
Function
called when transmit operation ends- error
Error
- output
Buffer
- error
Wrapper around SCardTransmit
.
Sends an APDU to the smart card contained in the reader connected to.
- input
Buffer
input data to be transmitted - control_code
Number
. Control code for the operation - res_len
Number
. Max. expected length of the response - callback
Function
called when control operation ends- error
Error
- output
Buffer
- error
Wrapper around SCardControl
.
Sends a command directly to the IFD Handler (reader driver) to be processed by the reader.
It frees the resources associated with this CardReader instance.
At a low level it calls SCardCancel
so it stops watching for the reader status changes.
Can I use this library in my Electron app?
Yes, you can! It works well.
But please 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.
Yes! This library provides prebuilt binaries for common platforms (Windows, macOS, Linux) and for both Node.js and Electron. The prebuilt binaries are automatically downloaded during installation, so you don't need to have a C/C++ compiler installed.
If a prebuilt binary for your platform is not available, the library will automatically build from source using node-gyp. In this case, you'll need to have a C/C++ compiler and the PC/SC development libraries installed (see the Installation section).
This makes it much easier to use this library in your Electron or NW.js applications without having to worry about rebuilding the native module.
In case there is another driver blocking the usb bus, you won't be able to access the NFC reader until you disable it. First, plug in your reader and check, which driver is being used:
$ lsusb -t
/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/12p, 480M
|__ Port 3: Dev 6, If 0, Class=Chip/SmartCard, Driver=pn533, 12M
...
In my case, there is a pn533
driver loaded by default. Now find the dependency tree of that driver:
$ lsmod | grep pn533
Module Size Used by
pn533_usb 20480 0
pn533 45056 1 pn533_usb
nfc 131072 1 pn533
We see, that there are drivers nfc
, pn533
and pn533_usb
we need to disable. Create file in /etc/modprobe.d/
with the following content:
$ cat /etc/modprobe.d/nfc-blacklist.conf
blacklist pn533_usb
blacklist pn533
blacklist nfc
After reboot, there will be no driver blocking the usb bus anymore, so we can finally enable and start the pscs deamon:
# systemctl enable pcscd
# systemctl start pcscd
@printags/node-pcsclite officially supports the following Node.js versions: 10.x, 12.x, 14.x, 16.x, 18.x, 20.x.
Short answer: NO
Explanation: Mobile support is virtually impossible because @printags/node-pcsclite uses Node Native Modules to access system PC/SC API. So the Node.js runtime and PC/SC API are required for @printags/node-pcsclite 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. On the other hand, these requirements are not normally met on mobile devices. On top of that, React Native does not contain any Node.js runtime.
@printags/node-pcsclite uses pre-built binaries via node-gyp-build
which should avoid this error. However, if you still encounter this issue, it could be due to one of these reasons:
-
No pre-built binary is available for your platform:
- In this case, the library tries to build from source using node-gyp
- Make sure you have all the requirements for building from source as described in the Installation section
-
There's an issue with the pre-built binary installation:
- Try clearing your npm cache:
npm cache clean --force
- Reinstall the package:
npm uninstall @printags/node-pcsclite && npm install @printags/node-pcsclite
- Try clearing your npm cache:
If the problem persists, open a new issue with details about your platform, OS, Node.js version, and npm/yarn version.