Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS2304: Cannot find name 'global' #70

Closed
SazhinDima opened this issue May 24, 2020 · 7 comments
Closed

TS2304: Cannot find name 'global' #70

SazhinDima opened this issue May 24, 2020 · 7 comments

Comments

@SazhinDima
Copy link

I generated typescript file and try to import it.
But i have error "TS2304: Cannot find name 'global'" on line throw new global.Error("Value is larger than Number.MAX_SAFE_INTEGER");

Help me please.

@stephenh
Copy link
Owner

@SazhinDima are you trying to run the ts-proto output in Node on the backend or in a browser on the frontend?

@stephenh
Copy link
Owner

@SazhinDima I'm guessing that you're trying to run in the browser, where globalThis needs to be used instead of global. Can you try version 1.21.2? If you're still having issues, let me know more details about what environment you're trying to run in and what your tsconfig looks like.

@SazhinDima
Copy link
Author

@stephenh i'm runing in the Node.js (react project)

tscinfig:

{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"jsx": "react",
"target": "es2018",
"baseUrl": ".",
"experimentalDecorators": true,
"skipLibCheck": true,
"typeRoots": [
"./node_modules/@types"
]
},
"include": [
"src/**/*",
"globalCss.d.ts"
],
"exclude": [
"node_modules",
"dist"
]
}

@SazhinDima
Copy link
Author

If i put in top:
declare var global: any;

evethig works correct. But i don't know is it ok.

@stephenh
Copy link
Owner

stephenh commented May 24, 2020

You probably need to do npm install @types/node because global comes from node. This is not a ts-proto issue but just a TypeScript/node issue, so please use stackoverflow/etc. to resolve from here on out.

zfy0701 pushed a commit to sentioxyz/ts-proto that referenced this issue Jan 5, 2023
@JeViCo
Copy link

JeViCo commented Jul 17, 2023

If someone still have this issue, try using globalThis instead

@stephenh
Copy link
Owner

Hi @JeViCo , thanks for the note; per globalThis, yes, at this point we have a pretty elaborate function to get the right global object:

const tsProtoGlobalThis: any = (() => {
  if (typeof globalThis !== "undefined") {
    return globalThis;
  }
  if (typeof self !== "undefined") {
    return self;
  }
  if (typeof window !== "undefined") {
    return window;
  }
  if (typeof global !== "undefined") {
    return global;
  }
  throw "Unable to locate global object";
})();

And then we do throw new tsProtoGlobalThis.Errror(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants