-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Critical dependency: the request of a dependency is an expression" with webpack, Babel & TypeScript #1154
Comments
Are you trying to use the agent in a browser? If so, that's not supported. You might want to take a look at the JavaScript Real User Monitoring agent instead. It's been a while since I played around with Meteor, but if I recall correctly it allows you to write on app that runs both on the server and in the browser. |
@arichter83 During bundling, every bundler(including webpack) generates module graph needed to basically decide what goes in to the bundle. When there are dynamic @watson We can simply use some tricks to fool webpack if users do use elastic-apm-node in universal(server & browser) environments. We use dynamic requires here and here. |
@vigneshshanmugam The Node.js agent use dynamic require-statements in all the 3 locations pointed out by the warnings. The first two is not strictly needed and can just be ignored (you can see that they are also enclosed in try-catch statements), but the last is required to run the agent (there's even a 4th not mentioned in the warnings, I guess because it uses a template sting). But if we can be sure those warnings don't mean anything and we somehow silence them, I'd be all ears. I just imagine that there are many cases where the warnings are important if the user tries to compile the code to run in the browser, which obviously we can never support. |
Thanks for the insights. You are right, template strings inside the require are usually ignored because they are computed as well.
True, webpack warns because users might accidentally include the files on the browser which the code is never meant to run (like in our case). We can suppress warnings easily by either using template strings or using ideas like this https://github.com/getsentry/sentry-javascript/blob/bd35d7364191ebed994fb132ff31031117c1823f/packages/utils/src/misc.ts#L9-L11 |
Not sure what's best though: Showing the warning to the user so they know not to run it in the browser, or trick Webpack by obfuscating the code so it doesn't show the warning... 🤔 |
I am also not sure which would be the best approach and users might also use the library to run it on the server side with webpack. we can probably wait and decide later after hearing more feedback from our users. |
Hi. I know this is old, but have some info that might be relevant. The warning means webpack could not accurately determine exactly which files/modules to bundle, so it either a) bundled none of them, or b) bundled the ones it got "hints" about. (such as I've written a Webpack Plugin that allows elastic APM (more specifically require-in-the-middle) to work when used inside a webpack bundle. You can see the plugin at nodejs/require-in-the-middle#35 (comment) for reference. I've only tested this with Webpack 4. I might look at Webpack 5 soon. For the warnings, in Webpack 4 you can do the following in the webpack config to "silence" them. this.config.stats = {
// Filter out some warnings we don't want to be notified about.
// The warnings filter removes messages pertaining to dynamic imports which cannot be resolved at build time.
warningsFilter: (warning: string): boolean => {
return warning.includes('Critical dependency: the request of a dependency is an expression') &&
(
warning.includes('log4js/lib/appenders/index.js') ||
warning.includes('express/lib/view.js') ||
warning.includes('keyv/src/index.js') ||
warning.includes('elastic-apm-node/lib/agent.js') ||
warning.includes('elastic-apm-node/lib/config.js') ||
warning.includes('elastic-apm-node/lib/instrumentation/index.js')
)
}
} |
Today I hit same issue with some additional problem. All related with latest Angular (10.x) and @nguniversal/express-engine. I try install clean Angular installation with following steps:
Then i add to my server.ts: import * as apm from 'elastic-apm-node/start';
apm.start({
serviceName: process.env.ELASTIC_APM_SERVICE_NAME || `ferraweb-websites-dev`,
serverUrl: process.env.ELASTIC_APM_SERVER_URL || 'localhost:8200',
secretToken: process.env.ELASTIC_APM_SECRET_TOKEN,
}); When i try start
|
By use const webpack = require('webpack');
module.exports = {
resolve: {
extensions: ['.json'],
}
}; Now |
Hitting the same problem as of today, This happens during the Unfortunately I do not know what @havran's workaround means |
@RedactedProfile I'm not sure which part of @havran's work-around didn't make sense for you, so my apologies if I repeat things you already know. The angular project uses webpack to compile its source code. Angular has webpack configured to compile every module even if (like the Elastic Node.js Agent) they don't need to end up as part of the compiled frontend code. Additionally, angular has webpack configured in such a way that this line confuses it. Webpack, as configured, doesn't know that (It's probably worth noting that when I say "angular has webpack configured" that some of these may be webpack's default configuration setting) The @havran's work-around shows the configuration they used to tell webpack that So if I was in your shoes, I'd start with figuring out how to use |
Big thanks to @havran your work around fixed the problem I was having! |
is your application up to date? I tried, and still got issues... Running nx project on angular 15
|
Describe the bug
I am using a TypeScript setup with webpack and babel and get the following warnings when including
elastic-apm-node
.To Reproduce
Steps to reproduce the behavior:
npm install
npm start
How are you starting the agent? (please tick one of the boxes)
elastic-apm-node/start
from within the source codeThe text was updated successfully, but these errors were encountered: