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

ReferenceError: window is not defined in NodeJS environment #585

Closed
jhlabs opened this issue Mar 13, 2024 · 5 comments
Closed

ReferenceError: window is not defined in NodeJS environment #585

jhlabs opened this issue Mar 13, 2024 · 5 comments
Assignees
Labels
bug Something isn't working stale

Comments

@jhlabs
Copy link

jhlabs commented Mar 13, 2024

Expected Behavior

I expect to be able to launch the Dapr Javascript SDK in a NodeJS environment when running in a Docker container.

Actual Behavior

My different Typescript services that import the Dapr JS SDK launch just fine in my local environment. As soon as I try to run them packaged in a Docker Container I receive the following error on startup:

2024-03-13 10:51:44 /app/build/index.js:96157
2024-03-13 10:51:44       return this || window || global2 || self || Function("return this")();
2024-03-13 10:51:44                      ^
2024-03-13 10:51:44 
2024-03-13 10:51:44 ReferenceError: window is not defined
2024-03-13 10:51:44     at /app/build/index.js:96157:22
2024-03-13 10:51:44     at ../../node_modules/@dapr/dapr/proto/dapr/proto/common/v1/common_pb.js (/app/build/index.js:96158:7)
2024-03-13 10:51:44     at __require (/app/build/index.js:12:51)
2024-03-13 10:51:44     at ../../node_modules/@dapr/dapr/implementation/Server/GRPCServer/GRPCServerImpl.js (/app/build/index.js:100699:23)
2024-03-13 10:51:44     at __require (/app/build/index.js:12:51)
2024-03-13 10:51:44     at ../../node_modules/@dapr/dapr/implementation/Server/GRPCServer/GRPCServer.js (/app/build/index.js:101244:44)
2024-03-13 10:51:44     at __require (/app/build/index.js:12:51)
2024-03-13 10:51:44     at ../../node_modules/@dapr/dapr/implementation/Server/DaprServer.js (/app/build/index.js:123257:40)
2024-03-13 10:51:44     at __require (/app/build/index.js:12:51)
2024-03-13 10:51:44     at ../../node_modules/@dapr/dapr/index.js (/app/build/index.js:135564:40)
2024-03-13 10:51:44 
2024-03-13 10:51:44 Node.js v20.11.1

I don't have a great explanation for that. It seems that the Dapr SDK somehow thinks it is in a browser environment and therefore requires modules like window?

Steps to Reproduce the Problem

Package any application that uses the Dapr JS SDK in a Docker container and run the container.

@shubham1172
Copy link
Member

Hi, this looks like an issue due to the protobuf library that we're using, see protocolbuffers/protobuf-javascript#8. There is a workaround mentioned in it, does that work for you?

@shubham1172 shubham1172 added the bug Something isn't working label Mar 13, 2024
@jhlabs
Copy link
Author

jhlabs commented Mar 13, 2024

@shubham1172 thanks for the quick reply. I tried using this workaround but still encountered the issue. It is unfortunately also not workable for all our services since we use Sveltekit that can run on both server and client. Using this workaround broke the build for Sveltekit.

@shubham1172 shubham1172 self-assigned this Mar 28, 2024
@rzyns
Copy link

rzyns commented May 25, 2024

In protocolbuffers/protobuf-javascript#8 (comment) the workaround is this:

// @ts-ignore
globalThis.window = null;
// @ts-ignore
globalThis.self = null;

@jhlabs, in case you haven't tried it already, how about something like this? 🤔

// @ts-ignore
if (typeof window === 'undefined') globalThis.window = null;
// @ts-ignore
if (typeof self === 'undefined') globalThis.self = null;

That shouldn't interfere with Sveltekit

@dapr-bot
Copy link
Collaborator

This issue has been automatically marked as stale because it has not had activity in the last 60 days. It will be closed in the next 7 days unless it is tagged (pinned, good first issue, help wanted or triaged/resolved) or other activity occurs. Thank you for your contributions.

@dapr-bot dapr-bot added the stale label Jul 24, 2024
@dapr-bot
Copy link
Collaborator

This issue has been automatically closed because it has not had activity in the last 67 days. If this issue is still valid, please ping a maintainer and ask them to label it as pinned, good first issue, help wanted or triaged/resolved. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale
Projects
None yet
Development

No branches or pull requests

4 participants