latlon-geohash icon indicating copy to clipboard operation
latlon-geohash copied to clipboard

unable to require w/ 2.0.0?

Open mmcfarland-novetta opened this issue 5 years ago • 4 comments

I can use this module through 'import' with both 1.1.0 and 2.0.0.

Using "const Geohash = require('latlon-geohash')" works for 1.1.0, but in 2.0.0 I see:

export default Geohash; ^^^^^^

SyntaxError: Unexpected token 'export' at Module._compile (internal/modules/cjs/loader.js:895:18) at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10) at Module.load (internal/modules/cjs/loader.js:815:32) at Function.Module._load (internal/modules/cjs/loader.js:727:14) at Module.require (internal/modules/cjs/loader.js:852:19) at require (internal/modules/cjs/helpers.js:74:18)

Can anyone confirm this, or am I missing something?

mmcfarland-novetta avatar Mar 05 '20 15:03 mmcfarland-novetta

Change to module.exports = Geohash instead of export default Geohash;

karvex avatar May 14 '20 00:05 karvex

Version 1.1.0 of the library uses old-style ES5-compatible CommonJS modules. CJS modules are imported with the require function, so const Geohash = require('latlon-geohash').

Version 2.0.0 uses the newer EcmaScript-Modules; ESM modules are imported with the import statement, so import GeoHash from 'latlon-geohash';.

Since Node.js v13.2.0, the import statement is available natively; from v8.0.0–v12.15.0 it is available with the esm package.

Apologies for the late response, hope that helps.

chrisveness avatar May 14 '20 15:05 chrisveness

@chrisveness After bumping to 2.0.0 and using Import statement the app doesn't work and returns the error

export default Geohash;
^^^^^^

SyntaxError: Unexpected token 'export'
    at Module._compile (internal/modules/cjs/loader.js:891:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Module.require (internal/modules/cjs/loader.js:848:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at eval (webpack-internal:///latlon-geohash:1:18)

Newst-team avatar Jul 31 '20 13:07 Newst-team

I'm getting this kind of error even though I import Geohash from 'latlon-geohash' when running tests with jest (node v14.15.4)

  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /home/slaweet/git/frontend/node_modules/latlon-geohash/latlon-geohash.js:241
    export default Geohash;
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      69 | import BigNumber from 'bignumber.js';
      70 | import Color from 'color';
    > 71 | import Geohash from 'latlon-geohash';
         | ^
      72 |
      73 | import { interpolateRgb } from 'd3-interpolate';
      74 | import colors from '@/constants/colors';

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at src/components/Events/EventsMap.vue:71:1
      at Object.<anonymous> (src/components/Events/EventsMap.vue:169:3)

Everything works well in browser when processed with webpack.

slaweet avatar May 24 '21 06:05 slaweet