Can't use import syntax Error while importing uuid - node.js

I am trying to import uuid like
import { v1 as uuidv1 } from 'uuid';
But I get a syntax error
Cannot use import statement outside a module

because nodejs doesn't support BABEL!
try:
const { v1 as uuid} = require('uuid');
If you need BABEL you must add it and enable it: look this:
https://dev.to/ganeshmani/configuring-babel-for-node-js-express-server-35hp
or google it.

Related

Firebase import cannot be done outside modules

I'm going crazy over "writing stuff exactly what the documentation wants but came back with different outcome."
I'm trying to import firebase to my express.js API so the app can GET directly from the database.
// Initialize Firebase
const functions = require('firebase-functions');
import { initializeApp } from 'firebase/app';
const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);
... //some other code
I've looked for the solution at
https://firebase.google.com/docs/web/setup
and
SyntaxError: Cannot use import statement outside a module Firebase Functions
but seems like the problem is pretty different. The outcome still 'SyntaxError: Cannot use import statement outside a module.'
Can anyone explain what happened? Did I miss something?
The latest Firebase version is now using the ES6/modular syntax which in return will result in SyntaxError: Cannot use import statement outside a module. You have to add the "type": "module" on your package.json. This enables ES6 modules discovery. Also, When using "module" you also have to change all your require syntax to import syntax. Here's your sample code for reference:
import * as functions from "firebase-functions";
import { initializeApp } from 'firebase/app';
const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);
package.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"type": "module",
...
}
For more information, you may check out this documentations:
ECMAScript Modules
Upgrade from version 8 to the modular Web SDK

How import firebase cloud function config dynamically in NodeJS 14 with `type: module`? [ closed ]

I would like to achieve importing cloud function config dynamically but it won't working as expected.
import config from "firebase-functions";
It works ✔️
const {config} = await import('firebase-functions');
It won't work ❌
also tried the following but won't works.
import * as functions from "firebase-functions"; // function is already using as static import
or
const functions = await import('firebase-functions');
functions.config ❌// => undefined
functions.auth ✔️
functions.firestore ✔️
//and analytics, app, auth, database, firestore, handler, https, pubsub, remoteConfig, storage, testLab, logger ✔️
Am not getting a way to dynamically import the config import along with function.
Oh finaly it works.
const { default: functions} = await import('firebase-functions');
and
import functions from "firebase-functions";
giving functions.config

Initializing multiple connections to firebase are failing in Nodejs for firebase-admin NPM

I have the following code trying to connect to Firebase Realtime Database in NodeJS using the latest firebase-admin package (10.x):
import { database } from 'firebase-admin';
import { applicationDefault, initializeApp } from 'firebase-admin/app';
const app = initializeApp({
credential: applicationDefault(),
databaseURL: 'https://DATABASE_NAME.firebaseio.com'
}, 'xx');
const db = database(app);
The issue is that the database(app) call fails with following error:
TypeError: _this.ensureApp(...).database is not a function
I believe that my code is correct since I need the ability to connect to multiple databases...
Anybody knows what am I doing wrong?
The following import is incorrect:
import { database } from 'firebase-admin';
The correct modular import is:
import { getDatabase } from 'firebase-admin/database';
getDatabase()
Or, if using the old namespaced API:
import * as admin from 'firebase-admin'
admin.database()

How do I fix "unexpected token 'export'" error when I try to use uuid package?

I'm working in a vue.js application with node.js in the backend. We've installed the npm package uuid in the backend but it's not working.
Our package.json file in the backend contains this:
"uuid": "^8.2.0",
In our controller, in the constructor, we have this:
this.uuid = require("uuid");
And we use it like this:
const id = this.uuid.v1();
The problem is, when we call the endpoint on the backend, we get this error:
C:\...\backend\node_modules\uuid\dist\esm-browser\index.js:1
export { default as v1 } from './v1.js';
^^^^^^
SyntaxError: Unexpected token 'export'
Googling this issue, the only solutions I've found involve importing v1 specifically, such as this:
import {v1 as uuid} from "uuid";
But since we're using node.js on the backend, we get this error:
SyntaxError: Cannot use import statement outside a module
How can I solve this problem?
I have been there. Used this approach:
const uuid = require('uuid');
const newUuid = uuid.v4();
You may use .v1() instead of .v4().
You should first export your functions (that you need in other modules) in v1.js like below-
function xyz() {
}
module.exports = xyz;
You can then use this in the respective files as-
const xyz = require(‘../pathname’);

Import external class in nodejs

i´v got a class which i want to export and import it in another file.
//db.js
class sqlConn {
constru....
}
modules.exports = sqlConn;
I tried to import it, but that doenst worked for me...
//main.html
var sqlConn = require('path_to_file');
var obj = new sqlConn(...);
That gives me following error:
Uncaught Error: Cannot find module 'path_to_file'
can someone help me?
Edit on some answers
I´m using electron with node.js
and my class is laying on an html server.
Also i´m trying to import all in an index.html to sahre an electron.exe which imports all with ajax.
Pass the good path on the required function :
var sqlConn = require('./db'); // or other path if the file db.js isn't on the same folder
but I see main.html, you try to use a node.js code into html?
If you're using ES6/ES2015 you can import the class:
db.js:
class sqlConn {
...
}
export { sqlConn as default }
main.html:
import sqlConn from './path_to_file');
var obj = new sqlConn(...);
Already tried. Gives me error: Uncaught SyntaxError: Unexpected token import
Did you try the require syntaxt?
const db = require('./db');

Resources