如何在 Node.js 中以编程方式使用 JSHINT?

How to use JSHINT programmatically within Node.js?

我正在使用 npm jshint package in command line tool, but now I want to use it programmatically. Documentation 参考了这个例子,但它没有说明如何包含或 require 那个 JSHINT 函数

var source = [
  'function goo() {}',
  'foo = 3;'
];
var options = {
  undef: true
};
var predef = {
  foo: false
};

JSHINT(source, options, predef);

console.log(JSHINT.data());

文档提到

JSHint exposes a JavaScript API for programmatic access in environments like web browsers and Node.js.

但暴露如何?

我在本地安装了 npm 包(我想在本地安装)

npm install jshint --save

我照原样做了,我得到了

JSHINT(code, JShintOpt);
^
ReferenceError: JSHINT is not defined

我也试过了

const JSHINT = require('jshint');

但是也没用

使用以下内容:

const { JSHINT } = require('jshint');

相当于:

const JSHINT = require('jshint').JSHINT;