Search code examples
javascriptes6-modules

node.js v18, only import works but export doesn't


I am using node.js v18.17.0 I have set in package.json that "type": "module"

I use import http from 'http' which doesn't give me any error However, if I try to use export like:

function renderStatus(url){
    var arr = ["/home", "/list"]
    return arr.includes(url)?200:404

}

export renderStatus

It gives me the following error:

SyntaxError: Unexpected token 'export'

Can anyone explain why this is happening? Thanks a lot!


Solution

  • As Being Shame said, export {renderStatus} should be the correct syntax. We can also export function renderStatus(url){...} to export a function.

    Read more here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export