SyntaxError: Cannot use import statement outside a module - and - ReferenceError: module is not defined in ES module scope

362 Views Asked by At

I'm attempting to create some API tests with Jestjs and Gotjs and typescript. Below is my snippet for initial.test.ts

import { got } from "got";

test('initial got jest test', async () => {
    const url = 'https://httpbin.org/anything';
    const response = await got(url);
});

This is giving me an error:

SyntaxError: Cannot use import statement outside a module

No, problem. There appears to be a published solution. Add the following to the package.json

SyntaxError: Cannot use import statement outside a module

Well, after adding that to the package.json, and running the script I have a new error:

ReferenceError: module is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/Users/christopher.carignan/Desktop/sunrun/testAutomation/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

This is really weird. Am I making the correct change to the packagae.json ?

1

There are 1 best solutions below

0
Sharan Balakrishnan On

Add the following code in index.d.ts file

declare module 'got' {
const content: any;
export default content;}

Folder Structure:

enter image description here

tsconfig file: (Add the folder to typeRoots parameter)

enter image description here