newrelic-webpack-plugin
An experimental plugin to allow New Relic's NodeJS Agent to work with webpack compiled server side javascript.
Quick Start
Add the plugin to your project
$ npm install @newrelic/newrelic-webpack-plugin
and then add and externals
and plugins
section to your webpack configuration like this.
/*...*/
const nodeExternals = require('newrelic-webpack-plugin/lib/externals')
const NewrelicWebpackPlugin = require('newrelic-webpack-plugin/lib/NewrelicWebpackPlugin')
module.exports = {
/* ... */
externals: [nodeExternals()],
plugins: [
new NewrelicWebpackPlugin()
]
/* ... */
}
How This Works
In order to use Newrelic's NodeJS agent with webpack, you'll need to
- Ensure the modules New Relic instruments are listed as webpack externals.
- Add
require('newrelic')
to the top of your generated sources.
This package allows you to do both. The nodeExternals
function
const nodeExternals = require('newrelic-webpack-plugin/lib/externals')
/* ... */
module.exports = {
/* ... */
externals: [nodeExternals()],
/* ... */
}
is borrowed from webpack-node-externals, which is the de-facto standard way to use webpack with NodeJS server side projects. If a module the agent instruments is not listed as a webpack external, the agent will not function properly.
The NewrelicWebpackPlugin
const NewrelicWebpackPlugin = require('newrelic-webpack-plugin/lib/NewrelicWebpackPlugin')
module.exports = {
/* ... */
plugins: [
new NewrelicWebpackPlugin()
]
/* ... */
}
ensures that a require('newrelic')
statment is added to the top of any .js
asset file generated by webpack.