Typescript Google API compilation errors - node.js

I'm using Typescript, tslint and Google APIs but there's issues compiling in typescript to javascript and I'm not sure why and for some reason I can't find anything specific online about this problem. Google searches don't render good results. I can't find a good example of how your tsconfig should be setup with this library also. So I'm coming here.
I'm running into lots of "Cannot find type definition file" and "Cannot find module" errors when I run tsc.
My file is literally just this line:
import {google} from 'googleapis'
That's it.
When I run tsc it gives me these errors:
$ tsc
node_modules/gaxios/build/src/common.d.ts:1:23 - error TS2688: Cannot find type definition file for 'node'.
1 /// <reference types="node" />
~~~~
node_modules/gaxios/build/src/common.d.ts:3:23 - error TS2307: Cannot find module 'https'.
3 import { Agent } from 'https';
~~~~~~~
node_modules/google-auth-library/build/src/auth/authclient.d.ts:16:23 - error TS2688: Cannot find type definition file for 'node'.
16 /// <reference types="node" />
~~~~
node_modules/google-auth-library/build/src/auth/authclient.d.ts:17:30 - error TS2307: Cannot find module 'events'.
17 import { EventEmitter } from 'events';
~~~~~~~~
node_modules/google-auth-library/build/src/auth/googleauth.d.ts:16:23 - error TS2688: Cannot find type definition file for 'node'.
16 /// <reference types="node" />
~~~~
node_modules/google-auth-library/build/src/auth/googleauth.d.ts:17:21 - error TS2307: Cannot find module 'fs'.
17 import * as fs from 'fs';
~~~~
node_modules/google-auth-library/build/src/auth/googleauth.d.ts:19:25 - error TS2307: Cannot find module 'stream'.
19 import * as stream from 'stream';
~~~~~~~~
node_modules/google-auth-library/build/src/auth/googleauth.d.ts:182:20 - error TS2503: Cannot find namespace 'NodeJS'.
182 _osPlatform(): NodeJS.Platform;
~~~~~~
node_modules/google-auth-library/build/src/auth/jwtaccess.d.ts:16:23 - error TS2688: Cannot find type definition file for 'node'.
16 /// <reference types="node" />
~~~~
node_modules/google-auth-library/build/src/auth/jwtaccess.d.ts:17:25 - error TS2307: Cannot find module 'stream'.
17 import * as stream from 'stream';
~~~~~~~~
node_modules/google-auth-library/build/src/auth/jwtclient.d.ts:16:23 - error TS2688: Cannot find type definition file for 'node'.
16 /// <reference types="node" />
~~~~
node_modules/google-auth-library/build/src/auth/jwtclient.d.ts:18:25 - error TS2307: Cannot find module 'stream'.
18 import * as stream from 'stream';
~~~~~~~~
node_modules/google-auth-library/build/src/auth/refreshclient.d.ts:16:23 - error TS2688: Cannot find type definition file for 'node'.
16 /// <reference types="node" />
~~~~
node_modules/google-auth-library/build/src/auth/refreshclient.d.ts:17:25 - error TS2307: Cannot find module 'stream'.
17 import * as stream from 'stream';
~~~~~~~~
node_modules/google-auth-library/build/src/crypto/crypto.d.ts:16:23 - error TS2688: Cannot find type definition file for 'node'.
16 /// <reference types="node" />
~~~~
node_modules/google-auth-library/build/src/crypto/crypto.d.ts:32:60 - error TS2580: Cannot find name 'Buffer'. Do you need to install type definitions for node? Try `npm i #types/node` and then add `node` to the types field in your tsconfig.
32 verify(pubkey: string | JwkCertificate, data: string | Buffer, signature: string): Promise<boolean>;
My tsconfig file is:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"esModuleInterop": true,
"noImplicitAny": false,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "app",
"baseUrl": "./app",
"paths": {
"*": [
"node_modules/*"
]
}
},
"include": [
"src/**/*"
]
}
My package dependencies are:
"dependencies": {
"googleapis": "^37.2.0",
"mysql": "^2.16.0",
"typescript": "^3.3.3333"
},
"devDependencies": {
"nodemon": "^1.18.10",
"tslint": "^5.12.1"
}
I'm not using the tsc node module (which is deprecated), I'm using typescript v3.3.3
Help would be appreciated! :)

