Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack - "Critical dependency: the request of a dependency is an expression" issue

I have an issue in an expressjs project.

When i run the npm run build command i get this error:

WARNING in ./node_modules/aws-crt/dist/native/binding.js 54:18-31
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/aws-crt/dist/native/io.js 16:34-54
 @ ./node_modules/aws-crt/dist/index.js 40:24-46
 @ ./node_modules/@aws-sdk/util-user-agent-node/dist-es/is-crt-available.js 3:96-114
 @ ./node_modules/@aws-sdk/util-user-agent-node/dist-es/index.js 5:0-52 16:23-37
 @ ./node_modules/@aws-sdk/client-s3/dist-es/runtimeConfig.js 16:0-65 26:898-914
 @ ./node_modules/@aws-sdk/client-s3/dist-es/S3Client.js 14:0-73 19:24-42
 @ ./node_modules/@aws-sdk/client-s3/dist-es/index.js 2:0-27 2:0-27
 @ ./src/server/data-handler/s3-api.js 1:0-46 4:21-29
 @ ./src/server/routes/freApi.js 5:0-51 39:4-20
 @ ./src/server/routes/index.js 3:0-55 3:0-55
 @ ./src/server/app.js 2:0-73 19:2-14 22:4-17 34:4-17 37:2-18 45:22-38
 @ ./src/server/index.js 35:0-24 53:18-21

WARNING in ./node_modules/express/lib/view.js 81:13-25
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/express/lib/application.js 22:11-28
 @ ./node_modules/express/lib/express.js 18:12-36
 @ ./node_modules/express/index.js 11:0-41
 @ ./libs/salesforce-express/build/app.js 10:39-57
 @ ./libs/salesforce-express/build/index.js 28:34-50
 @ ./src/server/index.js 34:0-76 36:12-31 37:13-33 54:12-35

2 warnings have detailed information that is not shown.
Use 'stats.errorDetails: true' resp. '--stats-error-details' to show it.

the npm scripts section looks like this

  "scripts": {
    "client:dev": "webpack-dev-server -d --config webpack/client/dev.babel.js",
    "start:dev": "BABEL_ENV=development MOCK_SERVER=true nodemon --watch src/server --exec babel-node src/server",
    "build:client": "webpack --config webpack/client/prod.babel.js",
    "build:server": "webpack --config webpack/server/prod.babel.js",
    "build": "npm run build:client && npm run build:server",
    "start": "BABEL_ENV=production MOCK_SERVER=true node ./dist/server/index.js",
    "test:watch": "jest --watch",
    "lint": "eslint --fix ./src --ext .js",
    "jest": "jest --colors",
    "test": "jest --colors --silent",
    "jest:update": "jest -u --colors",
    "coverage": "open ./coverage/index.html"
  },

Any ideas on what can solve this? Thanks

like image 532
David Gershtenkoren Avatar asked Oct 20 '25 17:10

David Gershtenkoren


2 Answers

Try to add this in you Next.config.js file :

  webpack: (config, { isServer, nextRuntime, webpack }) => {
  // Avoid AWS SDK Node.js require issue
  if (isServer && nextRuntime === "nodejs")
    config.plugins.push(
      new webpack.IgnorePlugin({ resourceRegExp: /^aws-crt$/ })
    );
   return config;
 },
like image 150
Vishal Avatar answered Oct 23 '25 06:10

Vishal


I has a similar issue in Next.js (that uses Webpack under the hood) using a custom package that used AWS SDK.

I found a solution : I installed aws-crt as a main dependency (not transitive from my custom package) and added in my webpack config

  resolve: {
    ...
    alias: {
      ...
      'aws-crt': path.resolve(__dirname, 'node_modules/aws-crt'),
    },
  }

This removes the warning, I couldn't really explain why.

I was inspired by this answer : https://github.com/aws-amplify/amplify-js/issues/11030#issuecomment-1500817501

like image 35
Mattgic_L Avatar answered Oct 23 '25 08:10

Mattgic_L