what3words / w3w-node-wrapper

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

Node js + lambda function : Getting error of navigator is not defined. #29

Closed niveditavijayvargiya closed 2 years ago

niveditavijayvargiya commented 2 years ago

I am using this package in my lambda function written in Nodejs + typescript, i am getting error: offline: Failure: navigator is not defined ReferenceError: navigator is not defined at fetchGet (D:\Project\BoxSmart\BoxSmartApi\services\user.api.webpack\service\handler.js:52453:105) at convertTo3waBase (D:\Project\BoxSmart\BoxSmartApi\services\user.api.webpack\service\handler.js:52667:60) at Module.convertTo3wa (D:\Project\BoxSmart\BoxSmartApi\services\user.api.webpack\service\handler.js:52669:57) at convertCoordinatesToAddress (D:\Project\BoxSmart\BoxSmartApi\services\user.api.webpack\service\handler.js:64694:22) at baseHandler (D:\Project\BoxSmart\BoxSmartApi\services\user.api.webpack\service\handler.js:64971:89)

Using following package
@what3words/api": "^3.3.6",
"axios": "^0.21.4"
c5haw commented 2 years ago

Hi @niveditavijayvargiya

Could you show me a code snippet of your lambda please, including your import of any of the @what3words/api package please?

I believe you are importing the es2015 transpiled modules, which has been packaged up for use in the browser (rightly or wrongly) and the navigator object isn't available inside a lambda, hence the error you are experiencing. If you try the following I suspect it will resolve your issue:

import { convertTo3wa } from '@what3words/api/umd';
niveditavijayvargiya commented 2 years ago

I am using it in this way import * as api from '@what3words/api';

api.setOptions({ key: process.env.WHAT3WORDS_API_KEY });

export const convertCoordinatesToAddress = async (lat: any, lng: any) => { return await api.convertTo3wa({ lat, lng: lng }); }; export const convert3WordsToCoordinates = async (word: any) => { return await api.convertToCoordinates(word); };

export const autoSuggest = async (word: any, options: any) => { switch (options.clipTo) { case 'country': return await api.autosuggest(word, { clipToCountry: options.countries }); case 'circle': return await api.autosuggest(word, { clipToCircle: { center: { lat: options.lat, lng: options.lng }, radius: options.radius }, }); case 'boundry': return await api.autosuggest(word, { clipToBoundingBox: options.boundry, }); case 'polygon': return await api.autosuggest(word, { clipToPolygon: options.coordinates, }); case 'focus': return await api.autosuggest(word, { focus: { lat: options.lat, lng: options.lng }, nFocusResults: 2, }); } };

Package.json

"description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "@types/node": "^16.6.1", "@typescript-eslint/eslint-plugin": "^4.29.2", "@typescript-eslint/parser": "^4.29.2", "eslint": "^7.32.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-prettier": "^3.4.0", "prettier": "2.3.2", "typescript": "^4.4.3" }, "dependencies": { "@what3words/api": "^3.3.6", "axios": "^0.21.4" } } calling it in this way await convertCoordinatesToAddress(latitute, longitude);

c5haw commented 2 years ago

Can you try importing the UMD module and see if that resolves your issue please?

niveditavijayvargiya commented 2 years ago

Hi @c5haw , I tried using import * as api from '@what3words/api/umd';

api.setOptions({ key: process.env.WHAT3WORDS_API_KEY });

export const convertCoordinatesToAddress = async (lat: any, lng: any) => { return await api.convertTo3wa({ lat, lng: lng }); };

but now i am getting this error.

offline: ANY /dev/user (λ: user) offline: Failure: Cannot find module './constants' Error: Cannot find module './constants' at webpackEmptyContext (D:\Project\BoxSmart\BoxSmartApi\services\user.api.webpack\service\handler.js:53000:10) at D:\Project\BoxSmart\BoxSmartApi\services\user.api.webpack\service\handler.js:52545:23 at D:\Project\BoxSmart\BoxSmartApi\services\user.api.webpack\service\handler.js:52532:17 at Object.../../@what3words/api/umd/index.js (D:\Project\BoxSmart\BoxSmartApi\services\user.api.webpack\service\handler.js:52541:3) at __webpack_require__ (D:\Project\BoxSmart\BoxSmartApi\services\user.api.webpack\service\handler.js:86289:42) at Object.../../@rsconnectuk/what3words/index.ts (D:\Project\BoxSmart\BoxSmartApi\services\user.api.webpack\service\handler.js:64889:26)

For your reference my tsconfig file

{ "compilerOptions": {
"target": "ES2021",
"module": "commonjs", "declaration": true, "esModuleInterop": true,
"forceConsistentCasingInFileNames": true,

"strict": true, 
"skipLibCheck": true    

} }

c5haw commented 2 years ago

@niveditavijayvargiya unfortunately we've not been able to recreate your issue. We use the node-wrapper internally within lambdas also and have found that it is working without any issue.

import * as api from "@what3words/api";
import config from "config";
import { logger } from "./logger";

const configureApi = (): void => {
  api.setOptions({
    key: config.get("what3words.api.key"),
    baseUrl: config.get("what3words.api.baseUrl"),
  });
};

interface Coordinates {
  lat: number;
  lng: number;
}

const convertToCoordinates = async (address: string): Promise<Coordinates> => {
  configureApi();

  try {
    const response = await api.convertToCoordinates(address);
    logger.debug("Convert-to-coordinates response received: %o", response);

    const { coordinates } = response;
    return coordinates;
  } catch (err) {
    logger.error({ err }, "Error calling convert-to-coordinates");
    throw err;
  }
};

Could you share a code sandbox snippet that successfully recreates the issue please?

jamescrowley commented 2 years ago

@c5haw for reference, I've also encountered the "Cannot find module './constants'" error running with AWS CDK, TypeScript and AWS Lambda. See the referenced ticket #30

c5haw commented 2 years ago

@jamescrowley and @niveditavijayvargiya - I have PR which is currently in review that addresses the issue you've both been experiencing. The library will have improved handling of node and browser environments and should no longer require you to target a specific build output for an environment to use it. https://github.com/what3words/w3w-node-wrapper/pull/31. Once reviewed it should get deployed and will inform you here once it is ready.

c5haw commented 2 years ago

@niveditavijayvargiya 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.