Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
35
rated 0 times [  39] [ 4]  / answers: 1 / hits: 31359  / 3 Years ago, sun, september 12, 2021, 12:00:00

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'

More From » function

 Answers
11

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.


[#50179] Wednesday, August 18, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emerymariamm

Total Points: 276
Total Questions: 97
Total Answers: 99

Location: Comoros
Member since Sun, Dec 13, 2020
4 Years ago
emerymariamm questions
Fri, Mar 4, 22, 00:00, 2 Years ago
Wed, Jan 12, 22, 00:00, 2 Years ago
Sun, Jul 4, 21, 00:00, 3 Years ago
Wed, Dec 23, 20, 00:00, 4 Years ago
;