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
Cannot use import statement outside a module #10470
Comments
To add something useful, I had a reply from the NextJS staff, in short OpenLayers is not compatible with ES Modules in NodeJS : |
Do you know what would be required to achieve such compatibility? I personally have no experience with this framework. Also maybe using a full build instead of ES6 modules will work better, e.g.: |
Hello @jahow, In short Next.js is a very popular and robust Javascript framework that allows to render pages built with React on the server (SSR), thus allowing fast loading and great SEO. I don't know the OL source code well, and it has changed since the last time I had a look at it (it was compiled with make if I am right). So I would need to dig and experiment with the current code base to see how big the modifications would be. I would say that this issue does not only concern a specific framework (Next.js), but all kind of JS frameworks that could render pages on server (Node.js). So the main concern is more to make OL compatible with Node.js than to make it compatible with X or Y. Also including the full build as you suggested is a workaround but not a long term solution. I'll keep you in touch, but I am very busy, so don't expect a return soon. Bonne soirée ;) |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@jahow, to achieve compatibility you have to respect the standards 😄 (it seems that not respecting written codes is a tradition where you work) Seriously, these days most packages distributed through NPM that are meant to be used only on the front-end do not respect the standard as using bundling and transpiling tools has become the norm. @jalik,
// next.config.js
const withTranspile = require('next-transpile-modules')(['ol']);
module.exports = withTranspile(); @jalik, I am currently working on OpenLayers SSR with next.js and I have a working demo that I will soon publish. |
@jalik , I have a working POC with SSR for https://github.com/mmomtchev/rlayers in the It currently works only for raster layers and fixed map sizes - as the server does not have a layout engine the map container must have a fixed size. You can check https://github.com/mmomtchev/rlayers-ssr-demo/blob/master/pages/index.js. I shimmed just a handful missing features in jsdom - almost everything else was there. I will probably prepare a medium article on running OpenLayers in Node.js in the coming days. |
Hey @mmomtchev great work with https://github.com/mmomtchev/rlayers , was working on orcasound-orcamap project which uses openlayers. I was trying to port to next.js and faced similar roadblock inevitably. Is there a proposal to have a package for stable rlayers? |
@parthsarthiprasad Everything but the SSR will probably be declared ready in the coming days. SSR has still a number of issues that must be solved before it is truly usable in a production environment - it needs its own worker model as multi-threading is very different in Node.js compared to the browser. |
Thanks will look out for this in future @mmomtchev , just wanted another suggestion, is there a way to have a support for nextjs and openlayers in production, as much as I've searched |
This is the solution - there is no way around it |
Describe the bug
Importing an OL component using syntax like
import Map from 'ol/Map'
does not work with NextJS framework.To Reproduce
npx create-next-app
and go to that foldernpm i -P ol
pages/index.js
import Map from 'ol/Map'
at the top of the filenpx next
Expected behavior
The import syntax of OL component should not fail when accessing the page.
System info
Node v12.14.0
OS: KDE neon User Edition 5.17 x86_64
The text was updated successfully, but these errors were encountered: