Esri / arcgis-rest-js

compact, modular JavaScript wrappers for the ArcGIS REST API
https://developers.arcgis.com/arcgis-rest-js/
Apache License 2.0
353 stars 121 forks source link

Module Resolution not working with @tsconfig/node18/tsconfig.json #1123

Closed sepehr500 closed 1 year ago

sepehr500 commented 1 year ago

Describe the bug

Hello. I am trying to import @esri/arcgis-rest-request into my nodejs application written in typescript. I get the following error,

Could not find a declaration file for module '@esri/arcgis-rest-request'. 'node_modules/@esri/arcgis-rest-request/dist/cjs/index.js' implicitly has an 'any' type.

Unless I set "moduleResolution": "node10" .

But this gives me the error

tsconfig.lib.json(5,25): error TS5109: Option 'moduleResolution' must be set to 'Node16' (or left unspecified) when option 'module' is set to 'Node16'.

Because node10 is incompatible with @tsconfig/node18/tsconfig.json

I am on typescript 5.2.2. Using "@tsconfig/node18": "^18.2.1"

My tsconfig looks like this

  "extends": "@tsconfig/node18/tsconfig.json",
  "compilerOptions": {
    "lib": ["DOM"],
    "declaration": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "declarationMap": true,
    "sourceMap": true,
    "inlineSources": true,
    "sourceRoot": "/",
    "noEmitHelpers": true,
    "importHelpers": true,
    "outDir": "./dist",
  },
  "exclude": ["node_modules", "**/lib/**/*", "**/*.test.ts", "**/dist/**/*", "**/cdk.out/**/*"],
}

Reproduction

Create a project with the tsconfig listed above and dependencies listed above.

Logs

No response

System Info

@esri/arcgis-rest-request": "^4.2.0

Additional Information

No response

gavinr-maps commented 1 year ago

HI @sepehr500, thanks for the question. In order to help you, we need some more details on your setup. Specifically a step-by-step replication case that we can follow that will show me the issue. For example:

  1. Run mkdir arcigs-rest-js-demo
  2. Run cd arcgis-rest-js-demo
  3. Run npm init
  4. Run npm install @esri/arcgis-rest-request
  5. etc
  6. etc
  7. etc
  8. Expected behavior:
  9. Actual behavior:

If you post a replication case like the above, I'd be happy to try it out and see if I get the same issue as you.

patrickarlt commented 1 year ago

@sepehr500 This is probably happening because of a mismatch between how you are importing the files and how your package.json is setup.

For example:

If you are using ES modules:

import { searchItems } from "@esri/arcgis-rest-portal";

Then you need to have "type": "module" set in your package.json,

If you are using common JS

const { searchItems } = require("@esri/arcgis-rest-portal");

Then you can omit "type": "module" or set "type": "commonjs"

Since @tsconfig/node18 is setting "module": "node16", on your tsconfig.json you need to follow Nodes module resolution rules in your files for loading Common JS vs ESM modules.


If this still doesn't work then we will need a full reproduction case that we can run locally.

sepehr500 commented 1 year ago

Thanks. I will give this a shot

sepehr500 commented 10 months ago

That worked! Thanks.