module not found can't resolve 're-base' - rebase

I am importing Rebase from re-base, but its throwing an error:
moodule not found can't resolve 're-base' in '/home/salisu/Desktop/React Projects/catch-of-the-day/src' "
Code:
import Rebase from 're-base';
const base = Rebase.createClass({
apiKey: "AIzaSyDciFjReP_AmFD31eSPnBhRbYkaJ7L2Mqc",
authDomain: "catch-of-the-day-salisu-a6502.firebaseapp.com",
databaseURL: "https://catch-of-the-day-salisu-a6502.firebaseio.com"
});
export default base;

install the re-base and also peer up it with the dependencies
Try this command my issue is solved.
npm install re-base --legacy-peer-deps

Related

User Authentication throwing error...Module not found

I am relatively new to React and Firebase. Was working on a netflix clone and during firebase configuration for user authentication, it throws an error. I have installed firebase using yarn add firebase but it still throws the error. here is the firebase.js file.
import firebase from "firebase";
const firebaseConfig = {
apiKey: "AIzaSyBeUkRFXLx3Jwvlevk6GSwlSJ-8pnCQeBM",
authDomain: "noctoc404.firebaseapp.com",
projectId: "noctoc404",
storageBucket: "noctoc404.appspot.com",
messagingSenderId: "818097445726",
appId: "1:818097445726:web:c22585debcda59f4074dcf",
};
const firebaseApp = firebase.initializeApp(firebaseConfig);
const db = firebaseApp.firestore();
const auth = firebase.auth();
export { auth };
export default db;
and it is throwing an error here saying Module not found
Compiled with problems:X
ERROR in ./src/firebase.js 3:0-32
Module not found: Error: Package path . is not exported from package /home/insidtrifl/Documents/Web Dev/Projects/noctoc/node_modules/firebase (see exports field in /home/insidtrifl/Documents/Web Dev/Projects/noctoc/node_modules/firebase/package.json) Did you mean './firebase'? Requests that should resolve in the current directory need to start with './'. Requests that start with a name are treated as module requests and resolve within module directories (node_modules, /home/insidtrifl/Documents/Web Dev/Projects/noctoc/node_modules). If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.
any help would be awesome. Thank You.

Reactjs does not read .env files

Hello I create a web app with firebase
my .env files does not work
heres my code in firebase.js
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: process.env.AUTH_KEY,
authDomain: process.env.AUTH_DOMAIN,
projectId: process.env.PROJECT_ID,
storageBucket: process.env.STORAGE_BUCKET,
messagingSenderId: process.env.MESSENGING_SENDER_ID,
appId: process.env.APPLE_ID
};
heres my .ENV code
API_KEY=Axxxxxxxxxxxxxxxxxxxxxxxxxx
AUTH_DOMAIN=xxxxxxxxxxxx
PROJECT_ID=xxxxxxxxxxx
STORAGE_BUCKET=mxxxxxxxxxx
MESSENGING_Sxxxxxxxxxxxxxxxxxxxxx
APPLE_ID=1:3xxxxxxxxxxxxxxxxxxx
heres where located the my .env
can guys explain why my .env does not read in my react? i search any question here in stack overflow but the same problem i got was in laravel i dont use laravel i use React JS
In react app all your envs should start with REACT_APP_
More Info
I had the same issue in a react native project - I fixed it with the installation of react-native-dotenv
https://www.npmjs.com/package/react-native-dotenv

How to use process.env in a React service worker

