petkaantonov/bluebird · error · Error
cannot enable long stack traces after promises have been cre
Error message
cannot enable long stack traces after promises have been created
See http://goo.gl/MqrFmX
What it means
Promise.longStackTraces() can only be enabled before any promise has been created/queued, because long-stack-trace instrumentation wraps core promise methods. If async.haveItemsQueued() reports pending work and long stack traces are not yet on, Bluebird throws this Error to avoid inconsistent instrumentation.
Source
Thrown at src/debuggability.js:144
Promise.prototype._warn = function(message, shouldUseOwnTrace, promise) {
return warn(message, shouldUseOwnTrace, promise || this);
};
Promise.onPossiblyUnhandledRejection = function (fn) {
var context = Promise._getContext();
possiblyUnhandledRejection = util.contextBind(context, fn);
};
Promise.onUnhandledRejectionHandled = function (fn) {
var context = Promise._getContext();
unhandledRejectionHandled = util.contextBind(context, fn);
};
var disableLongStackTraces = function() {};
Promise.longStackTraces = function () {
if (async.haveItemsQueued() && !config.longStackTraces) {
throw new Error(LONG_STACK_TRACES_ERROR);
}
if (!config.longStackTraces && longStackTracesIsSupported()) {
var Promise_captureStackTrace = Promise.prototype._captureStackTrace;
var Promise_attachExtraTrace = Promise.prototype._attachExtraTrace;
var Promise_dereferenceTrace = Promise.prototype._dereferenceTrace;
config.longStackTraces = true;
disableLongStackTraces = function() {
if (async.haveItemsQueued() && !config.longStackTraces) {
throw new Error(LONG_STACK_TRACES_ERROR);
}
Promise.prototype._captureStackTrace = Promise_captureStackTrace;
Promise.prototype._attachExtraTrace = Promise_attachExtraTrace;
Promise.prototype._dereferenceTrace = Promise_dereferenceTrace;
Context.deactivateLongStackTraces();
config.longStackTraces = false;
};
Promise.prototype._captureStackTrace = longStackTracesCaptureStackTrace;
Promise.prototype._attachExtraTrace = longStackTracesAttachExtraTrace;View on GitHub (pinned to c220cfe480)
Solutions
- Call Promise.longStackTraces() as the very first thing after requiring bluebird, before any promise is created
- Use the BLUEBIRD_DEBUG=1 / NODE_ENV=development environment variable which auto-enables long stack traces at load time
- Pass longStackTraces via Promise.config({longStackTraces: true}) early at startup
- Move the configuration out of lazy/deferred code paths to module initialization
- Audit imports so no module creates a Bluebird promise before configuration
Example fix
// before
const app = require('./app'); // creates promises at import
Promise.longStackTraces();
// after
const Promise = require('bluebird');
Promise.longStackTraces(); // before anything else
const app = require('./app'); Defensive patterns
Strategy: validation
Validate before calling
const Promise = require('bluebird');
if (!Promise.longStackTraces) return; // very old version
Promise.longStackTraces(); // must run before any promise creation Type guard
function canEnableLongStackTraces(Promise) {
try { Promise.longStackTraces(); return true; }
catch (e) { return false; }
} Try / catch
try {
Promise.longStackTraces();
} catch (e) {
if (e.message.includes('long stack traces')) {
console.warn('longStackTraces enabled too late; set it at startup or use BLUEBIRD_DEBUG=1');
} else throw e;
} Prevention
- Enable longStackTraces as the first statement after requiring bluebird
- Use NODE_ENV=development or BLUEBIRD_DEBUG env vars instead of runtime calls
- Never configure bluebird inside request handlers or lazy init
- Ensure no module creates promises at import time before configuration
When it happens
Trigger: Calling Promise.longStackTraces() (or setting config) after at least one promise has already been instantiated — e.g. enabling it inside application code that runs after module load, or in the middle of a running request handler instead of at process startup.
Common situations: Enabling longStackTraces in a middleware or lazy init path; requiring bluebird and creating promises before a config module runs; library code that creates a promise at import time, beating your configuration call.
Related errors
AI-assisted analysis of petkaantonov/bluebird@c220cfe480 (2026-09-02).
Data as JSON: /api/errors/a34fe6b345e38d2f.
Report an issue: GitHub.