尝试以可视代码导入模块时,其显示意外令牌{

时间:2018-07-19 17:50:47

标签: javascript node.js import module export

您好,我正在使用可视代码来运行项目,这是我的代码

external.js

 export let keyValue=1000;

script.js

import {keyValue} from './external.js';
console.log(keyValue);

然后我有命令将文件作为节点脚本运行

然后当我在可视代码中运行节点脚本时,它显示以下错误

F:\es6-examples\modules-classes\script.js:1

  (function (exports, require, module, __filename, __dirname) { import 
  {keyValue} from './external.js';
                                                                 ^

SyntaxError: Unexpected token {
at new Script (vm.js:74:7)
at createScript (vm.js:246:10)
at Object.runInThisContext (vm.js:298:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:236:19)

这是屏幕截图 enter image description here

1 个答案:

答案 0 :(得分:-2)

它是语法错误问题。尝试此代码

(function (exports, require, module, __filename, __dirname) {import 
    {keyValue} from './external.js';
})