CodaAPI and Node.js

My new project is to learn how to use Node.js.
It’s a whole new world for me and I think it will help me connect my Doc to APIs that Coda Pack will never ever support, my country’s mail service API for instance that uses SOAP API.
The thing is I’m stuck in the beginning.
I followed the instructions in
I got to install it using the command: npm install --save coda-js then I created a test file below using as instructed in the page using Atom. I also added my token.

import { Coda } from ‘coda-js’;
const coda = new Coda(’*********--’); // insert your token
// trick for using async in a script
(async () => {
const whoAmI = await coda.whoAmI();
})().catch((error) => console.log(error));

When I run “node mycode.js” I receive the error message below.

C:\Users\breno\teste>node teste5
(node:15152) Warning: To load an ES module, set “type”: “module” in the package.json or use the .mjs extension.
import Coda from ‘coda-js’;

SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1053:16)
at Module._compile (internal/modules/cjs/loader.js:1101:27)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47

I even tried to install globally -g, but I still I got the same message.

It told you what to do :slight_smile:

For any programming (e.g. JS) issues your best source of answers is googling the error, then examining results from StackOverflow (usually the first one or two) :slight_smile:

1 Like

Hi, @Paul_Danyliuk. Thank you for your help.
“type”: “module” in package.json didn’t do the job for me.
I used instead const { Coda } = require(“coda-js”) and I had also to remove “type”: “module” from package.json
There is post from the author explaining how to use it.

Congratulations and welcome to Node, @Breno_Nunes!

Depending on your use case, if you’d like to play with consuming the Coda API and expressing it in a web app, I highly recommend taking a look at 11ty. It’s a beautifully crafted, simple to get started, deceptively powerful SSG (static site generator) that is extremely easy to use with any combination of Markdown files and APIs to generate static websites.

And then it’s just a few clicks to host for free on Netlify and continuous deploy via your GitHub repo.

1 Like

Thank you very much.
I’ll definitely take a look at it.

1 Like