admin管理员组文章数量:1279008
isDeepStrictEqual() method to do object parison but am blocked by error:
util.isDeepStrictEqual() is not a function
According to official documentation: this method was introduced in v9.0.0 and I am using Node v12: .html#util_util_isdeepstrictequal_val1_val2
I confirmed this method is available on mand line:
D:\>node Wele 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
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); }
isDeepStrictEqual() method to do object parison but am blocked by error:
util.isDeepStrictEqual() is not a function
According to official documentation: this method was introduced in v9.0.0 and I am using Node v12: https://nodejs/api/util.html#util_util_isdeepstrictequal_val1_val2
I confirmed this method is available on mand line:
D:\>node Wele 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
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); }
- 1 reactjs runs on the web browser, nodejs runs on the console, they are two pletely different environment – arslan2012 Commented Sep 1, 2020 at 3:32
2 Answers
Reset to default 5I 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 mand line.
If you want to use this with "util" name:
import * as util from 'util';
本文标签: javascriptHow to import util module in NodejsStack Overflow
版权声明:本文标题:javascript - How to import util module in Node.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741234558a2362749.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论