nchaulet/node-geocoder

SyntaxError: await is only valid in async functions and the top level bodies of modules

baditaflorin opened this issue · 1 comments

Code

const NodeGeocoder = require('node-geocoder');
 
const options = {
  provider: 'google',
 
  // Optional depending on the providers
  fetch: customFetchImplementation,
  apiKey: 'YOUR_API_KEY', // for Mapquest, OpenCage, Google Premier
  formatter: null // 'gpx', 'string', ...
};
 
const geocoder = NodeGeocoder(options);
 
// Using callback
const res = await geocoder.geocode('29 champs elysée paris');

Response:

node node_geocoder.js
/Users/florin/Work/OR/onerecord-api-database-query/node_geocoder.js:15
const res = await geocoder.geocode('29 champs elysée paris');
^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules
at Object.compileFunction (node:vm:355:18)
at wrapSafe (node:internal/modules/cjs/loader:1022:15)
at Module._compile (node:internal/modules/cjs/loader:1056:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47

Node version:
v15.14.0

Code

const NodeGeocoder = require('node-geocoder');
 
const options = {
  provider: 'google',
 
  // Optional depending on the providers
  fetch: customFetchImplementation,
  apiKey: 'YOUR_API_KEY', // for Mapquest, OpenCage, Google Premier
  formatter: null // 'gpx', 'string', ...
};
 
const geocoder = NodeGeocoder(options);
 
// Using callback
const res = await geocoder.geocode('29 champs elysée paris');

Response:

node node_geocoder.js
/Users/florin/Work/OR/onerecord-api-database-query/node_geocoder.js:15
const res = await geocoder.geocode('29 champs elysée paris');
^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules
at Object.compileFunction (node:vm:355:18)
at wrapSafe (node:internal/modules/cjs/loader:1022:15)
at Module._compile (node:internal/modules/cjs/loader:1056:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47

Node version:
v15.14.0

You can't use await outside a function. In your case you need to do:

const NodeGeocoder = require('node-geocoder');
 
const options = {
  provider: 'google',
 
  // Optional depending on the providers
  fetch: customFetchImplementation,
  apiKey: 'YOUR_API_KEY', // for Mapquest, OpenCage, Google Premier
  formatter: null // 'gpx', 'string', ...
};
 
const geocoder = NodeGeocoder(options);

;(async () => {
  const res = await geocoder.geocode('29 champs elysée paris');
})();