universal-geocoder / universal-geocoder-js

Universal Geocoder is a universal JavaScript library for server-side and client-side geocoding applications with multiple built-in providers.
https://universal-geocoder.github.io/universal-geocoder-js/
Other
11 stars 1 forks source link

Critical dependency: require function is used in a way in which dependencies cannot be statically extracted #21

Open creage opened 2 years ago

creage commented 2 years ago

Trying to build a TypeScript project using Webpack5 throws an error:

Compiled with problems:

WARNING in ./node_modules/universal-geocoder/dist/esm/utils.js 74:10-17

Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

Switching to CJS does not help - it fires same error for the cjs/utils.js

import UniversalGeocoder from 'universal-geocoder';

const geocoder = UniversalGeocoder.createGeocoder({
    provider: 'googlemaps',
    apiKey: 'KEY',
    useSsl: true,
    useJsonp: false
});

geocoder.geocode({
    text: '1600 Pennsylvania Ave, Washington, DC',
    locale: 'FR',
    limit: 10
}).then(console.log);
alanpoulain commented 2 years ago

It's because this project needs the crypto module only in Node.js: https://github.com/universal-geocoder/universal-geocoder-js/blob/4fbeed9cb6f293f95a04c60a963026c2aacb12f2/src/provider/googlemaps/GoogleMapsProvider.ts#L686 Since this library is isomorphic, I needed to use this special function to make it work everywhere: https://github.com/universal-geocoder/universal-geocoder-js/blob/4fbeed9cb6f293f95a04c60a963026c2aacb12f2/src/utils.ts#L90-L95 That's why TypeScript is telling you that it cannot extract the dependencies statically. It's only a warning though, not an error. If you find a better way to do it, please tell me.

creage commented 2 years ago

How about separating browser and node parts? And letting people import just the part they need?

The way it is implemented right now doesn't seem to be universal much. I mean, I can compile, but my CI/CD pipeline won't allow that code.