【发布时间】:2022-02-10 20:43:35
【问题描述】:
我目前正尝试在我的 (Firebase) nodejs 项目中导入和使用 nanoid。我安装了它
npm i nanoid
我尝试用
导入它import { nanoid } from 'nanoid'
和
import { nanoid } from '../node_modules/nanoid/nanoid.js'
我尝试的一切都失败了。我是 nodejs 和 js 本身的完整初学者,但没有网站或文档帮助我解决问题。我只想要一个唯一的 id :(
这是我的 index.html(减少到最低限度:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Firebase Hosting</title>
!!!Here are Firebase imports!!!
<script defer src="/__/firebase/init.js?useEmulator=true"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="registerContainer">
<div class="registerContent">
<h1 id="title">Sign up</h1>
<iframe name="hiddenFrame" width="0" height="0" border="0" style="display: none;"></iframe>
<form id="form" onsubmit="return false">
<!-- Birth date Input (required) -->
<div class="input_field">
<input id="date" onfocus="(this.type = 'date')" class="text_input" type="text" name="birthdate" placeholder="Geburtsdatum" required />
</div>
<!-- Checkbox 1 -->
<div class="input_field checkbox_option">
<input type="checkbox" id="cb1" class="checkbox">
<label for="cb1">I agree with terms and conditions</label>
</div>
<input class="button" id="registerUser" type="submit" value="Anmelden"/>
</form>
</div>
</div>
<script src="app.js"></script>
</body>
</html>
app.js:
const nanoid = require('nanoid');
document.addEventListener('DOMContentLoaded', event => {
const app = firebase.app();
const db = firebase.firestore();
const users = db.collection('users');
})
async function addUser() {
console.log('adding User');
const db = firebase.firestore();
const users = db.collection('users');
const data = {
email: document.getElementById("email").value,
test_active: false
};
code = nanoid(10).toString();
await users.doc(code).set(data);
}
【问题讨论】:
-
你得到什么错误?
-
Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec. -
将
type="module"添加到您的脚本标签中。检查它是否有效? -
我已经添加了。如果没有
type="module,错误消息是:Cannot use import statement outside a module -
您使用的是什么版本的 Node.js?你的
package.json文件是什么样的?
标签: javascript node.js npm nanoid