Type errors in 3rd party libraries typings can easily be avoided by either submitting a pull request OR turning on skipLibCheck: true in your tsconfig and wait for them to be fixed.
Hope this helps.

Related

Typescript compiling errors using 'vss-web-extension-sdk' & 'azure-pipelines-task-lib'

I am trying to follow the following guide to make a custom task in azure devops https://learn.microsoft.com/en-us/azure/devops/extend/develop/add-build-task?view=azure-devops
When I install both 'azure-pipelines-task-lib' & 'vss-web-extension-sdk'
and have a single typescript file that requires the task-lib, like the guide suggests, I get tons of typescript errors.
package.json
"dependencies": {
"azure-pipelines-task-lib": "^3.1.9",
"typescript": "^4.4.3",
"vss-web-extension-sdk": "^5.141.0"
}
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es6",
"rootDir": "src/",
"outDir": "dist/",
"types": [
"vss-web-extension-sdk"
]
},
"files": [
"src/index.ts"
]
}
index.ts
import tl = require('azure-pipelines-task-lib/task');
export function Foo() {
return "BAR"
}
File structure
Root
tsconfig.json
package.json
src
index.ts
dist
index.js
Errors
https://imgur.com/4aNJetK
node_modules/#types/node/module.d.ts:2:5 - error TS2300: Duplicate identifier 'mod'.
2 export = NodeJS.Module;
~~~~~~~~~~~~~~~~~~~~~~~
node_modules/#types/requirejs/index.d.ts:38:14
38 export = mod;
~~~
'mod' was also declared here.
node_modules/#types/requirejs/index.d.ts:38:14 - error TS2300: Duplicate identifier 'mod'.
38 export = mod;
~~~
node_modules/#types/node/module.d.ts:2:5
2 export = NodeJS.Module;
~~~~~~~~~~~~~~~~~~~~~~~
'mod' was also declared here.
node_modules/#types/requirejs/index.d.ts:422:13 - error TS2403: Subsequent variable declarations must have the same type. Variable 'require' must be of type
'NodeRequire', but here has type 'Require'.
422 declare var require: Require;
~~~~~~~
node_modules/#types/node/globals.d.ts:213:13
213 declare var require: NodeRequire;
~~~~~~~
'require' was also declared here.
node_modules/vss-web-extension-sdk/typings/vss.d.ts:3168:13 - error TS2403: Subsequent variable declarations must have the same type. Variable 'require' must be of type 'NodeRequire', but here has type 'Require'.
3168 declare var require: Require;
~~~~~~~
node_modules/#types/node/globals.d.ts:213:13
213 declare var require: NodeRequire;
~~~~~~~
'require' was also declared here.
Can someone provide me with ANY working example of compiling typescript when these libraries are installed?

Can't import socket.io-client in angular project

