3

I'm getting this error when I copy-paste the underscore.js to my console. Error screenshot "Uncaught SyntaxError; Unexpected token 'export'"

I tried on both Chrome Version 100.0.4896.127 and Opera LVL3 (core: 85.0.4341.72) if that helps.

2
  • 1
    Welcome to Stack Overflow! For upvotes, add the error text to your question (i.e., copy it from the console or type it yourself) and remove your apology as it is not needed! :-) Commented Apr 24, 2022 at 12:27
  • Please provide enough code so others can better understand or reproduce the problem. Commented Apr 25, 2022 at 11:20

1 Answer 1

4

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/[email protected]/underscore-umd-min.js"></script>

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.