aws-sdk-js: Cannot be compiled with Babel 7

Hello, I get the following error trying to import the aws-sdk module in my react app.

ERROR in ../node_modules/aws-sdk/lib/event_listeners.js
Module not found: Error: Can't resolve 'util' in 'myproject-root/node_modules/aws-sdk/lib'

The app is compiled using @babel/core@7.1.2 and babel-loader@8.0.4 which are the last version on babel cores and loader.

Tried the same thing in another project using @babel/core@6.6.5 and babel-loader@7.1.2 and all works just fine.

Any idea? Thanks.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 20 (10 by maintainers)

Most upvoted comments

@chrisradek ok, I just found the bug and the solution and it was just simple and crazy at the same time.

The problem is basically introduced by a resolve alias override in the webpack config.

resolve: {
    alias: {
        assets: `${this.srcPathAbsolute}/assets/`,        
        app: `${this.srcPathAbsolute}/app/`,
        styles: `${this.srcPathAbsolute}/styles/`,
        // util: `${this.srcPathAbsolute}/util/`
    },
    extensions: ['.js', '.jsx'],    
}

Above the relevant configuration file that causes the issue. As you can see in the commented line, there is a new alias for util redefined as the path of a util file contained in the project. The skeleton of the project is part of a template that uses this alias for convenience, but in this case causes problems since the util alias redefinition change the result of each require('util') or import util from 'util'. I tried to comment that line and the aws-sdk import works just fine (for real this time).

To summarise, it doesn’t looks like the problem is from the aws-sdk itself but from a quirk in the config.

P.S. I will rename the alias to myUtil or something similar to avoid conflicts, but maybe could be safer to rename the aws-sdk util file too to something less common?

it solved my problem too…thanks guys 💯

@simonemazzoni

Thanks for working with us on this issue. I’m going to close this out. Appreciate the help.

@chrisradek ok, I just found the bug and the solution and it was just simple and crazy at the same time.

The problem is basically introduced by a resolve alias override in the webpack config.

resolve: {
    alias: {
        assets: `${this.srcPathAbsolute}/assets/`,        
        app: `${this.srcPathAbsolute}/app/`,
        styles: `${this.srcPathAbsolute}/styles/`,
        // util: `${this.srcPathAbsolute}/util/`
    },
    extensions: ['.js', '.jsx'],    
}

Above the relevant configuration file that causes the issue. As you can see in the commented line, there is a new alias for util redefined as the path of a util file contained in the project. The skeleton of the project is part of a template that uses this alias for convenience, but in this case causes problems since the util alias redefinition change the result of each require('util') or import util from 'util'. I tried to comment that line and the aws-sdk import works just fine (for real this time).

To summarise, it doesn’t looks like the problem is from the aws-sdk itself but from a quirk in the config.

P.S. I will rename the alias to myUtil or something similar to avoid conflicts, but maybe could be safer to rename the aws-sdk util file too to something less common?