I am currently trying to import and use nanoid in my (Firebase) nodejs project. I installed it with
npm i nanoid
and I tried to import it with
import { nanoid } from 'nanoid'
and
import { nanoid } from '../node_modules/nanoid/nanoid.js'
Everything I tried failed. I am a complete beginner with nodejs and js itself, but no Website or Documentation helped me fixing the problem. I just want an unique id :(
heres my index.html (reduced to minimum:
<!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.
– Pyrognosticstype="module"
to your script tag. Check if it works? – Halfpinttype="module
the error message is:Cannot use import statement outside a module
– Pyrognosticspackage.json
file look like? – Jittery{ "dependencies": { "nanoid": "^3.2.0" } }
I am using node version v16.13.2. – Pyrognosticsrequire
function doesn't exist in the browser – Oogonium