Im trying to import socket.io-client into my project but the import line
import * as io from 'socket.io-client';
is throwing the following error
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (30,204): '>' expected.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (30,239): '(' expected.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (30,260): ',' expected.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (30,265): ',' expected.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (30,296): '(' expected.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (30,313): ',' expected.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (30,321): Expression expected.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (37,49): ';' expected.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (37,92): Expression expected.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (21,86): Cannot find name 'Parameters'.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (30,212): 'EventNames' only refers to a type, but is being used as a value here.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (30,241): Cannot find name 'ReservedEvents'.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (30,256): Cannot find name 'Ev'.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (30,262): Cannot find name 'Ev'.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (30,273): 'EventNames' only refers to a type, but is being used as a value here.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (30,298): Cannot find name 'UserEvents'.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (30,309): Cannot find name 'Ev'.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (30,315): Cannot find name 'never'.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (37,58): Cannot find name 'never'.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (37,94): Cannot find name 'T'.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (51,31): Class 'StrictEventEmitter<ListenEvents, EmitEvents, ReservedEvents>' incorrectly extends base class 'Emitter<string>'.
Types of property 'on' are incompatible.
Type '<Ev extends ReservedOrUserEventNames<ReservedEvents, ListenEvents>>(ev: Ev, listener: [Ev]) => this' is not assignable to type '(event: string, listener: Function) => Emitter<string>'.
Types of parameters 'listener' and 'listener' are incompatible.
Type 'Function' is not assignable to type '[any]'.
Property '0' is missing in type 'Function'.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (72,53): A rest parameter must be of an array type.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/typed-events.d.ts (82,75): A rest parameter must be of an array type.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/socket.d.ts (48,9): An accessor cannot be declared in an ambient context.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/socket.d.ts (73,53): A rest parameter must be of an array type.
ERROR in C:/Users/USER/Desktop/VSCProjects/CellularAss8/client/node_modules/socket.io-client/build/socket.d.ts (184,9): An accessor cannot be declared in an ambient context.
After digging around for a bit I found that while being in angular debug mode, if I comment the import line out, save, uncomment and save the code run perfectly in debug mode.
Is there anyway to fix it so the import will work without the steps i mention above?
Angular version is 4.4.6
and socket.io-client version is 1.7.3
and here is the tsconfig.json file
{
"compileOnSave": false,
"compilerOptions": {
"esModuleInterop": true,
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": ["node_modules/#types"],
"lib": ["es2017", "dom"]
},
"allowSyntheticDefaultImports": true
}
You are not using socket.io-client in version 1.7.3 but rather version ^4.1.3. That one defacto creates dependency on typescript 4 thanks to its packed type definition files but your project compiles with typescript ~2.3.3. Hence the error you are getting.
Make sure to install the correct version with npm i -S socket.io-client#1.7.3. It doesn't pack its own type definitions though (so it creates no dependency on typescript), so you will also need to install #types/socket.io-client#1 to get the types.
I'm not sure about the issue you mention but I might be able to provide a workaround.
You can try using this package instead https://www.npmjs.com/package/ngx-socket-io while working with Angular and Socket.io. I've used it in the past and it works well, it might solve your problem.
Simply install it and then configure it. Something like:
import { SocketIoModule, SocketIoConfig } from 'ngx-socket-io';
const config: SocketIoConfig = { url: 'http://localhost:8988', options: {} };
#NgModule({
declarations: [AppComponent],
imports: [BrowserModule, SocketIoModule.forRoot(config)],
bootstrap: [AppComponent],
})
export class AppModule {}

"error TS2304: Cannot find name 'Long'" when compiling typescript with google api libraries

