How to use the error function from npmlog

Find comprehensive JavaScript npmlog.error code examples handpicked from public code repositorys.

npmlog.error is a method in Node.js that logs an error message to the console using the npmlog module.

87
88
89
90
91
92
93
94
95
}
const nodegypCmd = 'node-gyp rebuild ' + flags.join(' ')
log.info('install', `spawning node gyp process: ${nodegypCmd}`)
const child = child_process.exec(nodegypCmd, { maxBuffer: Infinity }, function(err, stdout, stderr) {
  const _err = err || stderr
  if (_err) log.error(_err)
})
child.stdout.pipe(process.stdout)
child.stderr.pipe(process.stderr)
fork icon794
star icon0
watch icon137

+ 12 other calls in file

131
132
133
134
135
136
137
138
            );
        } else {
            log.warn('Subscription', `Not sending email for list id:${list.id} because not send configuration is set.`);
        }
    } catch (err) {
        log.error('Subscription', err);
    }
}
fork icon703
star icon0
watch icon116

How does npmlog.error work?

npmlog.error is a method in the npmlog package that logs an error message with a given prefix and error object to the console and to a file if specified in the configuration. It also increments the error count of the logger instance.

56
57
58
59
60
61
62
63
64
65
    break;
case 'Complaint':
    if (req.body.Message.complaint) {
        campaigns.updateMessage(message, 'complained', true, (err, updated) => {
            if (err) {
                log.error('AWS', 'Failed updating message: %s', err.stack);
            } else if (updated) {
                log.verbose('AWS', 'Marked message %s as complaint', req.body.Message.mail.messageId);
            }
        });
fork icon702
star icon4
watch icon3

+ 15 other calls in file

29
30
31
32
33
34
35
36
37
38
    });
}

