ReferenceError: self is not defined
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.
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 I might be too late for you guys, but here it is something worked for me:
Add the following plugin to your Webpack configuration. This will create a "global" variable that you will use lately before instantiating the map.
new webpack.DefinePlugin({
__CLIENT__: !isServer // Your logic to define if webpack is running in server or client mode
}),
(!!) If you are on TypeScript remember to define the variable in your "index.d.ts" file (or whatever you have)
declare var __CLIENT__: boolean;
In your (component | class | whatever) you need to use the mapbox-gl use it like this:
const mapboxGl = __CLIENT__
? require('mapbox-gl')
: {};
mapboxGl.accessToken = MAPBOX_TOKEN;
const map = new mapboxGl.Map({
container: this.mapContainer, // The reference to your DOM el.
style: 'mapbox://styles/mapbox/streets-v9',
center: [lng, lat],
zoom: 5,
});
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.
import mapbox from "mapbox-gl";
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.
The library has side effects, meaning it is executing and trying to access browser specific APIs.
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 ssr-safe-mapboxgl module which does a simple check on process.browesers and re export mapboxgl
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 gatsby-node.js
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
if (stage === 'build-html') {
actions.setWebpackConfig({
module: {
rules: [
{
test: /mapbox-gl/,
use: loaders.null(),
},
],
},
})
}
}
@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 MyMapView I can use import mapboxgl from 'mapbox-gl'; without the error.
This is still an issue for me as well. I've worked around it by internalizing (i.e. excluding from webpack-node-externals) webpack and then using webpack's null-loader to make sure that it isn't included.
👋 @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:
import { setRTLTextPlugin } from 'mapbox-gl';
but the import fails with ReferenceError: self is not defined (this self: https://github.com/mapbox/mapbox-gl-js/blob/master/src/util/browser/window.js#L5)
this can be solved with global/window: https://github.com/Raynos/global/blob/master/window.js
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:
import window from 'global/window';
On the second thought, global/window might be an overkill. I think a simple implementation that I provided as a pull-request is better. @kkaefer has rejected the change though, feel free to express your opinion here.
@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 window at import time - it's understood that code that references window shouldn't be used in a nodeJS environment, but given that there's useful code in the library that doesn't require window, it'd be nice if mapbox-gl could be imported without referencing window.
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: https://github.com/mapbox/headless-gl https://github.com/mapbox/mapbox-gl-js/issues/1447
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: https://github.com/mapbox/headless-gl #1447
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.