I just added "#google-cloud/logging-winston":"2.1.0", in my pacakge.json and when I compile I get the following errors. I have seen this with other google libraries occasionally, and its root cause is most likely deeper in the stack in automatic generated types from protobuf definitions.
../node_modules/#google-cloud/logging/build/proto/logging.d.ts:1434:32 - error TS2304: Cannot find name 'Long'.
1434 line?: (number|Long|null);
~~~~
../node_modules/#google-cloud/logging/build/proto/logging.d.ts:1453:38 - error TS2304: Cannot find name 'Long'.
1453 public line: (number|Long);
~~~~
../node_modules/#google-cloud/logging/build/proto/logging.d.ts:1543:39 - error TS2304: Cannot find name 'Long'.
1543 requestSize?: (number|Long|null);
~~~~
../node_modules/#google-cloud/logging/build/proto/logging.d.ts:1549:40 - error TS2304: Cannot find name 'Long'.
1549 responseSize?: (number|Long|null);
~~~~
../node_modules/#google-cloud/logging/build/proto/logging.d.ts:1576:42 - error TS2304: Cannot find name 'Long'.
1576 cacheFillBytes?: (number|Long|null);
Here is how I worked around this issue until it is taken care of.
In your package.json dependencies section add "long":"4.0.0",
In your package.json devDependencies section: add "#types/long":"4.0.0",
Finally, in tsconfig.json (add to tsconfig.app.js if that doesn't work) (or in the tsc command line) add:
{
"compilerOptions": {
...
"types": [
...
"long"
],
...
}
gae123 suggestion did not work for me on typescript version 4.9.4.
I added the following to tsconfig.json. Hope this helps.
"compilerOptions":{
...
"skipLibCheck": true,
}
I solved adding the long packet and #types/long, as gae123 suggested, but now the type is not to add in tsconfig.json but in tsconfig.app.json.
"compilerOptions": {
...
"types": [...,"long"]
}
Thanks gae123!

Starting nestjs in production mode

I am using akveo backend bundle that I bought, and while everything seems to be working fine in development mode starting in production gives me following errors, I am new to nestjs itself.
Anyone know what's going here?
node_modules/#nestjs/core/adapters/http-adapter.d.ts:5:31 - error TS2420: Class 'AbstractHttpAdapter<TServer, TRequest, TResponse>' incorrectly implements interface 'HttpServer<TRequest, TResponse>'.
Property 'status' is missing in type 'AbstractHttpAdapter<TServer, TRequest, TResponse>' but required in type 'HttpServer<TRequest, TResponse>'.
5 export declare abstract class AbstractHttpAdapter<TServer = any, TRequest = any, TResponse = any> implements HttpServer<TRequest, TResponse> {
~~~~~~~~~~~~~~~~~~~
node_modules/#nestjs/common/interfaces/http/http-server.interface.d.ts:26:5
26 status(response: any, statusCode: number): any;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'status' is declared here.
node_modules/#nestjs/core/application-config.d.ts:2:39 - error TS2307: Cannot find module '#nestjs/common/interfaces/configuration-provider.interface'.
2 import { ConfigurationProvider } from '#nestjs/common/interfaces/configuration-provider.interface';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/#nestjs/core/guards/guards-context-creator.d.ts:3:39 - error TS2307: Cannot find module '#nestjs/common/interfaces/configuration-provider.interface'.
3 import { ConfigurationProvider } from '#nestjs/common/interfaces/configuration-provider.interface';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/#nestjs/core/interceptors/interceptors-context-creator.d.ts:2:39 - error TS2307: Cannot find module '#nestjs/common/interfaces/configuration-provider.interface'.
2 import { ConfigurationProvider } from '#nestjs/common/interfaces/configuration-provider.interface';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/#nestjs/core/nest-application.d.ts:24:5 - error TS2416: Property 'getHttpAdapter' in type 'NestApplication' is not assignable to the same property in base type 'INestApplication'.
Type '() => AbstractHttpAdapter<any, any, any>' is not assignable to type '() => HttpServer<any, any>'.
Property 'status' is missing in type 'AbstractHttpAdapter<any, any, any>' but required in type 'HttpServer<any, any>'.
24 getHttpAdapter(): AbstractHttpAdapter;
~~~~~~~~~~~~~~
node_modules/#nestjs/common/interfaces/http/http-server.interface.d.ts:26:5
26 status(response: any, statusCode: number): any;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'status' is declared here.
Found 5 errors.
I am using tsc -p tsconfig.build.json command to build it with tsconfig.build.json:
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "**/*spec.ts"]
}
and tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./"
},
"exclude": ["node_modules"]
}
I expect that this config should compile typescript code to javascript which then I would run with node dist/main.js command. Which it actually does. But I am worried about the typescript compiler errors.
Not sure without the code but got similar mismatches once upon a time in nest core packages. Check out package.json and ensure that all #nest packages have the same version, as sometimes they have 'breaking' changes even if it wasn't major release.
For an abstract example, something like that gave me errors related to core when building production:
"#nestjs/common": "6.6.0", // notice the core/common does not match, it should be bumped
"#nestjs/core": "6.6.2",

