SyntaxError: Unexpected token 'export' while exporting function Js

SyntaxError: Unexpected token 'export' while exporting function Js

我正在尝试从 Js 文件中导出函数,但收到 Unexpected token 错误

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 };

此代码返回错误:

export { download };
^^^^^^

SyntaxError: Unexpected token 'export'

使用module.exports={download}导出

使用const {download}=require('<yourfilepath>')导入

这是因为您在 NodeJS 中默认使用 CommonJS 模块。 CommonJS 模块不支持 export 语法。所以你可能需要为此使用 CommonJS 导出语法。

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 };

或者如果你真的想使用 export 语法,你可以使用 ES6 模块,如下所示: .