7

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'
1
  • 1
    Your code has to be in a module, as recognized by whatever environment is involved. Commented Sep 12, 2021 at 17:10

2 Answers 2

18

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.

Sign up to request clarification or add additional context in comments.

Comments

8

use module.exports={download} to export

use const {download}=require('<yourfilepath>') to import

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.