I am getting error when I import @google-cloud/storage in nodejs,
const gcs = require('@google-cloud/storage');
I have tried importing using:
/home/trd/TRD/new-rapi/copy/676cb539092d21127ded33478d1073ab6886fc33/node_modules/pify/index.js:3 const processFn = (fn, opts) = ^^^^^^^^^^
ReferenceError: Invalid left-hand side in assignment at new Script (vm.js:79:7) at createScript (vm.js:251:10) at Object.runInThisContext (vm.js:303:10) at Module._compile (internal/modules/cjs/loader.js:657:28) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) at Module.load (internal/modules/cjs/loader.js:599:32) at tryModuleLoad (internal/modules/cjs/loader.js:538:12) at Function.Module._load (internal/modules/cjs/loader.js:530:3) at Module.require (internal/modules/cjs/loader.js:637:17) at require (internal/modules/cjs/helpers.js:22:18) at Object. (/home/trd/TRD/new-rapi/copy/676cb539092d21127ded33478d1073ab6886fc33/node_modules/make-dir/index.js:4:14) at Module._compile (internal/modules/cjs/loader.js:689:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) at Module.load (internal/modules/cjs/loader.js:599:32) at tryModuleLoad (internal/modules/cjs/loader.js:538:12) at Function.Module._load (internal/modules/cjs/loader.js:530:3)
When I try to import it as:
import * as Storage from '@google-cloud/storage';
^
I get Syntax error:
SyntaxError: Unexpected token *
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/home/trd/TRD/new-rapi/676cb539092d21127ded33478d1073ab6886fc33/server.js:41:15)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
@google-cloud/storage version:Making sure to follow these steps will guarantee the quickest resolution possible.
Thanks!
@TRDRakesh for the
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
syntax to work please update to the latest "@google-cloud/storage" version: "^2.5.0".
In "@google-cloud/storage" version: "^1.7.0" the correct import syntax would be
const Storage = require('@google-cloud/storage');
@AVaksman It's not working, I tried adding ^2.5.0 already and in 1.7.0 I was using the correct syntax
@TRDRakesh
Please see below the code snippets that I tried testing with
For
@google-cloud/storage version: ^2.5.0
Node.js version: 10.15.2
packaje.json dependencies section
"dependencies": {
"@google-cloud/storage": "^2.5.0",
"uuid": "^3.3.2"
}
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
const uuid = require('uuid/v4');
// Creates a client
const storage = new Storage();
const bucketName = `bucket-name-${uuid().split('-').shift()}`;
const bucket = storage.bucket(bucketName);
async function createBucket() {
// Creates the new bucket
await storage.createBucket(bucketName);
console.log(`Bucket ${bucketName} created.`);
await bucket.delete();
console.log('deleted');
}
createBucket();
md5-a993e929a406ed9921445b2af4ada3b8
```ts
// Imports the Google Cloud client library
const Storage = require('@google-cloud/storage');
const uuid = require('uuid/v4');
// Creates a client
const storage = new Storage();
const bucketName = `bucket-name-${uuid().split('-').shift()}`;
const bucket = storage.bucket(bucketName);
async function createBucket() {
// Creates the new bucket
await storage.createBucket(bucketName);
console.log(`Bucket ${bucketName} created.`);
await bucket.delete();
console.log('deleted');
}
createBucket();
These worked for me.
Please tell me what I should change to reproduce the issue you are experiencing.
To add to this... what version of node.js are you using?
To add to this... what version of node.js are you using?
Edited my previous post, using Node.js 10.15.2 for the code snippets.
@AVaksman doesn't work, gives the same issue
@TRDRakesh :wave: would you mind providing the following:
node --version
npm --version
@bcoe It's already mentioned, Node.js version: 10.15.2
npm version: 6.9.0
@TRDRakesh I apologize, missed the version in your original PR.
Could you please provide a gist with a minimum reproduction of the issue you're seeing?
@bcoe This is my package.json file:
{
"name": "angular-docs-app",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --prod",
"test": "ng test",
"lint": "tslint ./src/*/.ts -t verbose",
"e2e": "ng e2e",
"precache": "sw-precache --verbose --config=sw-precache-config.js",
"start:fr": "ng serve --aot --i18nFile=src/locale/messages.fr.xlf --i18nFormat=xlf --locale=fr"
},
"private": true,
"dependencies": {
"@angular/animations": "^5.1.1",
"@angular/cdk": "^5.0.2",
"@angular/common": "^5.0.0",
"@angular/compiler": "^5.0.0",
"@angular/core": "^5.0.0",
"@angular/forms": "^5.0.0",
"@angular/http": "^5.0.0",
"@angular/material": "^5.0.2",
"@angular/platform-browser": "^5.0.0",
"@angular/platform-browser-dynamic": "^5.0.0",
"@angular/router": "^5.0.0",
"@nicky-lenaers/ngx-scroll-to": "^0.6.0",
"core-js": "^2.4.1",
"ng2-page-scroll": "^4.0.0-beta.12",
"ng2-pdf-viewer": "^5.2.3",
"ngx-page-scroll": "^4.0.2",
"rxjs": "^5.5.2",
"zone.js": "^0.8.14"
},
"devDependencies": {
"@angular/cli": "1.6.0",
"@angular/compiler-cli": "^5.0.0",
"@angular/language-service": "^5.0.0",
"@types/jasmine": "~2.5.53",
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"codelyzer": "^4.0.1",
"jasmine-core": "~2.6.2",
"jasmine-spec-reporter": "~4.1.0",
"karma": "~1.7.0",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"ng2-scrollable": "^0.8.0",
"protractor": "~5.1.2",
"sw-precache": "^5.2.1",
"ts-node": "~3.2.0",
"tslint": "~5.7.0",
"typescript": "~2.4.2"
}
}
@TRDRakesh mind also providing an example of the code that pulls in the nodejs-storage dependency? I have a hunch as to what the issue might be, but would like to see your usage in context.
@bcoe I didn't understood which code you want,
I have tried importing using:
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
let gcs = require('@google-cloud/storage');
But I get the same error.
@TRDRakesh would you be able to provide the contents of server.js?
I have many things in server.js file, but I have tried using just one line, i.e:
const gcs = require('@google-cloud/storage');
gives the same error
@TRDRakesh in a new folder on your computer, e.g., /tmp/test, do the following:
bencoe@bencoe:/tmp/test$ node --version
v10.15.3
bencoe@bencoe:/tmp/test$ npm i @google-cloud/storage
npm WARN [email protected] No description
npm WARN [email protected] No repository field.
+ @google-cloud/[email protected]
updated 1 package and audited 728 packages in 0.982s
found 0 vulnerabilities
bencoe@bencoe:/tmp/test$ node
> const gcs = require('@google-cloud/storage');
undefined
Does this run without any error?
My output:
$ node --version
v10.15.2
$ sudo npm i @google-cloud/storage
[sudo] password for trd:
npm WARN saveError ENOENT: no such file or directory, open '/home/trd/package.json'
npm WARN enoent ENOENT: no such file or directory, open '/home/trd/package.json'
npm WARN trd No description
npm WARN trd No repository field.
npm WARN trd No README data
npm WARN trd No license field.
+ @google-cloud/[email protected]
added 96 packages from 113 contributors and updated 120 packages in 6.667s
$ node
> const gcs = require('@google-cloud/storage');
undefined
@TRDRakesh this suggests that when you install @google-cloud/storage in a fresh project, you do not receive the error:
SyntaxError: Unexpected token *
I'm guessing that somewhere in your server.js code there might be invalid syntax. Keep in mind that, unless you have a TypeScript compiler, or are using a module like esm, the syntax import * will not work, and will raise the exception you're seeing.
If you remove references to import * from your repo, I think things might work for you.
I get this error:
/home/trd/TRD/new-rapi/copy/676cb539092d21127ded33478d1073ab6886fc33/node_modules/pify/index.js:3 const processFn = (fn, opts) = ^^^^^^^^^^
ReferenceError: Invalid left-hand side in assignment at new Script (vm.js:79:7) at createScript (vm.js:251:10) at Object.runInThisContext (vm.js:303:10) at Module._compile (internal/modules/cjs/loader.js:657:28) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) at Module.load (internal/modules/cjs/loader.js:599:32) at tryModuleLoad (internal/modules/cjs/loader.js:538:12) at Function.Module._load (internal/modules/cjs/loader.js:530:3) at Module.require (internal/modules/cjs/loader.js:637:17) at require (internal/modules/cjs/helpers.js:22:18) at Object. (/home/trd/TRD/new-rapi/copy/676cb539092d21127ded33478d1073ab6886fc33/node_modules/make-dir/index.js:4:14) at Module._compile (internal/modules/cjs/loader.js:689:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) at Module.load (internal/modules/cjs/loader.js:599:32) at tryModuleLoad (internal/modules/cjs/loader.js:538:12) at Function.Module._load (internal/modules/cjs/loader.js:530:3)
I will see the syntax and correct it, I didn't use import * anywhere.
Thanks a lot!
Wait a second.....
$ sudo npm i @google-cloud/storage
If you're installing like that, it's very likely you're getting something different when you actually import your code. Let's do this:
In general, you really want to avoid globally installing modules like that, unless you're using them as command line tools.
Can you try this?
$ cd ~/
$ npm unlink @google-cloud/storage
$ cd <your-code-directory>
$ npm install
$ npm ls @google-cloud/storage
@JustinBeckwith Thanks a ton! It worked!!
@JustinBeckwith The issue is coming again Please help!
@bcoe Sir can you please look into the issue again
If you're having the same error you were having before, the steps we laid out above are probably the same fix.
@JustinBeckwith I tried it, It doesn't solve the issue.
Most helpful comment
@JustinBeckwith Thanks a ton! It worked!!