I am trying to set-up a Firebase-messaging-sw.js file (for web push notifications). I am wondering if there is a way to avoid exposing my Firebase config data to the public as much as possible - though it might be revealed anyways? (I'm not too sure about the nuances)
I've tried following: How can I customize my Service Worker based on environment variables? But the answer's swEnvbuild doesn't seem to be running, as the swenv.js file is not found. I suspect it might need to be set-up differently in React?
(first question, please feel free to provide constructive criticisms of my question)
I recently had to do this with a CRA app, it's not easy to find information on it so I figured I should share my solution. Assuming you've already changed serviceWorker.unregister() to serviceWorker.register() in ./src/index.js, and have a .env file with your variables set in the root of your project, then you can update ./src/serviceWorker.js to include your process.env variables as a query string.
In the register function in serviceWorker.js, update const swUrl as shown below, notice the const firebaseConfig w/process.env, declared before swUrl..
./src/serviceWorker.js:
// Convert environment variables to URL `search` parameters
const firebaseConfig = new URLSearchParams({
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.REACT_APP_FIREBASE_DATABASE_URL,
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.REACT_APP_FIREBASE_APP_ID,
measurementId: process.env.REACT_APP_FIREBASE_MEASUREMENT_ID
}).toString();
// Service worker URL w/config variables
const swUrl = `${process.env.PUBLIC_URL}/firebase-messaging-sw.js?${firebaseConfig}`;
then in ./public/firebase-messaging-sw.js (create it if it doesn't exist), you can do something like the following..
./public/firebase-messaging-sw.js
importScripts('https://www.gstatic.com/firebasejs/8.0.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.0.2/firebase-messaging.js');
// Set Firebase configuration, once available
self.addEventListener('fetch', () => {
const urlParams = new URLSearchParams(location.search);
self.firebaseConfig = Object.fromEntries(urlParams);
});
// "Default" Firebase configuration (prevents errors)
const defaultConfig = {
apiKey: true,
projectId: true,
messagingSenderId: true,
appId: true,
};
// Initialize Firebase app
firebase.initializeApp(self.firebaseConfig || defaultConfig);
const messaging = firebase.messaging();
// Configure message handler (assumes backend is set up)
messaging.onBackgroundMessage((payload) => {
const { icon, body, title } = payload.data;
self.registration.showNotification(title, { body, icon });
});
If there's a more ideal solution, would love to hear about it, but this configuration worked for me.
I found this article which uses cra-append-sw to append the env vars. Then I created two pre scripts in my package.json. When I run npm start the prestart script runs creating a [root folder]/public/firebase-messaging-sw.js file that contains the env vars (after being processed by webpack).
Implementation
I created a [root folder]/firebase-messaging-sw.js. This file will be processed by webpack replacing the values of the env vars.
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here. Other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/8.1.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.1.1/firebase-messaging.js');
// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
firebase.initializeApp({
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.REACT_APP_FIREBASE_DATABASE_URL,
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.REACT_APP_FIREBASE_APP_ID,
measurementId: process.env.REACT_APP_FIREBASE_MEASUREMENT_ID,
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
messaging.onBackgroundMessage(function (payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/logo.png'
};
self.registration.showNotification(notificationTitle, notificationOptions);
});
then I have [root folder]/.env.dev and [root folder]/.env.prod
REACT_APP_FIREBASE_API_KEY=A...
REACT_APP_FIREBASE_AUTH_DOMAIN=d...
REACT_APP_FIREBASE_DATABASE_URL=h...
REACT_APP_FIREBASE_PROJECT_ID=d...
REACT_APP_FIREBASE_STORAGE_BUCKET=d...
REACT_APP_FIREBASE_MESSAGING_SENDER_ID=7...
REACT_APP_FIREBASE_APP_ID=1...
REACT_APP_FIREBASE_MEASUREMENT_ID=G...
And finally, i added 2 pre scripts in my package.json
...
"scripts": {
"prestart": "cra-append-sw --mode dev --env ./.env.dev ./firebase-messaging-sw.js",
"prebuild": "cra-append-sw --mode build --env ./.env.prod ./firebase-messaging-sw.js",
...
I had real troubles with this one myself. The service worker gets involved in the stack way before your environment gets bootstrapped so it makes sense that it doesn't have access to your .Env variables.
My Solution
I built an npm module that on build, using webpack, extracts your "safe" versioning variables from your .env file and puts them into a stand-alone JS file. You can then go ahead and import this file and use it in your service worker.
https://www.npmjs.com/package/vue-enverywhere
Disclaimer:
I know this is for vue, but its webpack, and it's not vue specific. Also, You might be better to just copy the code, and not use the module. This was more of a fun exercise for myself :)
In index.js file (or wherever you want to register service worker) :
if ("serviceWorker" in navigator) {
console.log("Registration started");
const firebaseConfig = encodeURIComponent(
JSON.stringify({
apiKey: process.env.FCM_API_KEY,
projectId: process.env.FCM_PROJECT_ID,
messagingSenderId: process.env.FCM_SENDER_ID,
appId: process.env.FCM_APP_ID,
})
);
navigator.serviceWorker
.register(
`../../../firebase-messaging-sw.js?firebaseConfig=${firebaseConfig}`
)
.then(function (registration) {
console.log("Registration successful, scope is:", registration.scope);
})
.catch(function (err) {
console.log("Service worker registration failed, error:", err);
});
In Service Worker, firebase-messaging-sw.js :
importScripts("https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js");
importScripts(
"https://www.gstatic.com/firebasejs/8.10.0/firebase-messaging.js"
);
firebase.initializeApp(
JSON.parse(new URL(location).searchParams.get("firebaseConfig"))
);
firebase.messaging();
This is enough for receiving Background Push Notifications on sending notification-type message.
this solution will work,
Just remember to edit the path to your Service-worer file (while registering in index.js or so), according to your project,
Or It will give "Mime Error".

Error When using re-base with react and firebase

I've created an application that gets all the data for various tables from static objects stored in their own files. I want to take these and move them onto Firebase so I can save my various data states. I'm really struggling to find a way to connect re-base and firebase together after multiple installs.
I get the error: TypeError: _base__WEBPACK_IMPORTED_MODULE_4__.base is undefined when I load my Loads module.
Here is my code for one of my components, note that LOADS is not my app component, could this be causing issues?
import {base} from '../../base';
class Loads extends Component{
constructor(props){
super(props);
this.state = {
tableData: null,
status: null
}
}
componentWillMount(){
this.loadDataRef = base.syncState('loadData', {
context: this,
state: 'loadData'
})
}
componentWillUnmount(){
base.removeBinding(this.loadDataRef);
}
Here is the code in base.js where I've imported firebase and rebase:
import Rebase from 're-base'
import firebase from 'firebase'
const config = {
apiKey: "this is a secret ;)",
authDomain: "yes",
databaseURL: "yes",
projectId: "yes",
storageBucket: "not sure",
messagingSenderId: "21",
appId: "yessir",
measurementId: "alright"
}
const app = firebase.initializeApp(config);
const base = Rebase.createClass(app.database());
export { base }
I've already done npm install --save re-base firebase, although I do get the following warnings:
npm WARN notsup Unsupported engine for firebase#7.14.3: wanted: {"node":"^8.13.0 || >=10.10.0"} (current: {"node":"8.10.0","npm":"6.14.4"})
npm WARN notsup Not compatible with your version of node/npm: firebase#7.14.3
npm WARN notsup Unsupported engine for #firebase/firestore#1.14.3: wanted: {"node":"^8.13.0 || >=10.10.0"} (current: {"node":"8.10.0","npm":"6.14.4"})
npm WARN notsup Not compatible with your version of node/npm: #firebase/firestore#1.14.3
npm WARN notsup Unsupported engine for #grpc/grpc-js#0.8.1: wanted: {"node":"^8.13.0 || >=10.10.0"} (current: {"node":"8.10.0","npm":"6.14.4"})
npm WARN notsup Not compatible with your version of node/npm: #grpc/grpc-js#0.8.1
Any help would be appreciated, I've been stuck here for a a day now looking at various ways of doing it but I can't seem to figure anything out.

Error in aws-amplify style.css while running the npm test

Had issue while running the npm test
C:\projects\Test\node_modules\#aws-amplify\ui\dist\style.css:13:root {^
error:
SyntaxError: Unexpected token :
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (node_modules/aws-amplify-react/src/Amplify-UI/Amplify-UI-Components-React.jsx:5:1)
here are my import statements
import Amplify from 'aws-amplify';
import { AuthPiece } from 'aws-amplify-react';
import { Auth, Logger, JS } from 'aws-amplify';
If I comment out the style.css it works without any errors.
But how can I remove this issue so that it doesnt cause issue to others when I commit code.
Had checked all the existing answers provided in different forums.But that doesnt work for me.
I ran into this problem as well, and this thread helped me resolve it. In my case, I needed to install identity-obj-proxy package and map it to the jest config:
npm install --save-dev identity-obj-proxy
package.json
"jest": {
"moduleNameMapper": {
"\\.(css|less)$": "identity-obj-proxy"
}
}
I hope this helps!

Resources