Error building angular2 library

This is my tsconfig-build.json used for taking the build
{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"sourceMap": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"outDir": "../dist"
},
"files": ["./index.ts"],
"angularCompilerOptions": {
"genDir": "compiled",
"strictMetadataEmit": true,
"skipTemplateCodegen": true
},
"exclude": [
"node_modules",
"dist",
"gh-pages",
"**/*.ngfactory.ts",
"**/*.shim.ts",
"**/*.spec.ts"
]
}
this is the script in my package.json file
"scripts": {
"prebuild": "rimraf ./dist ./compiled",
"build": "ngc -p ./src/tsconfig-build.json"
},
While running the script npm run build , Im getting missing error listed below.
../node_modules/#angular/core/src/di/reflective_provider.d.ts(87,123): error TS2304: Cannot find name 'Map'.
../node_modules/#angular/core/src/di/reflective_provider.d.ts(87,165): error TS2304: Cannot find name 'Map'.
../node_modules/rxjs/Observable.d.ts(58,60): error TS2693: 'Promise' only refers to a type, but is being used as a value here.
../node_modules/rxjs/Observable.d.ts(73,59): error TS2693: 'Promise' only refers to a type, but is being used as a value here.
../node_modules/#angular/core/src/change_detection/differs/iterable_differs.d.ts(14,48): error TS2304: Cannot find name 'Iterable'.
../node_modules/#angular/core/src/change_detection/differs/keyvalue_differs.d.ts(22,18): error TS2304: Cannot find name 'Map'.
../node_modules/#angular/core/src/change_detection/differs/default_iterable_differ.d.ts(12,32): error TS2304: Cannot find name 'Iterable'.../node_modules/#angular/core/src/change_detection/differs/default_keyvalue_differ.d.ts(24,16): error TS2304: Cannot find name 'Map'.
../node_modules/#angular/core/src/change_detection/differs/default_keyvalue_differ.d.ts(32,16): error TS2304: Cannot find name 'Map'.
../node_modules/#angular/common/src/directives/ng_class.d.ts(48,34): error TS2304: Cannot find name 'Set'.
../node_modules/ionic-angular/util/module-loader.d.ts(13,14): error TS2304: Cannot find name 'Map'.
../node_modules/ionic-angular/util/module-loader.d.ts(14,18): error TS2304: Cannot find name 'Map'.
../node_modules/ionic-angular/util/base-input.d.ts(2,38): error TS2307: Cannot find module '#angular/forms'.
../node_modules/ionic-angular/util/base-input.d.ts(3,27): error TS2307: Cannot find module '#angular/forms'.
../node_modules/ionic-angular/components/datetime/datetime.d.ts(2,38): error TS2307: Cannot find module '#angular/forms'.
../node_modules/ionic-angular/components/input/input.d.ts(2,27): error TS2307: Cannot find module '#angular/forms'.
../node_modules/ionic-angular/components/range/range.d.ts(2,38): error TS2307: Cannot find module '#angular/forms'.
../node_modules/ionic-angular/components/searchbar/searchbar.d.ts(2,27): error TS2307: Cannot find module '#angular/forms'.
../node_modules/ionic-angular/components/segment/segment.d.ts(2,27): error TS2307: Cannot find module '#angular/forms'.
../node_modules/ionic-angular/gestures/gesture-config.d.ts(1,37): error TS2307: Cannot find module '#angular/platform-browser'.
Its a simple angular directive for ionic app,What might be the reason
for above missing statements ?
Thanks
Change your tsconfig.json as
"compilerOptions": {
"target": "es5",
"lib": ["es5", "es6", "dom"], <--- this
...
}

Resources