Slack Node.js SDK
slack-node is a Node.js library for interacting with the Slack API, supporting both incoming webhooks and the comprehensive Slack Web API. Currently at version 0.3.2, it maintains a policy of zero runtime dependencies, making it a lightweight choice for integrating Slack functionalities into Node.js applications. The library is designed for flexibility, offering both traditional callback-based asynchronous operations and modern Promise-based and async/await syntax, catering to various coding styles. While its version number suggests a pre-1.0 stability, the project claims continuous updates and full feature support. Its core differentiation lies in its minimalist approach to dependencies and its dual API consumption models.
Common errors
-
ReferenceError: Slack is not defined
cause Attempting to use `import Slack from 'slack-node';` in a Node.js environment that does not transpile ESM to CJS, or forgetting to `require` the library.fixChange `import Slack from 'slack-node';` to `const Slack = require('slack-node');`. -
Error: Webhook URL is not set.
cause The `setWebhook()` method was not called on the `Slack` instance, or it was called with an invalid/empty URL, before attempting to send a message via `webhook()`.fixInitialize the `Slack` instance and call `slack.setWebhook('YOUR_SLACK_WEBHOOK_URL')` with a valid URL before sending messages. -
Error: Bad token
cause The Slack API token provided to the `Slack` constructor or during an API call is invalid, revoked, or lacks the necessary permissions.fixVerify that the API token is correct, active, and has the required scopes for the API methods you are attempting to call (e.g., `users:read` for `users.list`, `chat:write` for `chat.postMessage`).
Warnings
- gotcha This library is designed for CommonJS (`require()`) environments and does not natively support ES Modules (`import`). Attempting to use `import` statements will result in runtime errors unless a transpilation step (e.g., Babel or TypeScript) is configured to convert ESM to CJS.
- gotcha The package is currently at version 0.3.2, indicating it is pre-1.0. While the README suggests it's 'continuously updated,' pre-1.0 versions may introduce breaking changes between minor or patch releases without strictly adhering to semantic versioning standards.
- gotcha When using Slack webhooks, the `setWebhook()` method must be called with a valid webhook URL before calling `slack.webhook()`. Failure to do so will result in an error indicating the webhook URL is not set.
Install
-
npm install slack-node -
yarn add slack-node -
pnpm add slack-node
Imports
- Slack
import Slack from 'slack-node';
const Slack = require('slack-node'); - Slack instance (webhook)
const slack = new Slack(); slack.setWebhook('__your_webhook_url__'); - Slack instance (API)
const slack = new Slack('__your_api_token__');
Quickstart
const Slack = require('slack-node');
async function main() {
// Webhook usage
const webhookUrl = process.env.SLACK_WEBHOOK_URL ?? '__your_webhook_url__';
const slackWebhook = new Slack();
slackWebhook.setWebhook(webhookUrl);
if (webhookUrl === '__your_webhook_url__') {
console.warn("Please provide a valid SLACK_WEBHOOK_URL environment variable or replace the placeholder.");
} else {
try {
const webhookResponse = await slackWebhook.webhook({
channel: "#general",
username: "webhookbot",
text: "Hello from async/await via webhook!",
icon_emoji: ":ghost:"
});
console.log("Webhook message sent:", webhookResponse.data);
} catch (err) {
console.error("Webhook error:", err);
}
}
// API usage
const apiToken = process.env.SLACK_API_TOKEN ?? '__your_api_token__';
if (apiToken === '__your_api_token__') {
console.warn("Please provide a valid SLACK_API_TOKEN environment variable or replace the placeholder.");
} else {
const slackApi = new Slack(apiToken);
try {
const usersList = await slackApi.api("users.list");
console.log("Users list:", usersList.members.map(u => u.real_name));
const chatPostMessage = await slackApi.api("chat.postMessage", {
text: "hello from async/await via API",
channel: "#general"
});
console.log("API message sent (timestamp):", chatPostMessage.ts);
} catch (err) {
console.error("API error:", err);
}
}
}
main();