'How to use package with js? Installed the package, but getting: “SyntaxError: Cannot use import statement outside a module”

This is my first time using js so bear with me (I’m an experienced python programmer).

How am I supposed to use this repo in js: https://github.com/omerdn1/otter.ai-api

I installed the package with npm and created the setup script (and updated it to fit my login info), but I’m getting a SyntaxError: SyntaxError: Cannot use import statement outside a module at the first line import OtterApi from 'otter.ai-api';.

I looked at the similar SO questions (1 and 2) and the solution seems to be adding ”type”: “module” to the package.json file. But 1) I’m not sure if they actually me “module” or its a specific module name 2) I tried this with both “module” and “OtterApi” and I’m still getting the SyntaxError.

I’m totally lost. Don’t know how modules work in js. Would greatly appreciate your help. :)

Edit: I replaced the import with the following instead: const OtterApi = require('otter.ai-api')

This seems to work as the initial error is no longer there. However, I’m now getting a separate SyntaxError (which is may be unrelated to the first error?): SyntaxError: await is only valid in async functions and the top level bodies of modules.

This is from the following code:

const OtterApi = require(‘otter.ai-api’);

const otterApi = new OtterApi({
  email: '[email protected]', // Your otter.ai email
  password: 'abc123!#', // Your otter.ai password
});

await otterApi.init() // Performs login

The error happens at the last line.



Solution 1:[1]

I used the setup code, but I replaced the import with the following instead: const OtterApi = require('otter.ai-api').

Apparently, you can’t use ES Modules in Node without jumping through some hoops (or maybe using the latest version?). So, the above resolves that issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jacques Thibodeau