Registry / auth-security / node-salesforce-jwt

node-salesforce-jwt

JSON →
library1.1.0jsnpmunverified

Minimal implementation of the Salesforce OAuth 2.0 JWT Bearer Token Flow for server-to-server authentication. Version 1.1.0 is the current stable release with no recent updates (low maintenance cadence). Enables impersonating users via a connected app's private key and consumer key. Key differentiator: lightweight (no heavy SDK) and designed to integrate with jsforce by returning access_token and instance_url. No breaking changes reported; simple callback-based API.

npm install node-salesforce-jwt
INSTALL
IMPORT
SIG · NODE-SALESFORCE-JW
N
node-salesforce-jwt
auth-securityjavascriptv1.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

getToken
const nsj = require('node-salesforce-jwt'); nsj.getToken(options, callback);
import { getToken } from 'node-salesforce-jwt';
Package uses CommonJS; no default or named ESM export. Use require() in Node environments.
module.exports
const nsj = require('node-salesforce-jwt');
import nsj from 'node-salesforce-jwt';
CJS only. The entire module.exports is an object with getToken.

Demonstrates how to obtain a JWT bearer token using a private key and connected app credentials, then use it with jsforce to query Salesforce.

const nsj = require('node-salesforce-jwt'); const jsforce = require('jsforce'); const fs = require('fs'); const options = { isTest: true, clientId: process.env.SF_CONSUMER_KEY ?? '', privateKey: fs.readFileSync('./private.key', 'utf8'), user: 'admin@example.com' }; nsj.getToken(options, (err, response) => { if (err) { console.error(err); return; } const conn = new jsforce.Connection(); conn.initialize({ instanceUrl: response.instance_url, accessToken: response.access_token }); conn.query('SELECT Id, Name FROM Account LIMIT 1', (err, result) => { if (err) { console.error(err); return; } console.log('First record:', result.records[0]); }); });
Debug
Known issues
breakingThe isTest option determines the token endpoint URL: test.salesforce.com vs login.salesforce.com. Incorrect value leads to 404 errors.
fix
Set isTest: true for sandbox/test instances, false for production. This is not automatically detected.
affects: >=1.0.0
gotchaThe privateKey must be a string (e.g., read via readFileSync with 'utf8'). Passing a Buffer or file path directly will cause errors.
fix
Ensure privateKey is a string: fs.readFileSync('./key.pem', 'utf8')
affects: >=1.0.0
gotchaThe clientId must be the Consumer Key from the connected app, not the Consumer Secret. Mixing them results in authentication failure.
fix
Use the Consumer Key (long string ending with .something) from your Salesforce Connected App definition.
affects: >=1.0.0
Errors
Common errors & fixes
Error: unable to get local issuer certificate
Missing or outdated root CA certificates in Node.js environment.
fix
Set environment variable NODE_TLS_REJECT_UNAUTHORIZED=0 (not recommended) or update Node.js CA certificate bundle.
{"error":"invalid_client_id","error_description":"client identifier invalid"}
The clientId provided does not match a valid Salesforce Connected App Consumer Key.
fix
Verify the clientId is the Consumer Key from the connected app (not the Consumer Secret) and that the app is properly configured with the correct OAuth scopes.
Error: PEM_read_bio:no start line
The privateKey string does not contain a valid PEM-encoded key (e.g., missing header/footer).
fix
Ensure the private key file is correctly formatted with BEGIN and END RSA PRIVATE KEY lines. Read file with utf8 encoding.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
jsonwebtokenrequiredUsed to sign the JWT assertion with the private key.
Agent activity
13 hits · last 30 days
node
10
OpenAI (training)
1
Resources
node-salesforce-jwt — npm install node-salesforce-jwt · libregistry