What version of Javascript is supported by BTT? Trying to do a fetch command an all I get is `find variable: fetch`

I've a simple script that fetches the current weather from DarkSky and returns the wind direction and speed. Which works fine in my browser, but in BTT I get AppleScript Error Error on line 10: ReferenceError: Can't find variable: fetch this is an weird error, because I've selected "Java Script" from the dropdown not "Apple Script". My code looks something like this:

const apiKey = "...";
const latitude = "...";
const longitude = "...";
const lang = 'en';
const units = 'ca';
const proxy = "https://cors-anywhere.herokuapp.com/";
const api = `${proxy}https://api.darksky.net/forecast/${apiKey}/${latitude},${longitude}?lang=${lang}&units=${units}&exclude=minutely,hourly`;

function fetchAPI() {
  fetch(api)
    .then(response => {
      return response.json();
    })
    .then(data => {
      const current = data.currently;
      const windBearing = current.windBearing;
      const windSpeed = current.windSpeed;
      return windSpeed + ' ' + windBearing;
    });
}
fetchAPI();

I am wondering what Javascript is allowed and what the reason for that is.

It is called JAX (Java Script for Automation) and is a quite limited subset of JavaScript.
In your case I'd really recommend to use the "Shell Script Widget" instead of the Apple Script one and run a nodejs script with it. That gives you the full power of nodejs/JS

This requires to have nodejs installed though (to get the path of the node executable run "which node" in Terminal)

I've got this working. And I have globally installed node-fetch to have fetch within node but all I get is the following. It looks like I don't have access to my node_modules , from within this place, but I've no idea how to fix this.

[eval]:10
    fetch(api)
    ^

ReferenceError: fetch is not defined
at [eval]:10:3
at Script.runInThisContext (vm.js:116:20)
at Object.runInThisContext (vm.js:306:38)
at Object.<anonymous> ([eval]-wrapper:9:26)
at Module._compile (internal/modules/cjs/loader.js:1121:30)
at evalScript (internal/process/execution.js:80:25)
at internal/main/eval_string.js:23:3

Can you try to add

global.fetch = require("node-fetch");

on top?

?

Oh yes right. I've been trying all kinds of these things, but nothing seems to work. I than get

internal/modules/cjs/loader.js:957
throw err;
^

Error: Cannot find module 'node-fetch'
Require stack:
- /[eval]
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:954:17)
at Function.Module._load (internal/modules/cjs/loader.js:847:27)
at Module.require (internal/modules/cjs/loader.js:1016:19)
at require (internal/modules/cjs/helpers.js:69:18)
at [eval]:1:15
at Script.runInThisContext (vm.js:116:20)
at Object.runInThisContext (vm.js:306:38)
at Object.<anonymous> ([eval]-wrapper:9:26)
at Module._compile (internal/modules/cjs/loader.js:1121:30)
at evalScript (internal/process/execution.js:80:25) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/[eval]' ]
}

But as far as I can see it is all installed

as you have multiple versions of node installed, are you sure v13.2.0 where you have node-fetch installed is being used? (node --version)

Yes as you can see in the screenshot node-fetch is installed in v13.2.0 and is also the version I use in BTT with the "Launch path;".

I usually don't work with globaly installed npm modules and use them locally with a package.json, but this is not something you can do with BTT right?

You can, if you just want to run a node js file like you do normally with node you'd just set the launch path to node and then use the path to the project as parameter.

However environment variables are not available in BTT by default because they are specific to one's shell, possibly that's causing some issues with node?

@Andreas_Hegenberg thanks you, this is much easier, now I can just use my normal code editor and have a file I can easily track with git and best of all it just works!

1 Like

awesome!