Home > Net >  Unexpected token error when using import command
Unexpected token error when using import command

Time:09-24

I am trying to setup firebase and firestore for the first time

I have been trying for a few days now to get this to work with no success. Having followed all the docs on installing Firebase and firestore SDK9 modular system I still get this error.

Code

import { initializeApp } from "firebase/app";

Error:

Unexpected token '{'. import call expects exactly one argument.

CodePudding user response:

Make sure that you've set module type in <script> tag if you are using import, for example:

<script type="module" src="index.js"> </script>

And if you are using nodejs you should use the latest version of nodejs and indicate module type in package.json:

{
  "name": "Testing app",
  "version": "1.0.0",
  "description": "Testing application",
  "main": "index.js",
  "type": "module"
}

CodePudding user response:

I have installed the latest Node Js again version v14.17.6 and installed Firebase again using npm.

I have added type: module to package.json.

First error I got was ...

TypeError: Module specifier, 'firebase/app' does not start with "/", "./", or "../".

I cleared this error by editing the index.js file and changing

import {initializeApp} from "firebase/app";

to

import {initializeApp} from "/node_modules/firebase/app";

In the index.html file I also added

    <script type="module" src="index.js"> </script>

I now get the error 'text/html' is not a valid JavaScript MIME type.

This is my package.json code

{
  "name": "firebasev9",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "keywords": [],
  "author": "IBP",
  "license": "ISC",
  "dependencies": {
    "firebase": "^9.0.2"
  }
}

I am trying to follow the setup instructions on https://firebase.google.com/docs/web/setup but they never mention either of these errors I keep getting.

  • Related