繁体   English   中英

JS:未捕获的语法错误:无法在模块外使用导入语句

[英]JS : Uncaught SyntaxError: Cannot use import statement outside a module

我正在学习 firebase。我使用的是 Kubuntu 21.04。 我使用以下命令安装了 nodejs 和 npm:
sudo apt install nodejs npm我用 index.html 文件/home/username/project/index.html创建了一个项目文件夹。
当我在终端的项目文件夹中时,我还使用npm install firebasefirebase
我按照https://firebase.google.com/docs/web/setup中给出的步骤 2进行操作。
我的index.html内容-

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>User Auth</title>
</head>
<body>
  <h1>User Auth</h1>

<script>
  // Import the functions you need from the SDKs you need

  import { initializeApp } from "firebase/app";
  import { getAnalytics } from "firebase/analytics";


  // TODO: Add SDKs for Firebase products that you want to use
  // https://firebase.google.com/docs/web/setup#available-libraries

  // Your web app's Firebase configuration
  // For Firebase JS SDK v7.20.0 and later, measurementId is optional
  const firebaseConfig = {
    apiKey: "AIzaSyC5zv8b1wfc0ykmpULeFI6tJa6Grk",
    authDomain: "user-auth-75223.firebaseapp.com",
    projectId: "user-auth-78563",
    storageBucket: "user-auth-78563.appspot.com",
    messagingSenderId: "482788408547",
    appId: "1:482788408547:web:89e3ecdc24f3454576c00aa",
    measurementId: "G-VK5D45CCJ2"
  };

  // Initialize Firebase
  const app = initializeApp(firebaseConfig);
  const analytics = getAnalytics(app);
</script>
</body>
</html>

但是当我在 chrome 中运行 html 文件时。 它在 chrome 的控制台中给出错误。

未捕获的语法错误:无法在模块外使用导入语句

可能是因为你没有在package.json中添加"type":"module"

我通过添加这一行解决了这个问题

来源:( 节点:9374)警告:要加载 ES 模块,请设置“类型”:“模块”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM