0

Hi am using visual code to run project here are my codes

external.js

 export let keyValue=1000;

script.js

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

Then I have command to run file as node script

Then its showing the below error when I run node script in visual code

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)

Here is screenshot enter image description here

roy
  • 39
  • 1
  • 3
  • 9
  • You cannot use `import` / `export` like that without a precompiler like babel. You need `const keyValue = require('./external.js).keyValue;` –  Jul 19 '18 at 18:04
  • Possible duplicate of [How can I use an es6 import in node?](https://stackoverflow.com/questions/45854169/how-can-i-use-an-es6-import-in-node) –  Jul 19 '18 at 18:11

1 Answers1

-1

Its a syntax error issue. Try this code

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