Dependance Sequelize - node.js

I try to use Sequelize .. And I have problems :( . I don't know if I have a conflict with an other npm package ..
Like the tuto, I did :
npm install sequelize --save
npm install mysql2 -- save
In my react app, in "sequelizeYes" folder, I did :
import * as Sequelize from 'sequelize'
const seq = new Sequelize('galadat', 'root', '')
seq
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
export default seq
In app component, i call the file like this :
import('../sequelizeYes')
You can see on the picture differents errors in the console..
Do you have an idea ?

Did you require the module?
var Sequelize = require('sequelize');
var fs = require('fs');
If you shared the lines of code that the errors indicate it might help as well. It looks like several different files are having problems.

Sequelize is a nodejs module intended to be used in your backend. (server-side) It will not work on client-side (with libraries such as React) as it has dependencies to native NodeJS modules as based on your description that is what you're trying to do. If you're working on a web application and use for example express have a look here https://github.com/sequelize/express-example

As #Eric suggests, you should require sequelize on top of your code. So this should work:
var Sequelize = require('sequelize');
If the error persists, you should delete folder "node_modules" and reinstall dependencies using the command "npm install". Also you should make sure all errors are thrown because of sequelize import.
If the error still persists, you should make sure you try to manipulate the models in server-side code, as #archansel and #frenzzy suggest here: https://github.com/kriasoft/react-starter-kit/issues/976
The reason is referenced above by #razakj answer

Related

Module not found: Error: Can't resolve 'fs' when trying to use a NodeJS library

I initiated a basic ReactJS app using npx create-react-app, then I ejected using npm run eject. Now when I am trying to import the Casual library by import casual from 'casual';, I get the following error:
Compiled with problems:
ERROR in ./node_modules/casual/src/casual.js 3:13-37
Module not found: Error: Can't resolve 'fs'
in '/home/me/project/node_modules/casual/src'
And the code around line number 3 in casual.js looks like this:
var helpers = require('./helpers');
var exists = require('fs').existsSync;
var safe_require = function(filename) {
if (exists(filename + '.js')) {
return require(filename);
}
return {};
};
...
I found answers to similar questions. Those were mainly Node or Angular related. I also tried answers suggesting some changes in webpack config, but no luck.
The reason is Casual doesn't work on the front end. It runs on Node.js only.
You need to install maybe a new package to make things work.
Fs is unavailable on the browser so it won't work. Instead, you should use casual-browserify, it will work on browsers.

Cannot find module 'mongodb-client-encryption'

I have a script.js file, there requires mongodb module, and when I want to run it with browser, it say that require is not defined.So, I installed browserify using npm, but when i want to use it browserify script.js -o all.js -d, I get an error:
Error: Cannot find module 'mongodb-client-encryption' from
'C:\Users\User\Desktop\browserify\node_modules\mongodb\lib\operations'
As per comment, you need to install mongodb-client-encryption module. This is a Node.JS wrapper for libmongocrypt.
npm install mongodb-client-encryption
If you're seeing an error message related to this module, i.e.:
TypeError: encryption.createDataKey is not a function
Check the module import line, it should be:
const mongodb = require('mongodb');
const { ClientEncryption } = require('mongodb-client-encryption');
const { MongoClient } = require('mongodb');
See also:
github.com/mongodb-labs/field-level-encryption-sandbox
MongoDB Node.JS: Client Side Encryption examples

firebase.database() is not a function on local node

I'm running a simple node app in my local machine and I need to connect to firebase realtime database.
I installed firebase via npm:
npm install firebase --save
Then I initialize the app:
var firebase = require("firebase");
var config = {
apiKey: "api-key",
authDomain: "my-app-database.firebaseapp.com",
databaseURL: "https://my-url.firebaseio.com",
storageBucket: "my-app-database.appspot.com",
};
firebase.initializeApp(config);
var myRef = firebase.database().ref("collection").on("value", (snap) => {
// do something with the data
});
Then I get the error that database is not a function. I check firebase.database and is undefined, also so are firebase.auth and firebase.storage.
I followed all the steps in the docs, but I don't see anything that could be causing this.
Goodness gracious... It was as simple as requiring the other packages in the file, like this:
// firebase
const firebase = require("firebase");
// get database, auth and storage
require("firebase/auth");
require("firebase/storage");
require("firebase/database");
Nowhere in the docs or reference says that. I thought about going back a version, perhaps 4.12.x, so I went to the npm page to see the previous versions and install one of those and try, when I found this:
https://www.npmjs.com/package/firebase#include-only-the-features-you-need
Just scroll down where they mention using npm packages or Typescript and you'll find the answer.
Shout out to the Firebase team, this information can't be just in the npm page and not in the docs, getting started guides or the github repo. Not a lot of people goes to the npm page of a package for information and I went there to check previous versions, so I kind of stumbled upon it.

Build production on SystemJS with systemjs-builder

I have my project that use SystemJS (i use a template that comes on this way).
I need to build the final project to production env. I read that i can use systemjs-builder for this. I read the official page: https://github.com/systemjs/builder.
Sorry, i'm so new on Angular 2! I don't understand how to start.
For example, this part:
Basic Use
Ensure that the transpiler is installed separately (npm install babel-core here).
var path = require("path");
var Builder = require('systemjs-builder');
// optional constructor options
// sets the baseURL and loads the configuration file
var builder = new Builder('path/to/baseURL', 'path/to/system/config-file.js');
builder
.bundle('local/module.js', 'outfile.js')
.then(function() {
console.log('Build complete');
})
.catch(function(err) {
console.log('Build error');
console.log(err);
});
What i should to do here? I must to install this package called babel? And after this? I need to create a file with that code? Where? what name must have this file? How i can execute?
Thanks for your help. Please, be patient with a new angular developer!

How to connect Mongo DB in a webpack dev server

I would like to connect to Mongo DB using a webpack dev server. While the connection using the node mongodb driver and configuring in server.js is direct and straight forward, I am thinking of a way to do the same using webpack dev server in development (mainly for the hot loading advantage).
I understand that there is a way of achieving the same using a webpack middleware, but is there another easier and better way of doing it.
webpack dev-server is generally a simple Express or similar node.js server. It's essentially exactly the same as writing is on an express.js server.
npm install mongoose mongodb --save-dev
const mongoose = require('mongoose'); // Replace with import as desired
const mongoConnectString = 'mongodb://localhost/database-name-here';
mongoose.connect(mongoConnectString, (err) => {
if (err) {
console.log('Err, could not connect to the database.');
}
});
Replace the mongoConnectString as needed for developing or using databases that aren't local to your machine.

Resources