graphql-request icon indicating copy to clipboard operation
graphql-request copied to clipboard

SyntaxError: Cannot use import statement outside a module

Open asennoussi opened this issue 5 years ago • 7 comments

I'm trying to run the simple example: import { request } from 'graphql-request'

const query = { Movie(title: "Inception") { releaseDate actors { name } } }

request('https://api.graph.cool/simple/v1/movies', query).then(data => console.log(data)) But that's what I get when running: node file.js SyntaxError: Cannot use import statement outside a module

asennoussi avatar Feb 27 '20 09:02 asennoussi

I second this, getting the same error, did you figure it out?

stian-mrk avatar Jun 09 '20 09:06 stian-mrk

nope :(

asennoussi avatar Jun 09 '20 09:06 asennoussi

You can simply use: const {request, GraphQLClient} = require('graphql-request'); instead of using "import", should be all good then.

stian-mrk avatar Jun 09 '20 09:06 stian-mrk

For some reason now, i'm having issues with "fetch" as not defined, for some reason.

stian-mrk avatar Jun 09 '20 09:06 stian-mrk

To be honest.

To everyone using this package in Node.js, i recommend just using fetch instead, like so:

const fetch = require("node-fetch");

const query = `
  query {
    Lift(id: "panorama") {
      name
      status
    }
  }
`;
const url = "https://snowtooth.moonhighway.com/";
const opts = {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ query })
};
fetch(url, opts)
  .then(res => res.json())
  .then(console.log)
  .catch(console.error);
});

No errors, just works.

stian-mrk avatar Jun 09 '20 09:06 stian-mrk

Fetch isn't natively supported in server-side node js. You have to use node-fetch or some other package

coderinblack08 avatar Oct 24 '20 20:10 coderinblack08

A repro repo would help here.

jasonkuhrt avatar Oct 25 '20 01:10 jasonkuhrt