Shebang for Node JS and other languages

I am experimenting with an interesting site which curates a large set of APIs with sample code to retrieve data from the APIs - this is one exampe of a weather API:

The sample code gives examples in many different languages. Often the CURL example will work in KM as a shell script. But I would like to use Node JS or Javascript so I can manipulate/format the data after it is retrived

I am trying to figure out the right Shebang which works with Node JS specifically -and more generically for the many other languages the site supports.

I have tried:

#!/usr/bin node

#!/usr/bin/node

to no avail - though that is where Node is located when I search in Terminal.

If you want to test this example in KM, you can sign up for a RapidAPI account and do 1 million API calls in the free tier.

Can anyone help with a working example of a KM macro using Javascript or Node with this API or other examples on this site?

Hi @rkaplan,

Might be due to this?

The post above is a solution.
Or you may directly create a variable named "ENV_PATH" and add the paths there:

Thanks for the suggestion

I tried with this Shebang:

#!/usr/local/bin/Node

And I get this error:

image

Sorry. This error seems unrelated to Keyboard Maestro. I've never used Node before. So, I can't help with that.

If the reason to use JavaScript is mainly to manipulate/format the data after it is retrieved, then you don't need JavaScript at all. Keyboard Maestro is capable of it. If it is too explicated for Keyboard Maestro to process the returned data, you may pass it to another script to process it. For instance, you may use Shell Script to do the curl to retrieve the data, and then pass the returned data to JavaScript to process it if you are more comfortable with JavaScript (I'm saying this because you said the curl runs properly in ShellScript but not in Node and you'd like to process the data with JavaScript).

How do I pass data from curl in ShellScript to Node?

Try this:
image

#!/usr/local/bin/node

// get variable from KM whose name is "VarName" (without quotes)
// change VarName to whatever variable name you want.

var kmVar = process.env.KMVAR_VarName;
console.log(kmVar);

I'm sure you know that you may pass the Node result to KM variable or SystemClipboard etc.:
image

1 Like

Come back to your problem. I installed Node16 and did some tests. I can't see your error msg. You may open the Keyboard Maestro Engine log file to see the full message. I had a similar one when I tried to import the 'applescript' module:

2021-08-09 12:50:54 Execute a Shell Script failed with script error: node:internal/modules/cjs/loader:936
throw err;
^

Error: Cannot find module 'applescript'
Require stack:

  • /privatetext-script
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)
    at Object. (/privatetext-script:3:21)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12) {
    code: 'MODULE_NOT_FOUND',
    requireStack: [
    '/privatetext-script'
    ]
    }

This was because when I installed the applescript module, npm installed it to my Home directory. I have to specify the full module path in my code:

const applescript = require('/Users/myUserName/node_modules/applescript');

I suspect you might have the same issue. If so, you can do everything within Node by fixing this issue.

1 Like

Interesting

I am trying this very basic script:

image

And I get this:

Not sure I understand why "permission denied"

Can someone show an example of a simple shell script using Node that works in KM?

You had this:

But now, you are using this
image

@martin

Solved!

You were correct about the local of the Node modules

Both the simple and the complex Node examples worked fine with these as the initial lines

image

Huge thanks!