Eric Funk
Eric Funk

Reputation: 49

Error while trying to import a function from another js file

I am trying to import a function called secretPhraseToPublicKey from index.js.

import {secretPhraseToPublicKey} from "./index.js";

It is giving me this error: "Uncaught SyntaxError: Unexpected token {"

I have reviewed information about import and it seems that the way I have written the import should be valid. What else would cause me to have this error?

Upvotes: 0

Views: 400

Answers (1)

connexo
connexo

Reputation: 56720

Three things for that to work:

  1. index.js must have an export named secretPhraseToPublicKey.
  2. The <script> that does the import needs to be included in the HTML with type="module".
  3. You need to use a browser that supports ES6 imports (e.g. Firefox, Chrome).

Either 2. or 3. is the reason for the error message you are getting.

Upvotes: 1

Related Questions