Mocha and geometryEngine, SyntaxError: Cannot use import statement outside a module

1299
2
10-16-2021 10:59 AM
jtfinlay
New Contributor

 

Hello! I'm using geometryEngine in my project and have been running into CJS Syntax issues when I rely on geometryEngine.

I've been reading through the mocha and babel docs and am having a difficult time figuring out what is wrong here. I suspect imports within the library may not be playing well with CJS.

I've simplified the issue to this example (the JS branch repros without TS): Jtfinlay/arcgis-import-problems at js (github.com)

When running yarn test, I hit the following error:

$ mocha 'test/**/*.test.js'

D:\repos\repro\node_modules\@arcgis\core\geometry\geometryEngine.js:5
import{G as n}from"../chunks/geometryEngineBase.js";import{hydratedAdapter as e}from"./geometryAdapters/hydrated.js";function r(n){return Array.isArray(n)?n[0].spatialReference:n&&n.spatialReference}function t(e){return n.extendedSpatialReferenceInfo(e)}function u(t,u){return n.clip(e,r(t),t,u)}function i(t,u){return n.cut(e,r(t),t,u)}function o(t,u){return n.contains(e,r(t),t,u)}function c(t,u){return n.crosses(e,r(t),t,u)}function f(t,u,i){return n.distance(e,r(t),t,u,i)}function l(t,u){return n.equals(e,r(t),t,u)}function a(t,u){return n.intersects(e,r(t),t,u)}function s(t,u){return n.touches(e,r(t),t,u)}function p(t,u){return n.within(e,r(t),t,u)}function g(t,u){return n.disjoint(e,r(t),t,u)}function m(t,u){return n.overlaps(e,r(t),t,u)}function d(t,u,i){return n.relate(e,r(t),t,u,i)}function x(t){return n.isSimple(e,r(t),t)}function w(t){return n.simplify(e,r(t),t)}function E(t,u=!1){return n.convexHull(e,r(t),t,u)}function h(t,u){re

SyntaxError: Cannot use import statement outside a module
    at Module._compile (internal/modules/cjs/loader.js:891:18)
    at Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Object.newLoader [as .js] (D:\repos\repro\node_modules\pirates\lib\index.js:104:7)
    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 Object.<anonymous> (D:\repos\repro\src\/index.js:2:1)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Module._compile (D:\repos\repro\node_modules\pirates\lib\index.js:99:24)
    at 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 Object.<anonymous> (D:\repos\repro\test\/example.test.js:1:1)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Module._compile (D:\repos\repro\node_modules\pirates\lib\index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Object.newLoader [as .js] (D:\repos\repro\node_modules\pirates\lib\index.js:104:7)
    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 Object.implementationOfRequireOrImportForUnstableEsm [as requireOrImport] (D:\repos\repro\node_modules\mocha\lib\nodejs\esm-utils.js:101:12)
    at Object.exports.loadFilesAsync (D:\repos\repro\node_modules\mocha\lib\nodejs\esm-utils.js:88:34)
    at Mocha.loadFilesAsync (D:\repos\repro\node_modules\mocha\lib\mocha.js:461:19)
    at singleRun (D:\repos\repro\node_modules\mocha\lib\cli\run-helpers.js:125:15)
    at exports.runMocha (D:\repos\repro\node_modules\mocha\lib\cli\run-helpers.js:190:10)
    at Object.exports.handler (D:\repos\repro\node_modules\mocha\lib\cli\run.js:374:11)
    at D:\repos\repro\node_modules\yargs\build\index.cjs:443:71
error Command failed with exit code 1.

Here is the yarn test command:

"test": "mocha 'test/**/*.test.js'"

 I also asked this question on stackoverflow: typescript - Arcgis js, import geometryEngine, SyntaxError: Cannot use import statement outside a mo...

Appreciate any help!

0 Kudos
2 Replies
ReneRubalcava
Frequent Contributor

It looks like you're running this in a node environment with ESM.

Check out our node sample.

You have a couple of options, but this would work.

  • Add "type": "module" to your package.json
  • Directory imports not allowed in node with esm, so you need `import callIIntoArcgis from '../src/index.js`
  • Add the ".js" extension to all your imports from "@arcgis/core"

That will get your tests and everything else working.

0 Kudos
jtfinlay
New Contributor

Thank you - no luck out of the gate with the three changes you listed, but I will debug through the `jsapi-node` resources for a bit and let you know if I make headway.

0 Kudos