Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: Unexpected token 'export' while exporting function Js [duplicate]

I'm trying to export a function from a Js file but recieving a Unexpected token error

const youtubeDownload = require("./youtube/youtube-download"); // export function youtubeDownload 
const twitterDonwload = require("./twitter/twitter-download"); // export function twitterDownload

function download(tweet) {
    if(tweet.in_reply_to_status_id_str == null) return youtubeDownload(tweet);
    if(tweet.in_reply_to_status_id_str != null) return twitterDonwload(tweet);
};

export { download };

This code is returning the error:

export { download };
^^^^^^

SyntaxError: Unexpected token 'export'
like image 556
script.igor Avatar asked Jul 10 '26 03:07

script.igor


1 Answers

It's because you are using CommonJS modules by default in NodeJS. CommonJS modules doesn't support export syntax. So you may need to use CommonJS export syntax for this.

const youtubeDownload = require("./youtube/youtube-download"); // export function youtubeDownload 
const twitterDonwload = require("./twitter/twitter-download"); // export function twitterDownload

function download(tweet) {
    if(tweet.in_reply_to_status_id_str == null) return youtubeDownload(tweet);
    if(tweet.in_reply_to_status_id_str != null) return twitterDonwload(tweet);
};

module.exports = { download };

Or if you really wants to use export syntax, you can use ES6 modules as here: https://stackoverflow.com/a/45854500/13568664.

like image 96
ChalanaN Avatar answered Jul 15 '26 21:07

ChalanaN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!