js-sdk icon indicating copy to clipboard operation
js-sdk copied to clipboard

ReferenceError: window is not defined in NodeJS environment

Open jhlabs opened this issue 1 year ago • 3 comments

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.

jhlabs avatar Mar 13 '24 09:03 jhlabs

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?

shubham1172 avatar Mar 13 '24 10:03 shubham1172

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

jhlabs avatar Mar 13 '24 12:03 jhlabs

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

rzyns avatar May 25 '24 11:05 rzyns

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 avatar Jul 24 '24 12:07 dapr-bot

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.

dapr-bot avatar Jul 31 '24 12:07 dapr-bot