createDump(err => {
    if (err) {
        log.error('sqldump', err);
        process.exit(1);
    }
    log.info('sqldump', 'MySQL Dump Completed');
    process.exit(0);
fork icon702
star icon0
watch icon116

+ 13 other calls in file

Ai Example

1
2
3
4
5
6
7
const npmlog = require("npmlog");

// Set log level to "verbose"
npmlog.level = "verbose";

// Log an error message
npmlog.error("my-package", "Something went wrong!");

In this example, we first import the npmlog module. We then set the log level to "verbose", which means that all log messages (including errors) will be printed to the console. Finally, we use npmlog.error to log an error message with the package name "my-package" and the message "Something went wrong!". This will be printed to the console with a red "error" label to indicate that it's an error message.

134
135
136
137
138
139
140
141
142
* log.silly(prefix, message, ...)
* log.verbose(prefix, message, ...)
* log.info(prefix, message, ...)
* log.http(prefix, message, ...)
* log.warn(prefix, message, ...)
* log.error(prefix, message, ...)

Like `log.log(level, prefix, message, ...)`.  In this way, each level is
given a shorthand, so you can do `log.info(prefix, message)`.
fork icon453
star icon1
watch icon0

129
130
131
132
133
134
135
136
137
138
} else if (configFile.match(/\.json$/)) {
  this.readJSON(configFile, callback);
} else if (configFile.match(/\.yml$/)) {
  this.readYAML(configFile, callback);
} else {
  log.error('Unrecognized config file format for ' + configFile);
  if (callback) {
    callback.call(this);
  }
}
fork icon415
star icon0
watch icon69

85
86
87
88
89
90
91
92
93

  this.emit('tests-finish');

  return Bluebird.using(this.runHook('on_exit'), () => {});
}).catch(error => {
  log.error(error);
  log.info('Stopping ' + this.config.appMode);

  this.emit('tests-error');
fork icon415
star icon0
watch icon2

19
20
21
22
23
24
25
26
27
28
var cmd = isWin ? 'where' : 'which';

return new Bluebird.Promise(function(resolve) {
  var test = childProcess.spawn(cmd, [exe], options);
  test.on('error', function(error) {
    log.error('Error spawning "' + cmd + exe + '"', error);
  });
  test.on('close', function(exitCode) {
    return resolve(exitCode === 0);
  });
fork icon415
star icon0
watch icon2

216
217
218
219
220
221
222
223
224
225
    }else if (chr === 'd'){
      splitPanel.halfPageDown()
    }
    this.trigger('inputChar', chr, i)
  }catch(e){
    log.error('In onInputChar: ' + e + '\n' + e.stack)
  }
},
nextTab: function(){
  var currentTab = this.get('currentTab')
fork icon414
star icon1
watch icon48

+ 3 other calls in file

126
127
128
129
130
131
132
133
134
log.verbose('app', 'Initialising')

getHoodieServer(options, function (error, server, config) {
  if (error) {
    var stack = new Error().stack.split('\n').slice(2).join('\n')
    return log.error('app', 'Failed to initialise:\n' + stack, error)
  }

  log.verbose('app', 'Starting')
fork icon459
star icon0
watch icon1

+ 15 other calls in file

207
208
209
210
211
212
213
214
215
216
  var combined = (stdout + '\n' + stderr).trim()
  var command = 'git ' + args.join(' ') + ':'
  if (silent) {
    log.verbose(command, combined)
  } else {
    log.error(command, combined)
  }
  return cb(er)
}
log.verbose('mirrorRemote', from, 'git clone ' + cloneURL, stdout.trim())
fork icon322
star icon0
watch icon2

67
68
69
70
71
72
73
74
75
76
  }
} else if (err) {
  log.info('publish', 's3 headObject error: "' + err + '"');
  return callback(err);
} else {
  log.error('publish', 'Cannot publish over existing version');
  log.error('publish', "Update the 'version' field in package.json and try again");
  log.error('publish', 'If the previous version was published in error see:');
  log.error('publish', '\t node-pre-gyp unpublish');
  return callback(new Error('Failed publishing to ' + opts.hosted_path));
fork icon258
star icon0
watch icon0

+ 39 other calls in file

17
18
19
20
21
22
23
24
25
require('./lib/certs');

// Initialize database connection
db.connect(err => {
    if (err) {
        log.error('Db', 'Failed to setup database connection');
        errors.notify(err);
        return setTimeout(() => process.exit(1), 3000);
    }
fork icon245
star icon0
watch icon54

+ 131 other calls in file

143
144
145
146
147
148
149
150
151
152

res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('Content-Dispositon', 'attachment; filename=export.mbox');

output.on('error', err => {
    log.error('Audit', `Failed processing audit ${req.params.audit}: ${err.message}`);
    try {
        res.end();
    } catch (err) {
        //ignore
fork icon245
star icon0
watch icon54

+ 7 other calls in file

102
103
104
105
106
107
108
109
110
111
    if (!started) {
        started = true;
        return done(err);
    }

    log.error('ACME', err);
});

server.listen(config.acme.agent.port, config.acme.agent.host, () => {
    if (started) {
fork icon245
star icon0
watch icon54

+ 3 other calls in file

80
81
82
83
84
85
86
87
88
89
                }
            });
            return job;
        } catch (err) {
            // ignore?
            log.error('Events', err);
        }
        return false;
    }
};
fork icon245
star icon0
watch icon0

181
182
183
184
185
186
187
188
189
190
    }
} catch (error) {
    if (error.code === 40573) {
        // not a replica set!
        FORCE_DISABLE = true;
        log.error('Indexer', FORCE_DISABLED_MESSAGE);
        return;
    }

    if (this.changeStream.isClosed()) {
fork icon245
star icon0
watch icon54

+ 7 other calls in file

55
56
57
58
59
60
61
62
63
64
let key = keys[pos++];
let smtpProxy = new SMTPProxy(key, config.smtpInterfaces[key]);
smtpProxy.start(err => {
    if (err) {
        log.error('SMTP/' + smtpProxy.interface, 'Could not start ' + key + ' MTA server');
        log.error('SMTP/' + smtpProxy.interface, err);
        return done(err);
    }
    log.info('SMTP/' + smtpProxy.interface, 'SMTP ' + key + ' MTA server started listening on port %s', config.smtpInterfaces[key].port);
    smtpInterfaces.push(smtpProxy);
fork icon83
star icon526
watch icon44

+ 23 other calls in file

38
39
40
41
42
43
44
45
46
47
error message:

```js
mongoose.connect('localhost');
mongoose.connection.on('error', function(err) {
  log.error(err);
});
```

![](images/backend/beginner/logging-3.png)
fork icon49
star icon254
watch icon9

+ 2 other calls in file

117
118
119
120
121
122
123
124
125
126
var gitUrls = ['git+', 'git://']
var githubRepoUrls = /github\.com(?:\/[^/]+){2}($|#)/

// Determine which type of install we would like
rimraf(self.installTo, function (err) {
  if (err) log.error(err.message)
  if (self.useCache && fs.existsSync(self.cacheTo)) {
    return cacheInstall()
  } else if (gitUrls.indexOf(self.url.slice(0, 4)) !== -1) {
    return gitInstall()
fork icon33
star icon409
watch icon12