SyntaxError: Cannot use import statement outside a module
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
I second this, getting the same error, did you figure it out?
nope :(
You can simply use: const {request, GraphQLClient} = require('graphql-request'); instead of using "import", should be all good then.
For some reason now, i'm having issues with "fetch" as not defined, for some reason.
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.
Fetch isn't natively supported in server-side node js. You have to use node-fetch or some other package
A repro repo would help here.