dapr / js-sdk

Dapr SDK for Javascript
Apache License 2.0
192 stars 80 forks source link

ReferenceError: window is not defined in NodeJS environment #585

Closed jhlabs closed 5 days ago

jhlabs commented 4 months ago

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 commented 4 months ago

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

jhlabs commented 4 months ago

@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.

rzyns commented 2 months ago

In https://github.com/protocolbuffers/protobuf-javascript/issues/8#issuecomment-1405033187 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 commented 1 week ago

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 commented 5 days ago

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.