Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

320
Views
Node.js Error - 'Cannot use import outside of a module'

I am working on a React Project, and I have...

Firebase.js

import firebase from 'firebase/app'
import 'firebase/database'
import 'firebase/auth'

const firebaseConfig = {
    apiKey: process.env.FIREBASE_API_KEY,
    authDomain: process.env.FIREBASE_AUTH_DOMAIN,
    projectId: process.env.FIREBASE_PROJECT_ID,
    databaseURL: process.env.FIREBASE_DATABASE_URL,
    storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
    messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
    appId: process.env.APP_ID,
    measurementId: process.env.MEASUREMENT_ID
}

firebase.initializeApp(firebaseConfig)

export const googleAuthProvider = new firebase.auth.GoogleAuthProvider()
export const database = firebase.database()

Server.js

const express = require('express')
const path = require('path')
const { database } = require('../firebase/firebase')

const app = express()
const port = process.env.PORT || 3000
const publicDir = path.join(__dirname, '../public')

app.use(express.static(publicDir))
app.use(express.urlencoded({ extended: true }))

app.post('/someRoute', (req, res) => {
    // Wanna send data to database using 'database'
})

app.listen(port, () => {
    console.log('Server is up on PORT...', port)
})

Now, I just wanna save Data to Database on '/someRoute' but it is giving me an error : 'cannot use import outside module'

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

import {} from XXX is not supported initially from NodeJS. So you should either use

  • const XXX = require()-syntax or
  • type: module in your package.json.

The same is for exports: in Node, you can use module.exports = YOUR_FUNCTION_OR_CLASS.

over 4 years ago · Santiago Trujillo Report

0

Try adding "type" : "module" to your package.json. You can also change your file extension from .js to .mjs

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!