0

I am somewhat new to Node and I am running an extremely simple example from https://javascript.info/import-export in Node (10.17.0) to check the import functionality.

function.js

export function sayHi(user) {                                               
    alert('Hello, ${user}!');
}

I am calling this with:

say.js

import {sayHi} from './function.js';                                        

sayHi('Mike');

but for some reason when I run (on the command line)

node say.js

it returns

import {sayHi} from './function.js';
     ^

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:623:3)

It is probably very basic, but I can't see what I am doing wrong here.

Mike
  • 3,775
  • 8
  • 39
  • 79

1 Answers1

2

The way you are importing is Typescript way but node doesn't support Typescript, unless you write typescript and compile it to JavaScript.

Change your import like this

const sayHi=require('./function.js')
Shivam
  • 3,514
  • 2
  • 13
  • 27