lovell/sharp · error · Error

Expected noise to be an object

Error message

Expected noise to be an object

What it means

Thrown when inputOptions.create.noise is defined but is not an object. The noise generator requires a descriptor with at least a type field (and optionally mean/sigma), so passing a primitive (number, string, array) is rejected before any field is read. The guard is `!is.object(inputOptions.create.noise)`.

Source

Thrown at lib/input.mjs:353

      ) {
        inputDescriptor.createWidth = inputOptions.create.width;
        inputDescriptor.createHeight = inputOptions.create.height;
        inputDescriptor.createChannels = inputOptions.create.channels;
        inputDescriptor.createPageHeight = 0;
        if (is.defined(inputOptions.create.pageHeight)) {
          if (is.integer(inputOptions.create.pageHeight) && inputOptions.create.pageHeight > 0 && inputOptions.create.pageHeight <= inputOptions.create.height) {
            if (inputOptions.create.height % inputOptions.create.pageHeight !== 0) {
              throw new Error(`Expected create.height ${inputOptions.create.height} to be a multiple of create.pageHeight ${inputOptions.create.pageHeight}`);
            }
            inputDescriptor.createPageHeight = inputOptions.create.pageHeight;
          } else {
            throw is.invalidParameterError('create.pageHeight', 'positive integer', inputOptions.create.pageHeight);
          }
        }
        // Noise
        if (is.defined(inputOptions.create.noise)) {
          if (!is.object(inputOptions.create.noise)) {
            throw new Error('Expected noise to be an object');
          }
          if (inputOptions.create.noise.type !== 'gaussian') {
            throw new Error('Only gaussian noise is supported at the moment');
          }
          inputDescriptor.createNoiseType = inputOptions.create.noise.type;
          if (!is.inRange(inputOptions.create.channels, 1, 4)) {
            throw is.invalidParameterError('create.channels', 'number between 1 and 4', inputOptions.create.channels);
          }
          inputDescriptor.createNoiseMean = 128;
          if (is.defined(inputOptions.create.noise.mean)) {
            if (is.number(inputOptions.create.noise.mean) && is.inRange(inputOptions.create.noise.mean, 0, 10000)) {
              inputDescriptor.createNoiseMean = inputOptions.create.noise.mean;
            } else {
              throw is.invalidParameterError('create.noise.mean', 'number between 0 and 10000', inputOptions.create.noise.mean);
            }
          }
          inputDescriptor.createNoiseSigma = 30;
          if (is.defined(inputOptions.create.noise.sigma)) {

View on GitHub (pinned to 56676c6918)

Solutions

  1. Wrap the noise spec in an object with a type field: noise: { type: 'gaussian', mean: 128, sigma: 30 }.
  2. Ensure mean and sigma (if provided) are numbers in range 0-10000.
  3. Confirm channels is 1-4 (a separate check inside the noise branch will enforce 1-4).

Example fix

// before
sharp({ create: { width: 100, height: 100, channels: 3, noise: 'gaussian' } })

// after
sharp({ create: { width: 100, height: 100, channels: 3, noise: { type: 'gaussian', mean: 128, sigma: 30 } } })
Defensive patterns

Strategy: type-guard

Validate before calling

function noiseCreate(opts) {
  if (typeof opts.noise !== 'object' || opts.noise === null) {
    throw new Error('create.noise must be an object e.g. { type: "gaussian", mean, sigma }');
  }
  return sharp({ create: opts });
}

Type guard

function isNoiseObject(v) {
  return v !== null && typeof v === 'object' && !Array.isArray(v);
}

Prevention

When it happens

Trigger: sharp({ create: { width, height, channels, noise: 'gaussian' } }) — a string instead of an object. noise: 5 or noise: ['gaussian']. Mistakenly passing the type directly instead of wrapping it.

Common situations: Misreading the API and assuming noise takes a string type. Copying an example that used the old/imagined shorthand. Auto-generating options from a config where noise ended up as a scalar.

Related errors


AI-assisted analysis of lovell/sharp@56676c6918 (2026-08-13). Data as JSON: /api/errors/96eb70b8aecee8c1. Report an issue: GitHub.