Getting Unexpected Token Export

SyntaxError: Unexpected token 'export' while exporting function Js

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.

SyntaxError: Unexpected token 'export' Underscore.js

Unexpected token 'export' means that the engine encountered JavaScript's export keyword, which is only allowed when you do <script type=module>. This means that you probably copied the ESM bundle to your console (underscore-esm.js), which is not meant for this scenario.

For copy-pasting into the console, the UMD bundle (underscore-umd.js) is more suitable. This will give you a global _ variable that lets you access all Underscore functions.

When writing JavaScript projects "for serious", you will generally either do something like this:

import _, { times, debounce } from 'underscore';

if you are using an ESM platform (which may require setting up an import map or similar alias configuration in some cases), or something like this:

<script src="https://cdn.jsdelivr.net/npm/underscore@1.13.3/underscore-umd-min.js"></script>

if you are attaching your dependencies directly to an HTML page.

Unexpected token 'export'

What should I do to be able to export variable after DOMContentLoaded

You can't. export and import can only appear at the top level of a module.

What you can do instead is export the variable, and set its value in the handler:

export let k;
document.addEventListener('DOMContentLoaded', function(){ k = 12; })

If other modules import k, they get a read-only binding to it, meaning that if they use it before DOMContentLoaded, they'll get whatever initial value you provide (undefined in the above, because there's no initializer on the let k; declaration), but if they use it after the DOMContentLoaded, they'll see the value assigned by the handler.

That said, if your module provides information that is only available after a certain point, you might consider exporting a promise, like so:

let kPromiseResolve;
export const kPromise = new Promise(resolve => {
kPromiseResolve = resolve;
});
document.addEventListener('DOMContentLoaded', function(){ kPromiseResolve(12); })

Then modules using it would do:

import { kPromise } from "./your-module.js";
kPromise
.then(k => {
// ...use `k`...
})
.catch(error => {
// ...
});

or in environments supporting top-level await, if the importing module can't do anything useful without k, they could do:

import { kPromise } from "./your-module.js";
const k = await kPromise;
// ...use `k`...


Related Topics



Leave a reply



Submit