最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ES6 modules in Visual Studio Code - Stack Overflow

programmeradmin1浏览0评论

To my big surprise the following trivial line:

export const PI = 3.14;

does not pile in Visual Studio Code and I am getting the following error:

(function (exports, require, module, __filename, __dirname) { export const PI = 3.14;                                                  

SyntaxError: Unexpected token export

Am I doing something wrong?

To my big surprise the following trivial line:

export const PI = 3.14;

does not pile in Visual Studio Code and I am getting the following error:

(function (exports, require, module, __filename, __dirname) { export const PI = 3.14;                                                  

SyntaxError: Unexpected token export

Am I doing something wrong?

Share Improve this question edited Apr 23, 2018 at 10:37 Unknown developer asked Apr 23, 2018 at 10:34 Unknown developerUnknown developer 5,97017 gold badges61 silver badges121 bronze badges 6
  • Could do you please explain what do you mean by is not running on Visual Studio Code – Hyyan Abo Fakher Commented Apr 23, 2018 at 10:36
  • @HyyanAboFakher he means the code doesnt pile – mast3rd3mon Commented Apr 23, 2018 at 10:37
  • Hmm , are you using the Code Runner Extension – Hyyan Abo Fakher Commented Apr 23, 2018 at 10:39
  • Code never really piles "in VS Code", it piles in some piler which is run by VS Code based on your configuration. You'll need to explain what piler settings you are using. – Quentin Commented Apr 23, 2018 at 10:39
  • Exactly, I am using Code Runner Extension – Unknown developer Commented Apr 23, 2018 at 10:40
 |  Show 1 more ment

2 Answers 2

Reset to default 4

You are using ES6 Module syntax.and node.js env which does not support ES6 Module syntax. NodeJS uses CommonJS Module syntax module.exports not ES6 module syntax export keyword.

Solutions:

  1. Use babel npm package to transpile your ES6 to a monjs target
  2. Refactor with CommonJS syntax.

To use ES6 Export module do you need add some transpiler to you project, if you don't want static type checker you can use Babel but if you want work with types in JS I remend you use TypeScript.

发布评论

评论列表(0)

  1. 暂无评论