question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

package.json: type module, but no exports map

See original GitHub issue

Describe the bug The ol package specifies in its package.json that it’s of type module. This means that any folder-style imports like import Map from 'ol/Map'; are prohibited when using it inside a project that itself is of type module. According to the documentation, they should work.

To Reproduce Steps to reproduce the behavior:

  1. Clone https://github.com/maxicarlos08/sveltekit-ol
  2. npm ci
  3. npm run build
  4. See error about an invalid directory import

Expected behavior Folder imports should work.

To make them work, you need to add an exports map to your package.json. Beware though, this means that anything not listed in the exports map is no longer accessible from the outside, so strictly speaking this would be a breaking change.

cc @maxicarlos08 who opened the issue over at https://github.com/sveltejs/kit/issues/3037

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tschaubcommented, Dec 15, 2021

As mentioned in sveltejs/kit#3037, here is a working index.svelte route with no modifications to the ol package (or any other):

<script>
  import "ol/ol.css"
  import Map from 'ol/Map.js';
  import View from 'ol/View.js';
  import OSM from 'ol/source/OSM.js';
  import Tile from 'ol/layer/Tile.js';
  import {browser} from '$app/env';
  import {onMount} from 'svelte';

  let target;
  let map;

  onMount(() => {
    if (browser) {
      const view = new View({center: [0, 0], zoom: 2});
      const layer = new Tile({source: new OSM()});
      map = new Map({target, view, layers: [layer]});
    }
  });
</script>

<div class="ol-container" bind:this={target} />

<style>
  :global(html, body, #svelte),
  div {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
  }
</style>

The key difference between this example and and maxicarlos08/sveltekit-ol:

-       import { OSM } from 'ol/source';
+       import OSM from 'ol/source/OSM.js';

At some point it may also be possible to import {OSM} from 'ol/source.js'; (note the file extension). However, it looks like SvelteKit / Vite are also importing all the other modules that are imported and re-exported from the ol/source.js module (perhaps because there may be side effects). The one causing trouble is ol/source/GeoTIFF.js. We can make a change in this library to use Pool etc from the default export of the geotiff package, but the source also creates a worker using the Blob constructor, so it fails in Node.

Update: Even if we lazily create resources for the worker (avoiding references to Blob on import as in #13115), webpack and Node disagree on how to import things from the geotiff package.

webpack believes you should

import {Pool} from 'geotiff';

while Node believes you should

import geotiff from 'geotiff';

const Pool = geotiff.Pool;

Ideally this hardship could be avoided if tools let you completely opt out of SSR (like not even importing modules) for parts of your app that only work in a browser (maybe sveltejs/kit#1650 will bring this).

0reactions
tschaubcommented, Dec 16, 2021

There will probably continue to be things we can do to support importing ol modules in Node, but I’ll close this for now (I don’t think we need an exports map).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modules: Packages | Node.js v19.3.0 Documentation
In a package's package. json file, two fields can define entry points for a package: "main" and "exports" . Both fields apply to...
Read more >
New package.json `exports` field not working with TypeScript
json field, called exports , to select and rewrite exported files. But Typescript raises the 2307 error, saying that module is not found....
Read more >
Node.JS (New) Package.json Exports Field - Medium
The exports field (or “export map”) provides a way to expose your package modules for different environments and JavaScript flavors WHILE ...
Read more >
Use `package.json#exports` map · Issue #4155 - GitHub
Hey folks, this is very important: basically Parcel cannot use modules which support both common.js and ESM. We're forced to revert and drop...
Read more >
Package exports - webpack
The exports field in the package.json of a package allows to declare which module should be used when using module requests like import...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found