Nodejs-storage: Storage is not a contructor

Created on 5 Nov 2018  路  4Comments  路  Source: googleapis/nodejs-storage

Hi i am following a tutorial on storing images to firebase storage by using functions in react-native, I have followed the documentation on how to use google-cloud/storage and combined with the code to upload the images but i cant seem to successfully deploy the functions as i keep getting the error

Storage is not a constructor

firebase-tools :6.0.0
google-cloud/storage: 2.2.0

Environment details

  • OS: windows 10
  • Node.js version: v8.11.1
  • npm version: 6.1.0
  • @google-cloud/storage version:

Steps to reproduce

 const functions = require('firebase-functions');
const cors=require("cors")({origin:true})
const fs=require("fs");
const UUID=require("uuid");
const {Storage}=require("@google-cloud/storage");

const gcs = new Storage({
  projectId:"my-project-id",
  keyFileName:"project.json"
})

exports.storeImage = functions.https.onRequest((request, response) => {
    cors(request,responses,()=>{
        //Pass the reques body to turn it into a javascript object
        const body=JSON.stringfy(request.body);
        fs.writeFileSync("/tmp/uploaded-image.jpg",body.image,"base64",err=>{
          console.log(err);
          return response.status(500).json({err:err})
        });

        const bucket = gcs.bucket("myProject.appspot.com")
        const uuid=UUID();

        bucket.upload("/tmp/uploaded-image.jpg",{
          uploadType:"media",
          destination:"/places/"+UUID+".jpg",
          metadata:{
              metadata:{
                contentType:"image/jpeg",
                firebaseStorageDownloadTokens:UUID
              }
          }
        },(err,file)=>{
          if(!err){
            //Show link to the file
            response.status(201).json({
              imageUrl:"https://firebasestorage.googleapis.com/v0/b"+
                       bucket.name+
                       "/o/"+
                       encodeURIComponent(file.name)+
                       "?alt=media&token="+
                       uuid
            });
          }
          else{
            console.log(err)
            response.status(500).json({error:err})
          }
        })
    })
});

I have also tried to do this const gcs=new Storage() but still gives me the Storage is not a constructor error

storage question web

Most helpful comment

I think this will all come down to the version of @google-cloud/storage you're using. I know you mentioned 2.2.0 above, but I cannot reproduce the error. Since 2.0, it's switched to:

const {Storage} = require('@google-cloud/storage')

as opposed to:

const Storage = require('@google-cloud/storage')

Could you please confirm you're using that version of this library?

For reference, here's the test script I'm using:

package.json

{
  "name": "gissue-505",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@google-cloud/storage": "2.2.0"
  }
}

index.js

'use strict'

const {Storage} = require('@google-cloud/storage')
const storage = new Storage()
console.log('It worked!')

Terminal

$ node ./
It worked!

All 4 comments

I think this will all come down to the version of @google-cloud/storage you're using. I know you mentioned 2.2.0 above, but I cannot reproduce the error. Since 2.0, it's switched to:

const {Storage} = require('@google-cloud/storage')

as opposed to:

const Storage = require('@google-cloud/storage')

Could you please confirm you're using that version of this library?

For reference, here's the test script I'm using:

package.json

{
  "name": "gissue-505",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@google-cloud/storage": "2.2.0"
  }
}

index.js

'use strict'

const {Storage} = require('@google-cloud/storage')
const storage = new Storage()
console.log('It worked!')

Terminal

$ node ./
It worked!

I ran the script npm view @google-cloud/storage version and version is actually 2.3.0, i even updated my npm and also tried to run the project on my other pc but i still get this error

If you haven't already, would you try it with just my script?

Greetings! Another thing you can try here is to:

  • Delete your node_modules directory
  • Delete the package-lock.json file
  • Run npm install @google-cloud/storage
  • Try to run the script again :)

If this doesn't work out - please do let us know!

Was this page helpful?
0 / 5 - 0 ratings