Node.js assert模块strict模式调用deepEqual报错,请求协助分析
Cannot read property 'deepEqual' of undefined Error with assert.strict in Node.js Hey there! Let's break down why you're hitting this error and how to fix it.
Root Cause of the Error
The TypeError: Cannot read property 'deepEqual' of undefined tells us that require('assert').strict is returning undefined in your environment. This happens because the .strict property for the assert module was only introduced in Node.js v10.0.0. If you're running an older version of Node.js, this property doesn't exist—hence the error.
How to Resolve This
You have two solid options depending on your Node.js version:
Option 1: Use a Supported Node.js Version (v10.0.0+)
If you can upgrade your Node.js to version 10.0.0 or later, your original code structure will work—just make sure to handle the assertion properly (since your test case will fail intentionally due to type mismatch):
const assert = require('assert').strict; try { assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]); } catch (err) { console.log(err.message); // Will correctly flag the type difference between 3 and '3' }
Note: In strict mode, assert.deepEqual() enforces strict type checking, exactly like assert.deepStrictEqual() does in non-strict mode.
Option 2: Compatibility with Older Node.js Versions
If upgrading isn't an option, skip the .strict property and directly use assert.deepStrictEqual()—it achieves the same strict comparison behavior you're targeting:
const assert = require('assert'); try { assert.deepStrictEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]); } catch (err) { console.log(err.message); // Same strict type mismatch error as above }
Quick Version Check
To confirm your Node.js version, run this command in your terminal:
node -v
If it returns a version lower than v10.0.0, that's definitely the source of your issue.
内容的提问来源于stack exchange,提问作者Haseena

