admin管理员组

文章数量:1291584

I've been googling and searching everywhere and couldn't find an answer.

I'm just a little familiar with Typescript. Started working on my GraphQL NodeJS server and wanted to use Typescript for safer and easier coding.

The first thing needed to be done is set the current JS version to ES6 and so I did.

In addition I set my tsconfig that way:

{
"pilerOptions": {
    "module": "es6",
    "target": "es6",
    "noImplicitAny": false,
    "outDir": "./build",
    "sourceMap": true
},
"exclude": [
    "node_modules"
]

}

my index.js

import * as express from 'express';
import * as graphqlHTTP from 'express-graphql';
import {Schema} from './src/schema/Schema';

const PORT = 3000;

const app = express();

const graphqlOption: graphqlHTTP.OptionsObj = {
  schema: new Schema().getSchema(),
  pretty: true,
  graphiql: true
};

app.use('/graphql', graphqlHTTP(graphqlOption));

app.listen(PORT, ()=> {
  console.log("listening on PORT 3000");
});

When running this I'm getting

SyntaxError: unexpected token import

also, when hovering the const graphqlOption: graphqlHTTP.OptionsObj I get

Types are not supported by the current JavaScript version

Please help, what am i doing wrong?

Thanks in advance.

EDIT:

Wanted it to be clear that when I'm using a simple var express = require('express') I do not get this unexpected token and it moves to the next line

SCREENSHOTS:

I've been googling and searching everywhere and couldn't find an answer.

I'm just a little familiar with Typescript. Started working on my GraphQL NodeJS server and wanted to use Typescript for safer and easier coding.

The first thing needed to be done is set the current JS version to ES6 and so I did.

In addition I set my tsconfig that way:

{
"pilerOptions": {
    "module": "es6",
    "target": "es6",
    "noImplicitAny": false,
    "outDir": "./build",
    "sourceMap": true
},
"exclude": [
    "node_modules"
]

}

my index.js

import * as express from 'express';
import * as graphqlHTTP from 'express-graphql';
import {Schema} from './src/schema/Schema';

const PORT = 3000;

const app = express();

const graphqlOption: graphqlHTTP.OptionsObj = {
  schema: new Schema().getSchema(),
  pretty: true,
  graphiql: true
};

app.use('/graphql', graphqlHTTP(graphqlOption));

app.listen(PORT, ()=> {
  console.log("listening on PORT 3000");
});

When running this I'm getting

SyntaxError: unexpected token import

also, when hovering the const graphqlOption: graphqlHTTP.OptionsObj I get

Types are not supported by the current JavaScript version

Please help, what am i doing wrong?

Thanks in advance.

EDIT:

Wanted it to be clear that when I'm using a simple var express = require('express') I do not get this unexpected token and it moves to the next line

SCREENSHOTS:

Share Improve this question edited Aug 24, 2016 at 14:10 LazyOne 165k47 gold badges414 silver badges415 bronze badges asked Aug 24, 2016 at 12:26 Kesem DavidKesem David 2,2453 gold badges29 silver badges47 bronze badges 15
  • 2 stackoverflow./questions/36901147/… – Jaromanda X Commented Aug 24, 2016 at 12:30
  • @JaromandaX Can you please explain? – Kesem David Commented Aug 24, 2016 at 12:37
  • Have you read the accepted answer on that link? it explains SyntaxError: unexpected token import – Jaromanda X Commented Aug 24, 2016 at 12:39
  • 1 So far this is not a WebStorm error (as error happens during actual execution by node). Maybe it needs to be somehow transpilled first? (sorry, not a JS person at all so cannot really help here) – LazyOne Commented Aug 24, 2016 at 14:13
  • 1 Yes, if Typescript can pile to es5 then piling to es6 is a mistake. Currently there is ZERO javascript interpreters that support all of es6. People are either picky about using only supported features like arrow functions or they use an es5 to 6 piler. – slebetman Commented Aug 24, 2016 at 14:22
 |  Show 10 more ments

3 Answers 3

Reset to default 5

Go to:

File -> Settings -> Languages -> JavaScript

And enable ES6.

The problem is not with WebStorm or NodeJS, it is that you are not transpiling the ES6 module declarations into their ES5 CommonJS equivalent, so when you run the output you are getting an error as no version of Node implements ES6 modules.

Update your tsconfig.json:

"pilerOptions": {
    "module": "monjs",
    "target": "es5",
    ...
}

Thanks for all the help, eventually I've found the problem. If you'll look carefully enough you'll notice i do not have an index.ts and what I was trying to do by mistake is to run the index.js with Typescript syntax in it.

I've renamed it to index.ts, used tsc to create the build directory again and then ran the index.js created there, everything works.

Thanks for all the ments.

本文标签: javascriptES6 is not recognizedNodeJS and WebStormStack Overflow