AnswerBun.com

How to import util module in Node.js?

Stack Overflow Asked by LLD on October 23, 2020

isDeepStrictEqual() method to do object comparison but am blocked by error:

util.isDeepStrictEqual() is not a function
  1. According to official documentation: this method was introduced in v9.0.0 and I am using Node v12: https://nodejs.org/api/util.html#util_util_isdeepstrictequal_val1_val2

  2. I confirmed this method is available on command line:

    D:>node
    Welcome to Node.js v12.18.2.
    Type ".help" for more information.
    > require("util").isDeepStrictEqual({'name': 'john'}, {'sex': 'male'})
    false
    > require("util").isDeepStrictEqual({'sex': 'john'}, {'sex': 'male'})
    false
    > require("util").isDeepStrictEqual({'sex': 'male'}, {'sex': 'male'})
    true
    
  3. Here is my code:

    class App extends Component {
    
     constructor(props) {
         super(props);
         var util = require('util');
         var obj1 = {name: 'john'};
         var obj2 = {sex: 'male'};
         var result = util.isDeepStrictEqual(obj1, obj2);
     }
    

One Answer

I dont know if you have done this so first do this util-npm

npm install util

And adding on to the @3limin4t0r answer I don't think util should be declared in the constructor.

have it as a global constant outside the class

for example

const util = require('util');

class Foo {
  constructor() {
    this.a = 42;
  }

  bar(callback) {
    callback(null, this.a);
  }
}

const foo = new Foo();

const naiveBar = util.promisify(foo.bar);
// TypeError: Cannot read property 'a' of undefined
// naiveBar().then(a => console.log(a));

naiveBar.call(foo).then((a) => console.log(a)); // '42'

const bindBar = naiveBar.bind(foo);
bindBar().then((a) => console.log(a)); // '42'

which was the example for promisify right below Util doc(isDeepStrictEqual)

So either use it as a global constant or use it directly as you have used in the command line.

Answered by Glitch_Znab on October 23, 2020

Add your own answers!

Related Questions

Why does the color editor in Blend stop working?

1  Asked on December 1, 2020 by vladimir-yudin

     

eCharts marker line is cutting off text

2  Asked on December 1, 2020 by sanya

     

how do i connect my mongodb to my laptop?

2  Asked on December 1, 2020 by omojuwon-soneye

         

How to write text over an image?

2  Asked on December 1, 2020 by hichem-neggaz

     

Flutter text background with figure

1  Asked on December 1, 2020 by talisman7525

 

Minimum count to make equal Array

4  Asked on December 1, 2020

   

Trying to convert a csv to HDF5 and read it using vaex

1  Asked on December 1, 2020 by droning-hangman

       

Run Tree Structure in Python

1  Asked on November 30, 2020 by tianhe-xie

 

How iterate over a list and delete values along the way

1  Asked on November 30, 2020 by jaden-listrani-ayers

 

import packages from project directory

2  Asked on November 30, 2020 by will-waltrip

     

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP