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

self is not defined in NPM packages imported in a Worker #25008

Closed
oscarotero opened this issue Aug 12, 2024 · 1 comment · Fixed by #25062
Closed

self is not defined in NPM packages imported in a Worker #25008

oscarotero opened this issue Aug 12, 2024 · 1 comment · Fixed by #25062
Labels
bug Something isn't working correctly

Comments

@oscarotero
Copy link
Contributor

Version: Deno 1.45.5

I have the following code:

// worker.js
import prism from "npm:[email protected]";

console.log(prism);
console.log(self);

That it's executed inside a worker:

new Worker(new URL('./worker.js', import.meta.url), { type: 'module' });

And this produces the following error:

error: Uncaught (in worker "") (in promise) ReferenceError: self is not defined
    at Object.<anonymous> (file:///Users/oscarotero/Library/Caches/deno/npm/registry.npmjs.org/prismjs/1.29.0/prism.js:11:48)

Looks like the self object is not defined inside Prism npm package. It's accessed here:

var _self = (typeof window !== 'undefined')
	? window   // if in browser
	: (
		(typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)
			? self // if in worker
			: {}   // if in node js
	);

Replacing self with globalThis, the script works fine:

var _self = (typeof window !== 'undefined')
	? window   // if in browser
	: (
		(typeof WorkerGlobalScope !== 'undefined' && globalThis instanceof WorkerGlobalScope)
			? globalThis // if in worker
			: {}   // if in node js
	);

The problem seems to exist only in NPM packages, because the following js file run in a Worker doesn't fail:

console.log(self);
@lucacasonato lucacasonato added the bug Something isn't working correctly label Aug 12, 2024
@bartlomieju
Copy link
Member

Appears to be a regression from #24637 - self should be undefined in the main context, but defined in worker contexts.

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

Successfully merging a pull request may close this issue.

3 participants