3

this is my code.

import app  from './core/app'

const PORT=process.env.PORT || 3000;

app.express.listen(PORT,()=>{
    console.log(`server up and running on port ${PORT}`)
});

when i am importing my core module from core folder giving this error.

(function (exports, require, module, __filename, __dirname) { import app  from './core/app'
                                                              ^^^^^^

SyntaxError: Unexpected token import
pergy
  • 5,285
  • 1
  • 22
  • 36
Amit kumar
  • 41
  • 1
  • 5
  • 2
    Possible duplicate of [Node error: SyntaxError: Unexpected token import](https://stackoverflow.com/questions/37634198/node-error-syntaxerror-unexpected-token-import) – Jeremy Thille Sep 25 '18 at 09:32

1 Answers1

3

The answer is, NodeJS environment currently does not support the import syntax. We only have one approach to load an external module is using the require() function.

You Nguyen
  • 9,961
  • 4
  • 26
  • 52
  • That's right, I use `import` all around the place, but I write Typescript, not Javascript. I had a look at the transpiled code, and all `import` disappear and are replaced with `require`. – Jeremy Thille Sep 25 '18 at 09:43
  • This might be a relevant article for you: https://medium.com/the-node-js-collection/an-update-on-es6-modules-in-node-js-42c958b890c – Kevin Vugts Sep 25 '18 at 10:02