Skip to content

Latest commit

 

History

History

jsapi-node

ArcGIS Maps SDK for JavaScript in Node.js

Integrating Node.js with @arcgis/core can be done by building the app with native ES modules or by transpiling to CommonJS (CJS). This sample contains examples of both approaches.


Known Issues

  • Using the SDK's projection engine in Node.js requires transpiling to CJS and using a local copy of the SDK's assets.
  • If you are getting the error ReferenceError: crypto is not defined and you are using Node 18, use the --experimental-global-webcrypto flag.
  • If you are getting the error TypeError: Cannot read properties of undefined (reading 'bind'), try upgrading to Node 18.19+ or use the --experimental-fetch flag, for example: node --experimental-fetch test-request.js or node --experimental-fetch test-webmap.js.

Get Started

Make sure you are running Node 18.19.0 or greater: node -v.

To run a test app, execute these commands in a terminal window:

  1. npm install - install the modules
  2. npm run build - run the build script
  3. node test-projection.js - run the app and the output will be written to the terminal window.

The source files are in the /src directory. Running the build command transpiles the files to CJS and outputs them in the /public directory. Only projection.js requires transpiling because it uses the SDK's projection engine. The .mjs files can be run natively, e.g. node ./src/request.mjs.

Working with assets

For most local builds, the API's assets are pulled from the ArcGIS CDN at runtime and there is no need for additional configuration. However, when working with certain modules, Node.js may require configuring the API to manage the assets locally. The assets include images, web workers, web assembly (.wasm) and localization files. Be sure to set config.assetsPath so that the assets are correctly resolved, for example:

import esriConfig from '@arcgis/core/config.js';
esriConfig.assetsPath = "node_modules/@arcgis/core/assets"; // relative to when running in root

An example can be found in projection.js and in test-webmap.js.

More information can be found in the SDKs Working with assets guide topic.

IdentityManager

You will also want to disable the IdentityManager using config.request so it doesn't attempt to load DOM-related JavaScript.

import esriConfig from "@arcgis/core/config.js";
esriConfig.request.useIdentity = false;