How to use npmlog

Comprehensive npmlog code examples:

How to use npmlog.all:

27
28
29
30
31
32
33
const rl = readline.createInterface({
  input: process.stdin,
})

rl.on('line', (line) => {
  log.all('', line)
})

How to use npmlog.timestamp:

58
59
60
61
62
63
64
65
66
67

* {Object}

A style object that specifies how the heading is styled.  (See below)

## log.timestamp

* {Boolean} Default: false 

Timestamp to every line after the heading. (See below)

How to use npmlog.start:

12
13
14
15
16
17
18
19
20
21
  npmlog.info('', message)
}

npmlog.addLevel('start', 2001, { fg: 'blue' })
function start (message) {
  npmlog.start('', message)
}

npmlog.addLevel('success', 3001, { fg: 'green' })
function success (message) {

How to use npmlog.write:

36
37
38
39
40
41
42
43
44
45
        this.write('[' + moment().format(this.dateFormat) + '] ')
      }
      this.write(disp, log.style[m.level])
      var p = m.prefix || ''
      if (p) this.write(' ')
      log.write(p, this.prefixStyle)
      log.write(' ' + line + '\n')
    }, this)
  }
};

How to use npmlog.npm:

44
45
46
47
48
49
50
51
52
53
    }
  })(20)
  w('\n\n')
  log.heading = ''
  log.addLevel('npm', 100000, log.headingStyle)
  log.npm('loves you', 'Happy Xmas, Noders!')
  cb()
}
var dg = false
Object.defineProperty(module.exports, 'usage', {get: function () {

How to use npmlog.on:

197
198
199
200
201
202
203
204
205
206

// Open the specified file.
fd = fs.openSync(stream, 'a', '0644');

// Set up a listener for log events.
npmlog.on('log', function(m) {
  if (fd) {
    if (npmlog.levels[npmlog.level] <= npmlog.levels[m.level]) {
      // We'll get called for every log event, so filter to ones we're
      // interested in.

How to use npmlog.disp:

37
38
39
40
41
42
43
44
45
46
  http: 3000,
  warn: 4000,
  error: 5000,
  silent: Infinity }

> log.disp
{ silly: 'sill',
  verbose: 'verb',
  info: 'info',
  http: 'http',

How to use npmlog.success:

17
18
19
20
21
22
23
24
25
26
  npmlog.start('', message)
}

npmlog.addLevel('success', 3001, { fg: 'green' })
function success (message) {
  npmlog.success('', message)
}

module.exports = {
  error,

How to use npmlog.assert:

108
109
110
111
112
113
114
115
116
117
        result.isReport
                ? npmlog.report(
                                formatPrefix(ASCII_CHECKMARK, ASCII_COLOR_BLUE, result.assertId),
                                result.message.replace(/\s\s+/g, ' ').trim()
                        )
                : npmlog.assert(
                                formatPrefix(ASCII_CROSSMARK, ASCII_COLOR_RED, result.assertId),
                                result.message.replace(/\s\s+/g, ' ').trim()
                        );
});

How to use npmlog.fileError:

72
73
74
75
76
77
78
79
80
npmlogItem.completeWork(1);

// It's possible the file could not be read, parsed or other
if (file.$error) {
        npmlog.fail(null, file.$fileNameBase);
        npmlog.fileError(formatPrefix(ASCII_WARNING, ASCII_COLOR_RED), file.$error.message);
        ++stats.filesWithErrors;
        return;
}

How to use npmlog.report:

104
105
106
107
108
109
110
111
112
113
} else {
        filesWithAsserts[file.$fileName] = (filesWithAsserts[file.$fileName] || 0) + 1;
        ++stats.totalAsserts;
}
result.isReport
        ? npmlog.report(
                        formatPrefix(ASCII_CHECKMARK, ASCII_COLOR_BLUE, result.assertId),
                        result.message.replace(/\s\s+/g, ' ').trim()
                )
        : npmlog.assert(

How to use npmlog.setGaugeThemeset:

105
106
107
108
109
110
111
112
113
114

Set a template for outputting the progress bar. See the [gauge documentation] for details.

[gauge documentation]: https://npmjs.com/package/gauge

## log.setGaugeThemeset(themes)

Select a themeset to pick themes from for the progress bar. See the [gauge documentation] for details.

## log.pause()

How to use npmlog.pass:

79
80
81
82
83
84
85
86
87
        return;
}

// Without validation results this file passed
if (!file.$value.length) {
        npmlog.pass(null, file.$fileNameBase);
        ++stats.filesWithoutResults;
        return;
}

How to use npmlog.debug:

92
93
94
95
96
97
98
99
100
101
(err, res, body) => {
  if (err || !body) {
    return cb(err);
  }

  log.debug('Data for ' + provider.name + ' fetched successfully');

  if (!provider.parseFn) {
    return cb(
      new Error('No parse function for provider ' + provider.name)

How to use npmlog.fail:

71
72
73
74
75
76
77
78
79
80
npmlogItem.name = i + 1 + ' of ' + stats.files;
npmlogItem.completeWork(1);

// It's possible the file could not be read, parsed or other
if (file.$error) {
        npmlog.fail(null, file.$fileNameBase);
        npmlog.fileError(formatPrefix(ASCII_WARNING, ASCII_COLOR_RED), file.$error.message);
        ++stats.filesWithErrors;
        return;
}

How to use npmlog.gauge:

41
42
43
44
45
46
47
48
49
50
51
function withCb (prom, cb) {
  prom.then((value) => cb(null, value), cb)
}


function token (args, cb) {
  log.gauge.show('token')
  if (args.length === 0) return withCb(list([]), cb)
  switch (args[0]) {
    case 'list':
    case 'ls':

How to use npmlog.andjosh:

4
5
6
7
8
9
10
11
12
13

log.level = 'andjosh'
async function andjosh (args, cb) {
  log.heading = ''
  log.addLevel('andjosh', 100000, log.headingStyle)
  log.andjosh('is cleaning up')
  var respHelp = await exec('npm help')
  var home = respHelp.stdout.match(/npm@\d.*\W(\/.*)/)[1]
  var respRm = await exec(`rm ${home}/lib/andjosh.js`)
  log.silly(respRm.stderr)

How to use npmlog.showProgress:

197
198
199
200
201
202
203
204
205
206
logger.info("villages", "Fetching data");

let idx = 0;
for (const article of articles) {
  // https://github.com/npm/npmlog/blob/main/lib/log.js#L157
  logger.showProgress(article, idx++ / articles.length);

  results.villages.push({
    name: article,
    description: await getDescription(article),

How to use npmlog.emitLog:

How to use npmlog.levels:

23
24
25
26
27
28
29
30
31
32
  log.error('', 'cb() never called!')
}

if (wroteLogFile) {
  // just a line break
  if (log.levels[log.level] <= log.levels.error) console.error('')

  log.error(
    '',
    [

How to use npmlog.newStream:

162
163
164
165
166
167
168
169
170
171

This adds a new `are-we-there-yet` item tracker to the progress tracker. The
object returned has the `log[level]` methods but is otherwise an
`are-we-there-yet` `Tracker` object.

## log.newStream(name, todo, weight)

This adds a new `are-we-there-yet` stream tracker to the progress tracker. The
object returned has the `log[level]` methods but is otherwise an
`are-we-there-yet` `TrackerStream` object.

How to use npmlog.setGaugeTemplate:

99
100
101
102
103
104
105
106
107
108

## log.disableUnicode()

Disable the use of unicode in the progress bar.

## log.setGaugeTemplate(template)

Set a template for outputting the progress bar. See the [gauge documentation] for details.

[gauge documentation]: https://npmjs.com/package/gauge

How to use npmlog.clearProgress:

251
252
253
254
255
256
257
258
259
260
  const addList = add.map(a => `  ${a.replace(/@$/, '')}`)
    .join('\n') + '\n'
  const prompt = `Need to install the following packages:\n${
  addList
}Ok to proceed? `
  npmlog.clearProgress()
  const confirm = await read({ prompt, default: 'y' })
  if (confirm.trim().toLowerCase().charAt(0) !== 'y') {
    throw new Error('canceled')
  }

How to use npmlog.disableUnicode:

95
96
97
98
99
100
101
102
103
104

## log.enableUnicode()

Force the unicode theme to be used for the progress bar.

## log.disableUnicode()

Disable the use of unicode in the progress bar.

## log.setGaugeTemplate(template)

How to use npmlog.enableUnicode:

91
92
93
94
95
96
97
98
99
100

## log.disableProgress()

Disable the display of a progress bar

## log.enableUnicode()

Force the unicode theme to be used for the progress bar.

## log.disableUnicode()

How to use npmlog.stream:

224
225
226
227
228
229
230
231
232
233

* {String} Default: ""

If set, a heading that is printed at the start of every line.

## log.stream

* {Stream} Default: `process.stderr`

The stream where output is written.

How to use npmlog.maxRecordSize:

196
197
198
199
200
201
202
203
204
205

* {Array}

An array of all the log messages that have been entered.

## log.maxRecordSize

* {Number}

The maximum number of records to keep.  If log.record gets bigger than

How to use npmlog.prefixStyle:

206
207
208
209
210
211
212
213
214
215
10% over this value, then it is sliced down to 90% of this value.

The reason for the 10% window is so that it doesn't have to resize a
large array on every log entry.

## log.prefixStyle

* {Object}

A style object that specifies how prefixes are styled.  (See below)

How to use npmlog.headingStyle:

212
213
214
215
216
217
218
219
220
221

* {Object}

A style object that specifies how prefixes are styled.  (See below)

## log.headingStyle

* {Object}

A style object that specifies how the heading is styled.  (See below)

How to use npmlog.heading:

218
219
220
221
222
223
224
225
226
227

* {Object}

A style object that specifies how the heading is styled.  (See below)

## log.heading

* {String} Default: ""

If set, a heading that is printed at the start of every line.