问题
I'm getting the below error every time with the first 'import' when I trying to run grandstack neo4j graphql api app.
PS C:\Users\grand-stack-starter-master\api> nodemon index.js
[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node index.js src/index.js`
C:\Users\grand-stack-starter-master\api\src\index.js:1
import { typeDefs } from "./graphql-schema";
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
[nodemon] app crashed - waiting for file changes before starting...
Below is my grandstack node.js code
import { typeDefs } from "./graphql-schema";
import { ApolloServer } from "apollo-server";
import { v1 as neo4j } from "neo4j-driver";
import { makeAugmentedSchema } from "neo4j-graphql-js";
import dotenv from "dotenv";
// set environment variables from ../.env
dotenv.config();
const schema = makeAugmentedSchema({
typeDefs
});
const driver = neo4j.driver(
process.env.NEO4J_URI || "bolt://localhost:7687",
neo4j.auth.basic(
process.env.NEO4J_USER || "neo4j",
process.env.NEO4J_PASSWORD || "xxxx"
)
);
const server = new ApolloServer({
context: { driver },
schema: schema
});
server.listen(process.env.GRAPHQL_LISTEN_PORT, "0.0.0.0").then(({ url }) => {
console.log(`GraphQL API ready at ${url}`);
});
I get this error for every import statement. If I remove the problematic import from the code(ex:import { typeDefs } from "./graphql-schema";) then I get the same error with the next import like below
import { ApolloServer } from "apollo-server";
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
[nodemon] app crashed - waiting for file changes before starting...
Can someone please help me to identify where it goes wrong
回答1:
Looks like there is something wrong with your package.json.
When you start the api I see this command:
[nodemon] starting `node index.js src/index.js`
So you are not using babel to transpile the code, and that's probably why your imports don't work. You should use the package.json in the grand-stack-starter (downloaded from the website) which calls the correct command to start the api server with the babel support.
"start": "./node_modules/.bin/nodemon --exec babel-node src/index.js"
As you can see this command uses babel-node to transpile your code using babel, this adds support for your imports that now should work properly, and you can run the api with just a npm start. If you use the package from the github repository, the code is little different and you should use the npm command:
npm start-dev
回答2:
In my case, I was in the wrong folder when using the command, for anyone else having this same issue, it might be the reason too, so make sure you are in the server folder. My case was of Express.js.
Happy Coding!
来源:https://stackoverflow.com/questions/58374279/syntaxerror-unexpected-token-when-compiling-my-node-js-code