简体   繁体   中英

how to use require in javascript files

in my app.js that is located in the root folder, I mean Mywebsite/app.js i can access require variable

const express = require("express");
const admin = require("firebase-admin");

but in all other folders like public or static or views folders, I can not use require and also I can't import any library like this one

import admin from "firebase-admin"

and im getting this error for import

SyntaxError: Unexpected identifier 'admin'. import call expects exactly one argument.

or this for require

ReferenceError: Can't find variable: require

I'm new to web development especially when it comes to using an express app now all my problem is with require or importing a library in javascript files, is there any other ways to include or call models in nodejs rather than require

here is my dependencies

"dependencies": {
    "body-parser": "^1.19.0",
    "cookie-parser": "^1.4.5",
    "csurf": "^1.11.0",
    "ejs": "^3.1.6",
    "express": "^4.17.1",
    "firebase": "^8.6.3",
    "firebase-admin": "^9.9.0",
    "firebase-functions": "^3.14.1",
    "nodemon": "^2.0.7",
    "popups": "^1.1.3",
    "requirejs": "^2.3.6"
  },

have you tried using const { admin } = require("firebase-admin"); ?

const { admin } = require("firebase-admin"); should solve your issue but if it doesn't

Do check the following

  • Node Version
  • Add a key to package.josn , "main": "app.js"
  • run npm i
  • run node app.js in the root folder

import admin from "firebase-admin" won't work as you're not using ES6.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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