what3words / w3w-node-wrapper

Node wrapper for the What3Words API
https://docs.what3words.com/api/v3/
MIT License
44 stars 10 forks source link

Cannot find module './constants' with esbuild + TypeScript #30

Closed jamescrowley closed 2 years ago

jamescrowley commented 2 years ago

Raising a separate issue to https://github.com/what3words/w3w-node-wrapper/issues/29 specifically for the second error Cannot find module './constants' mentioned there, as I'm reproducing currently in a similar set up (AWS lambda using CDK, which uses esbuild under the hood in their NodeJsFunction abstraction). This appears to be either an issue with your library, or with esbuild. I have raised an issue over there too, but logging here in case you have suggestions.

This is the simplified command that generates the transpiled-JS which triggers the 'cannot find module' error.

esbuild --bundle "index.ts" --platform=node

index.ts:


import * as what3words from "@what3words/api";

exports.handler = async function (event) {
  console.log('Received S3 event:', JSON.stringify(event, null, 2));
  what3words.setOptions({ key: "abc" });
  const what3wordsResponse = await what3words.convertTo3wa({lat:-23, lng:2});
  console.log(`Got what3words ${JSON.stringify(what3wordsResponse)}`);
};

tsconfig.json:


{
  "compilerOptions": {

  }
}

package.json:

{
  ...
  "dependencies": {
    "@what3words/api": "^3.3.6"
  }
}

and the error:

Runtime.ImportModuleError: Error: Cannot find module './constants'

I also experimented with importing @what3words/api/umd as suggested in the other ticket but that made no difference to the JS transpilation output.

c5haw commented 2 years ago

Hi @jamescrowley - thanks for the information. I have been able to reproduce the issue you are seeing.

It looks that the esbuild bundler is incorrectly bundling in the example provided.

As you are bundling your code with esbuild the easiest way to fix this in this case is to change the import of the @what3words/api library to

import * as what3words from '@what3words/api/es2015';
jamescrowley commented 2 years ago

@c5haw I haven't been able to get that to work. I get

"navigator is not defined",

in lambda, along with errors highlighting the use of 'fetch' and navigator, as I guess those are browser-only and not for a node environment?

jamescrowley commented 2 years ago

@c5haw having looked at your build process further, I guess we need the es2015 build, but without the browser targeting and the swapping in of the fetch.browser.ts. Would you be open to this being added? (happy to submit a PR). I guess it would have to be an additional distribution to avoid breaking compatibility at this stage, but all ears if you can think of a nicer solution, not sure how other libraries handle this

jamescrowley commented 2 years ago

@c5haw see also the comment here: https://github.com/evanw/esbuild/issues/1753#issuecomment-963796880 - it seems like the recommendation in the Typescript docs for node distributions is to use commonJS rather than umd

c5haw commented 2 years ago

Hi @jamescrowley - thanks for the information. I am working for a fix on this and should be finished soon. Apologies for the delay in getting back to you.

c5haw commented 2 years ago

@jamescrowley we've released a new version of the library that should resolve these issues you've had. Please check the updated documentation for the latest changes.