bilibili/flv.js · error · TypeError

Cannot convert undefined or null to object

Error message

Cannot convert undefined or null to object

What it means

This is a polyfilled ES6 Object.assign in src/utils/polyfill.js that mirrors the native spec: per spec, Object.assign throws TypeError when the target is undefined or null. The polyfill re-implements the same behavior for browsers lacking native Object.assign, so calling it with a nullish target fails the same way the builtin would.

Source

Thrown at src/utils/polyfill.js:31

 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

class Polyfill {

    static install() {
        // ES6 Object.setPrototypeOf
        Object.setPrototypeOf = Object.setPrototypeOf || function (obj, proto) {
            obj.__proto__ = proto;
            return obj;
        };

        // ES6 Object.assign
        Object.assign = Object.assign || function (target) {
            if (target === undefined || target === null) {
                throw new TypeError('Cannot convert undefined or null to object');
            }

            let output = Object(target);
            for (let i = 1; i < arguments.length; i++) {
                let source = arguments[i];
                if (source !== undefined && source !== null) {
                    for (let key in source) {
                        if (source.hasOwnProperty(key)) {
                            output[key] = source[key];
                        }
                    }
                }
            }
            return output;
        };

        // ES6 Promise (missing support in IE11)
        if (typeof self.Promise !== 'function') {

View on GitHub (pinned to 42343088f2)

Solutions

  1. Ensure the target argument is an object before calling Object.assign
  2. Default the target: Object.assign(target || {}, source)
  3. Debug why the variable holding the target is undefined/null
  4. Support modern browsers where native Object.assign is available and fails with the same TypeError, making the root cause the same

Example fix

// before
const config = Object.assign(userOptions, defaults); // userOptions is undefined

// after
const config = Object.assign({}, defaults, userOptions || {});
Defensive patterns

Strategy: type-guard

Validate before calling

function isObjectAssignSafe(target) {
  return target !== undefined && target !== null;
}

if (!isObjectAssignSafe(target)) {
  target = {};
}
const merged = Object.assign(target, defaults);

Type guard

function isNonNullObject(v) {
  return v !== null && typeof v === 'object';
}

if (!isNonNullObject(userOptions)) userOptions = {};

Try / catch

let merged;
try {
  merged = Object.assign(userOptions, defaults);
} catch (e) {
  if (e instanceof TypeError && /undefined or null/.test(e.message)) {
    merged = Object.assign({}, defaults);
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Calling Object.assign(undefined, ...) or Object.assign(null, ...) (or a variable intended to be the target object that is nullish) on an old browser/runtime where the polyfill replaces the native method.

Common situations: Older browsers (IE11, legacy mobile WebViews) without native Object.assign; a config/defaults object variable that failed to initialize and is undefined/null when merged with defaults.

Related errors


AI-assisted analysis of bilibili/flv.js@42343088f2 (2026-09-01). Data as JSON: /api/errors/072b4b1947d86420. Report an issue: GitHub.