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.
Related
I have several JavaScript files that requires several modules that are managed by NPM. I am trying to bundle these files together with Browserify. However, I am getting the following error:
Cannot find module 'fairychess-web' from '...App.js'
(the full path has been elided).
This module is required in the following area of App.js:
exports.__esModule = true;
var jsx_runtime_1 = require("react/jsx-runtime");
var react_1 = require("react");
require("./App.css");
var fairychess_web_1 = __importStar(require("fairychess-web"));
var chess_lib_1 = require("./chess_lib");
var net_lib_1 = require("./net_lib");
My dependencies are in my package.json file as follows:
"dependencies": {
"#types/node": "^18.11.11",
"#types/react": "^18.0.26",
"fairychess-web": "file:../fairychess-web/pkg",
"http": "^0.0.1-security",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"ws": "^8.11.0"
}
As you can see, the offending fairychess-web module is defined locally rather than on the NPM registry. I think this is why Browserify is having trouble finding the module, because I'm not receiving any error messages about the previous imports of libraries downloaded from NPM, but I have no idea as to how to solve the problem.
A few notes that may be relevant
App.js and the rest of my files, like chess_lib and net_lib, were created though tsc run on TypeScript files.
The fairychess-web module is a Rust package that was compiled to WASM with wasm-bindgen. This created a TypeScript wrapper file through which I am interfacing the Rust code and my Typescript code.
This code was originally built on create-react-app. At that time there were no problems importing the local module, this problem only arose when I decided to manually build the project.
I've been trying to silence the warning:
It looks like you're using the development build of the Firebase JS
SDK. When deploying Firebase apps to production, it is advisable to
only import the individual SDK components you intend to use.
For the CDN builds, these are available in the following manner
(replace with the name of a component - i.e. auth, database,
etc):
https://www.gstatic.com/firebasejs/5.0.0/firebase-.js
Per the instructions here Warning: It looks like you're using the development build of the Firebase JS SDK, I've updated all of my imports to match this format but the warning is not silenced:
import firebase from "firebase/app";
import "firebase/firestore";
I've searched my code for all instances of from "firebase" and cannot find any that haven't been updated to from "firebase/app", so am a bit stumped. Are there any other imports that could be causing this? Here are my firebase packages in package.json:
"dependencies": {
"firebase": "^8.2.5",
"firebase-functions": "^3.20.0",
"firebase-tools": "^10.2.1",
"firebaseui": "^5.0.0",
"react": "^18.1.0",
"react-firebaseui": "^5.0.0",
"react-router-dom": "^5.2.0",
"react-scripts": "^5.0.1",
"typescript": "^4.7.3",
},
The firebase-admin and firebase-functions modules are not for use in frontend web apps. They are for backend nodejs code only. You should remove them from this project.
I'm migrating one of my CLI tools over to a global installation so that it can be installed globally and used anywhere on my system. Most of my src files include require('dotenv').config() at the top of them, but for some reason the env is undefined now that it's installed globally.
What am I missing?
My package JSON looks like:
{
"name": "scoop",
"version": "1.9.0",
"main": "bin/scoop.js",
"dependencies": {
"axios": "0.20.0",
"cli-spinners": "2.4.0",
"commander": "6.1.0",
"dotenv": "8.2.0",
"log-symbols": "4.0.0",
"ora": "5.1.0",
"readline": "1.3.0"
},
"bin": {
"scoop": "./bin/scoop.js"
}
}
bin/scoop.js then contains the following at the top:
#!/usr/bin/env node
require('dotenv').config();
const forms = require('../src/utils/LocateForms');
...
And I'm loading in additional JS src files that are exported, I've got an .env in my project my my custom variables just come up as undefined now?
I think it's expected behaviour.
The default path value for dotenv is Default: path.resolve(process.cwd(), '.env') according to the GitHub readme.
Now process.cwd changes depending upon from where you execute the executable file.
For example if u start node /a/b/c.js then the cwd would be /a/b and if you start it from node /a/b/d/c.js the cwd would be /a/b/d.
So, in order to get the the .env file that you want either you have to store the .env file in a common area like ~/.yourenv like most other executables do(think .bashrc).
Or, you can try to get the installation folder and get the .env file using an absolute path.
For example you can try to import npm and get the prefix to find out the installation folder.
var npm = require("npm")
npm.load({}, function (er) {
if (er) return handleError(er)
console.log(npm.get('prefix'));
})
Or, you can use some package like https://www.npmjs.com/package/get-installed-path
Learning NodeJs here. Problem is when I tried to search for answers, I am not finding what I am looking for. Probably because this is too basic or non-issue.
I am working on nodejs with angular2. So naturally, I have things like:
import { stuff } from 'some_module'
But I am trying to work with a package that has usage example of:
var stuff = require('some_module')
Obviously, my code didn't work when I use import etc. else I wouldn't be posting here. Is it because I am doing something wrong? Or am I out of luck such that this particular module doesn't work with import? Can someone shed some light on how to write proper import statements when I see usage sample of require('some_stuff'), so I can use other modules I download from npm?
thanks in advance.
EDIT:
So I tried npm install requirejs --save. Then I wrote the require statement above. But I am getting a 404 on the package...
You can use import but you have to run your app with babel.
you have to add this line to your package.json file
"scripts": {
"start": "NODE_ENV=production node_modules/.bin/babel-node --presets 'es2015' src/server.js",
"build": "NODE_ENV=production node_modules/.bin/webpack -p"
},
"dependencies": {
"babel-cli": "^6.11.4",
"babel-core": "^6.13.2",
"babel-loader": "^6.2.5",
"babel-preset-es2015": "^6.13.2"
},
"devDependencies": {
"http-server": "^0.9.0",
"webpack": "^1.13.2",
"webpack-dev-server": "^1.14.1"
}
src/server.js file is your main file location
and then run file with following command
npm run start
when you use import { stuff } from 'module';
then you can directly use stuff() in your program.
but when you use var stuff = require('module');
then you need to do stuff.stuff() in your program.
It's interesting that the original syntax
var stuff = require('some_module')
is not working for you. If your app was scaffolded from Angular CLI then it should support both imports and require statements out of the box.
for example, I'm using MSR in an Angular 2 component like this:
var MediaStreamRecorder = require('msr');
I'm creating an app using react native but some imports raise the error from the title: "Unknown named module". This happens with two packages, react-native-material-design and react-native-db-models, so I suspect the problem isn't on the modules but in my setup.
I tried linking the packages with react-native link and repackaging the app using react-native run-android, but none of these solved this issue. I've looked up examples of imports on github, and even copying working code raises the same error.
I tried installing the modules using either yarn and npm. I tried with relative paths like ../../react-native-db-models. Didn't work!
This is how I import the modules:
import RNDBModel from 'react-native-db-models';
import { List, Button, Toolbar, } from 'react-native-material-design';
These are my project dependencies:
"dependencies": {
"react": "15.4.1",
"react-native": "0.39.2",
"react-native-db-models": "^0.1.3",
"react-native-material-design": "^0.3.7",
"react-native-md-textinput": "^2.0.4",
"react-native-vector-icons": "0.8.5",
"react-redux": "^5.0.1",
"redux": "^3.6.0",
"redux-logger": "^2.7.4"
},
If React/React Native is incompatible with these modules, how do I figure out which version should I use? Maybe the error has nothing to do with imports and has something to do with my project?
The package server started by react-native start seems to have a cache of the node modules. Stopping and restarting the server solved the issue.
Always restart your react server after installing modules!
A quick check on the package's repository shows 'List' module is no longer available. Here is a link to why it was removed.
So, you will have to remove 'List' module from your import:
import { Button, Toolbar, } from 'react-native-material-design';