-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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: self is not defined #4593
Comments
Hi, thanks for using Mapbox. mapbox-gl-js is a client-side module; it doesn't work in node.js except in very specialized circumstances. Note that this issue tracker is for reporting bugs and feature requests. If you need general help with the node/webpack/browserify ecosystem, please refer to the documentation for those projects. Thank you! |
@jhjhjh96 Did managed to fix this error? |
Hi @rudza and @jhjhjh96 Add the following plugin to your Webpack configuration. This will create a "global" variable that you will use lately before instantiating the map.
(!!) If you are on TypeScript remember to define the variable in your "index.d.ts" file (or whatever you have) In your (component | class | whatever) you need to use the mapbox-gl use it like this:
Another way might be implementing the webpack-isomorphic-tool |
Please reopen this. The issue isn't so much that people are trying to render WebGL on the server, it's that mapbox-gl-js eagerly accesses browser-specific globals upon module import. The library shouldn't try to touch the DOM or global namespace at all until Mapbox is instantiated. Examples: |
The use case for reopening this is for an isomorphic website, we don't call or initialize the mapbox API, but it is still imported in the file, and this library does browser specific calls just from being imported, which it should not. I.e., this code should not do any execution. The library has side effects, meaning it is executing and trying to access browser specific APIs.
|
Same error on a Next.js app. |
I've hit this one too, in Gridsome |
Another use case affected by this issue: I have an internal library of shared JS code, bundled with webpack. Some of the code is front end (react components) and some could be useful on either front end or backend (functions). If I import mapbox-gl anywhere in this library then it can no longer be used on the backend, without some kind of hack. |
This is true, happening on any isomorphic node js project (in my case razzle) 😬 I think its expected for most of the features not work at all on SSR, but importing should not throw an expection 🙂 Workaround that we are doing here is having a let __ssr_safe__mapboxgl;
if (process.browser) {
__ssr_safe__mapboxgl = require("mapbox-gl");
}
export { __ssr_safe__mapboxgl }; Then we can use it anywhere on the app, nodejs safe import { __ssr_safe_mapboxgl as mapboxgl } from "./ssr-safe-mapboxgl";
const foo = new mapboxgl.Map(...) |
Here is the temporary workaround for Gatsby. Put this in
|
@omasback The above fix worked great for me using Gatsby, thank you. Saved me a lot of time figuring out the Webpack config. |
I got this error on a Next.js app too but got it to work using dynamic import without server-side rendering. import dynamic from 'next/dynamic'
const MyMapView = dynamic(() => import('../components/MyMapView'), { ssr: false }) Then in |
This is still an issue for me as well. I've worked around it by internalizing (i.e. excluding from |
👋 @mourner would it be possible to prioritize this? Looks like it makes a lot of people unhappy.. In my case, I'm trying to import:
but the import fails with |
this can be solved with Let me know which approach do you prefer and I can submit a PR if necessary. |
NameFILIP do you have an example of implementing global/window? For example is there some way to bring it into an index.js which contains the mapboxgl.Map? I'm not sure how exactly to use this solution. Does this enable server side rendering of the map? Sorry if my question is off base. |
@hjrobinson the usage will be the following:
On the second thought, |
@hjrobinson you're not going to be able to do a server render of the map - WebGL doesn't work that way. You can provide a global.window object, but that's essentially a workaround, not a fix. I'd argue that the most correct fix for this particular issue would be to not reference |
This is beyond my expertise, but I thought I would throw this out there. Any thoughts on using headless-gl to make server side rendering work with mapbox-gl.js? I noticed Mapbox has a fork and has discussed it in one of their issues. See links below: |
This issue isn't really about server-side rendering, it's just the fact that you can't even import the mapbox-gl library in node without an error. |
I'm facing this problem for the second time. The first time was for normal use of mapbox-gl in a .vue file. I work with Meteor and Vue.
I hope that you will be able to dedicate it some time soon. It's been knocking around for 3 years now! Could the error be caught and an INFO message printed? Does it have to crash everything? |
This is packed with workarounds for a very common use case. Server side rendering is around for many years, does mapbox really not consider this in their libraries? :( |
UPDATE: I moved my testing to Jest to try and solve this problem. This seems absurd
|
Fixes issue in Gatsby. Thank you! |
Any updates on this? I can't do a dynamic import in my project as the build tools doesn't support multiple entry/target points. Even though the project is never rendered server-side, it still causes this error simply by importing the module. |
Yes, I really don't like complaining, it's not fair on a lot of hard-working people. But this has to be the most annoying, show-stopping error that I've come across where nothing is happening. It keeps rearing its weird, ugly head all over the place, and I've wasted so much time on it. |
Hey all. I just want to chime in to assure people that we aren't ignoring this issue. We understand it's been frustrating for some specific use cases. The GL JS team is small though and we have to prioritize our work. So far, this issue hasn't become high priority because it represents a bit of an edge case. Additionally, there seem to be a number of workarounds in this thread for various setups.
WebGL is not available in server environments such as Node so server-side rendering is not something that GL JS has worried about. There's not much we can do on this front. That being said, it is possible to render GL JS in headless environments such as Selenium and Puppeteer using e.g. a Node driver. Finally, I'm not sure that it's a totally simple change to remove all the references to mapbox-gl-js/src/util/worker_pool.js Lines 56 to 57 in 59a484e
We've left this issue open because we'd like to eventually fix it. Hopefully this gives a little more context from our end. |
@ryanhamley I don't think the problem here is so much that it should render serverside as it is that we cannot even import it in very common projects like next.js, Gatsby, Metor etc. Sure the workarounds mentioned here "works" if you are building an application, and can conditionally render it. But in our case, we're building an npm module that DON'T support multiple entries/outputs which makes it impossible conditionally render mapbox. Rather than updating every single instance of window to work without it. Surely at the entry point you can just check if the window exists. If it does not exist, just don't try to render or run anything? This should be a fairly trivial task? Something like
We don't need it to work on the server. We just need it to not break if the npm module is simply installed in the project. |
@MarkLyck sums it up perfectly. I use Meteor, which has a grey area where server and client code can coexist. I realise it may seem all too simple to us on the outside, but all I'm looking for is:
|
@ryanhamley With all due respect, you still are totally missing what the actual issue is here. Pardon the shoutiness for emphasis, but: THIS HAS NOTHING TO DO WITH SERVER-SIDE RENDERING!This does have everything to do with the fact that browser-only globals are accessed on module import as a side-effect. Solving this problem seriously can't be any more difficult than gating browser-only side-effects behind functions and requiring them to be called upon map initialisation. I think this can probably even be done as a non-breaking change. Addendum: Again, really not trying to be snarky, I'd chip in more but I so don't have time to troubleshoot this right now, I'm working 50 hour weeks as it is and I don't have any map-related projects on the horizon. But my best guess is the problem originates here: mapbox-gl-js/src/util/window.js Line 20 in 11cbea8
|
mapbox-gl@1.10.0 Here's the trace I get, which is always the same:
@Aendrew as far as I can see @ryanhamley was saying exactly what you were saying about server-side rendering, except that he said it nicely. |
@mwarren2 Apologies if I came across as somewhat exasperated there, I really didn't mean to. But I've been following this issue for literally years now and it's really frustrating when the problem still being seen as "trying to use WebGL on the server" when it's absolutely not. Will try to take a deeper dive on this later today in penance for being a bit shouty there... 🙇🏼♀️ |
Hey friends, sorry for not getting to this earlier and misunderstanding the problem. I just made a quick attempt at shimming the window object when evaluating the browser bundle in Node at #9749 — can you check this out and see if it solves your case? I'm not sure because I've never worked with things like Meteor, but at least |
@mourner I'm confident that Meteor and node have one and the same problem, so we should be good |
@mourner You're awesome; thank you!!! 🙌 I did take a look as promised, but alas it seems I was looking in totally the wrong spot, my debugging methods would definitely not have caught that bit in the Rollup config. Apologies again for the earlier exasperation! |
Sorry to bother, but: I wanted to work with the solution but have been unable to install the current dev version which includes the merged solution. There was once an indication in a ReadME, but that seems to have disappeared. Here's what I did:
But when I start the app I get 'cant find module mapbox-gl'. |
@mwarren2 That should work. I tried building my Next.js app with both
Also, this will be included in a release next week (either a 1.11.0-beta.2 or a final 1.11.0 release). |
Looking at your steps, I should also have included at step 2 However it doesn't want to work, even with the master branch, so I'll wait for a release and no harm done. I'm guessing that Meteor is failing on the symlink and has to be configured somewhere to follow symbolic links. EDIT: All solved now as far as Meteor is concerned with 1.11.0 release. Thanks |
Gatsby wouldn't build with the older libraries -- see mapbox/mapbox-gl-js#4593 for full context. Note that dev mode worked. Avoided v5 of react-mapbox-gl as I encountered this bug: alex3165/react-mapbox-gl#904 This commit also adds updates the package-lock and removes a linting error which prevented TravisCI passing
Mapbox gl does not work in my node js.
-error code-
C:\Users\YJH\Documents\Visual Studio 2017\Projects\mapbox\mapbox\node_modules\mapbox-gl\dist\mapbox-gl.js:402
"use strict";module.exports=self;
ReferenceError: self is not defined
at Object.__dirname.196 (C:\Users\YJH\Documents\Visual Studio 2017\Projects\mapbox\mapbox\node_modules\mapbox-gl\dist\mapbox-gl.js:402:29)
at s (C:\Users\YJH\Documents\Visual Studio 2017\Projects\mapbox\mapbox\node_modules\mapbox-gl\dist\mapbox-gl.js:1:684)
at C:\Users\YJH\Documents\Visual Studio 2017\Projects\mapbox\mapbox\node_modules\mapbox-gl\dist\mapbox-gl.js:1:735
at Object.__dirname.194../window (C:\Users\YJH\Documents\Visual Studio 2017\Projects\mapbox\mapbox\node_modules\mapbox-gl\dist\mapbox-gl.js:398:25)
at s (C:\Users\YJH\Documents\Visual Studio 2017\Projects\mapbox\mapbox\node_modules\mapbox-gl\dist\mapbox-gl.js:1:684)
at C:\Users\YJH\Documents\Visual Studio 2017\Projects\mapbox\mapbox\node_modules\mapbox-gl\dist\mapbox-gl.js:1:735
at Object.__dirname.65.../package.json (C:\Users\YJH\Documents\Visual Studio 2017\Projects\mapbox\mapbox\node_modules\mapbox-gl\dist\mapbox-gl.js:139:26)
at s (C:\Users\YJH\Documents\Visual Studio 2017\Projects\mapbox\mapbox\node_modules\mapbox-gl\dist\mapbox-gl.js:1:684)
at e (C:\Users\YJH\Documents\Visual Studio 2017\Projects\mapbox\mapbox\node_modules\mapbox-gl\dist\mapbox-gl.js:1:855)
at C:\Users\YJH\Documents\Visual Studio 2017\Projects\mapbox\mapbox\node_modules\mapbox-gl\dist\mapbox-gl.js:1:873
I don't know, what was wrong.
I read a mapbox document. Initial setting also installed such as 'webpack', 'browserify' ect...
Help me solve this problem.
The text was updated successfully, but these errors were encountered: