ApplicationInsights-node.js: Using trackEvent or trackException causes error

I’m using v1.1.0 of this package (looks like it was just released yesterday?). I have gotten set up with the basics, but when I try to add a trackEvent or trackException in my JavaScript code, I get an error:

import * as appinsights from 'applicationinsights';
appinsights.setup(appInsightsId);
appinsights.start();
....
appinsights.trackException({
    exception: new Error("handled exceptions can be logged with this method")
})

results in

These dependencies were not found:

* async_hooks in ./node_modules/cls-hooked/context.js
* child_process in ./node_modules/applicationinsights/out/Library/Sender.js, ./node_modules/
async-listener/index.js
* console in ./node_modules/diagnostic-channel-publishers/dist/src/console.pub.js
* dns in ./node_modules/async-listener/index.js
* fs in ./node_modules/applicationinsights/out/Library/Context.js, ./node_modules/applicatio
ninsights/out/Library/Sender.js and 1 other
* module in ./node_modules/diagnostic-channel/dist/src/channel.js, ./node_modules/diagnostic
-channel/dist/src/patchRequire.js
* net in ./node_modules/async-listener/index.js

To install them, you can run: npm install --save async_hooks child_process console dns fs module net
TypeError: applicationinsights__WEBPACK_IMPORT
ED_MODULE_0__.trackException is not a function

I tried installing the packages it suggests installing but that di not fix the problem. Any ideas on how to fix this? I also tried reverting to v1.0.8 to see if that fixed the problem but still got a similar error.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 17 (8 by maintainers)

Most upvoted comments

Some things to try here. When setting up the SDK, add setInternalLogging(true, true) to enable debug logging. Then after trackException, add appinsights.defaultClient.flush() to avoid having to wait for batching.

You should also provide an Error object to trackException, rather than a string. (It will technically accept a string gracefully, but this violates the type contract)

One extra note, you might not need the isServer and try/catch stuff if you’ve moved this code directly to an express server.

Oh! Nevermind! The exceptions showed up - I just wasn’t seeing them before. I see them now! I think the main fix was the fact that I needed to send an ‘Error’ type object instead of a string. Thank you so much!

I think this problem is specific to NextJS limitations on only supporting APIs that are available in both the server and client. It seems to relate to https://github.com/zeit/next.js/issues/173 and https://github.com/zeit/next.js/issues/4552#issuecomment-395519758

Here’s a workaround you can do - though I’m not sure how particularly stable it is.

Have the following in next.config.js:

module.exports = {
    webpack: (config) => {
        config.node = {
            console: false,
            child_process: 'empty',
            dns: 'empty',
            fs: 'empty',
            module: 'empty',
            net: 'empty'
        };
        config.externals = {
            "async_hooks": "async_hooks",
            "console": "console"
        };
        return config
    }
};

In your code file, use this SDK in the following way:

const isServer = typeof window === 'undefined';
if (isServer) {
    try {
        const appInsights = require("applicationinsights");
        appInsights.setup('ikey').start();
        appinsights.defaultClient.trackException({ exception: new Error("Exception!") });
    } catch (e) {
        // Ignore client-side processing
    }
}

Another workaround I saw was to use a separate server (or supply Next with a custom one: https://github.com/zeit/next.js/#custom-server-and-routing) to perform Node.js-specific operations.