minio-js icon indicating copy to clipboard operation
minio-js copied to clipboard

NodeJS 13 ECMAScript Modules | Babel Error

Open roonie007 opened this issue 5 years ago • 8 comments

Hey,

We are using the minio module in our project using NodeJS 13 which now supports natively the Import/Export.

But for our tests and since Jest still does not work properly with NodeJS 13 (https://github.com/facebook/jest/issues/9430) we are using Babel for your tests .

Everything works perfectly with all other modules like mongoose, fastify, moment, multer etc... .

When we added Minio to our project and added our tests, when we run the project in NodeJS 13 everything works perfectly and here is the code :

import Minio from 'minio';

const minioClient = new Minio.Client({
  endPoint: process.env.S3_HOST,
  port: parseInt(process.env.S3_PORT),
  useSSL: false,
  accessKey: process.env.S3_ACCESS_KEY,
  secretKey: process.env.S3_SECRET_KEY
});

and when we run yarn test we get this error from Babel

Test suite failed to run

    TypeError: Cannot read property 'Client' of undefined
      4 | 
    > 5 | const minioClient = new Minio.Client({
        |                               ^
      6 |   endPoint: process.env.S3_HOST,
      7 |   port: parseInt(process.env.S3_PORT),
      8 |   useSSL: false,

But when we change the import Minio from 'minio'; to import * as Minio from 'minio'; the tests now works perfectly but when we run the server using NodeJS 13 we get this error

11:52:35 AM api.1  |  const minioClient = new Minio.Client({
11:52:35 AM api.1  |                      ^
11:52:35 AM api.1  |  TypeError: Minio.Client is not a constructor
11:52:35 AM api.1  |      at *****************************************************/media.js:5:21
11:52:35 AM api.1  |      at ModuleJob.run (internal/modules/esm/module_job.js:110:37)
11:52:35 AM api.1  |      at async Loader.import (internal/modules/esm/loader.js:167:24)

Can you help please ? I have been stuck with this from yesterday. More than 150 tests works with the other modules without any problems, but it the problem occurs only with minio.

Thanks

roonie007 avatar Mar 05 '20 10:03 roonie007

Hey @kaankabalak @kannappanr any news about this issue ?

roonie007 avatar Apr 07 '20 01:04 roonie007

Hey @kaankabalak @kannappanr any news about this issue ?

We are busy with other priorities right now @roonie007 feel free to send a PR yourself if you do have time.

harshavardhana avatar Apr 07 '20 01:04 harshavardhana

this is already fixed, will be released in >7.1.0

trim21 avatar May 07 '23 11:05 trim21

I am running 7.1.3 and still experience the error when trying to run a typescript file with tsx. It tells me:

TypeError: Cannot read properties of undefined (reading 'Client')

When trying to run it with bun I get:

Missing 'default' import in module '/Users/bjoernrave/projects/sightseer/node_modules/minio/dist/esm/minio.mjs'

BjoernRave avatar Oct 02 '23 18:10 BjoernRave

How are you importing @BjoernRave ?

prakashsvmx avatar Oct 03 '23 03:10 prakashsvmx

I am running 7.1.3 and still experience the error when trying to run a typescript file with tsx. It tells me:

TypeError: Cannot read properties of undefined (reading 'Client')

When trying to run it with bun I get:

Missing 'default' import in module '/Users/bjoernrave/projects/sightseer/node_modules/minio/dist/esm/minio.mjs'

minio doesn't have default exports, you should use wildcard import

import * as minio from 'minio'

trim21 avatar Oct 03 '23 04:10 trim21

@trim21 okay, it works like that.

Yes I was importing it like:

import Minio from 'minio'

Would be nice if it would work like that though, as that's how most imports work

BjoernRave avatar Oct 03 '23 10:10 BjoernRave

@trim21 okay, it works like that.

Yes I was importing it like:

import Minio from 'minio'

Would be nice if it would work like that though, as that's how most imports work

you can configure your babel to make it intercepting like this, or tsconfig allowSyntheticDefaultImports

trim21 avatar Oct 03 '23 10:10 trim21