Cannot find module 'mongodb-client-encryption' - node.js

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

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.

How to resolve fs.existsSync is not a function

In NodeJS I have:
const fs = require('fs');
if (!fs.existsSync("some_path")) {
...
}
But I get the error:
TypeError: fs.existsSync is not a function
After doing some searching, I read that Webpack brings its own require which clobbers node.js's require, so when you require a node.js core module that webpack can't resolve to one of your files or dependencies, it throws.
(My stack trace includes __webpack_require__)
But how can I fix it?
I was facing the same Error like TypeError: fs.existsSync is not a function
So, I figured out that one extra line was added automatically which was creating this issue in import.
after removing this line from import
import { TRUE } from "node-sass";
the issue has been resolved.
I had the same error that you have. Your vscode might have added a new module to your js file. Remove that module and your app should work just fine.
You can allow webpack to use the Node's require and include fs etc. by targeting node in the config:
module.exports = {
entry: './src/main.js',
target: 'node',
output: {
path: path.join(__dirname, 'build'),
filename: 'backend.js'
}
}
As described here: https://webpack.js.org/concepts/targets/ and https://webpack.js.org/configuration/target/
I was working on an electron application, I wanted to send a message from node and get in on the react side, but I was having that same issue when requiring ipcRenderer from electron, I tried
import { ipcRenderer } from 'electron';
and
const { ipceRenderer } = require('electron') This leads to an error due to webpack transforming node's require to its own webpack_require. See more info here
What worked for me was to use
const {ipcRenderer} = window.require('electron'); on the react side/renderer side from electron
In my case, I forgot that I'd only imported the promises API, const fs = require("fs").promises, which doesn't have exist or existsSync functions in Node 17.4.0.
To use exist or existsSync, make sure you've imported fs using the sync API (const fs = require("fs")).
Note: I'm adding this answer as a possible solution for future visitors to a canonical thread for the error, not OP who appears to have required fs correctly.
It is nothing to worry about, check your code for something like import { types } from "node-sass";, it would have mistakenly and automatically imported without you know. Remove that line, and everything should work perfectly.
Even if it is not type, it is something from node-sass in your node_modules file, and you can't edit that file.
So look for and remove import { types } from "node-sass"
In my case VSCode added a arbitrary import from electron. After removing it my application worked.
import { Menu } from 'electron';
In my case, i needed to send a message from the node to react. I tried importing ipcRenderer from 'electron'; and const ipceRenderer = require('electron') This results in an error owing to webpack changing the node's require to its own webpack require. See more info here

Writing WebSocket client with TypeScript running both on browser and Node.JS

I am writing a typescript code that would run in a web-browser and would be tested with Node.JS.
My client code looks like below.
import * as WebSocket from 'ws';
export class SomeClient {
constructor(url) {
this.ws = new WebSocket(url);
}
send(data: any) {
this.ws.send(data);
}
}
I had no problem in writing a unit test code using mocha/chai.
However, trying to bundle this code, browserify includes all the 'ws' node module and the size of the output file is almost 100kb. If I remove the import 'ws' statement, the bundle file size shrinks less than 1kb. But, in this case, the Node.JS test complains with 'WebSocket is not defined' error.
I think, this is because WebSocket is natively supported in web browsers but not supported in Node.JS and the external 'ws' module is required to run properly.
How can I make a bundle with the minimum size for web browsers yet can use in Node.JS???
Try isomorphic-ws:
npm i isomorphic-ws -s
or universal-websocket-client:
npm install --save universal-websocket-client
I struggled with the same problem, best solution I could find was to use isomorphic-ws create a decs.d.ts in my typescript rootDir with the following content
declare module "isomorphic-ws";
and then use it inside typescript like that:
import { IsoWebSocket } from "isomorphic-ws";
var ws = new IsoWebSocket("wss://echo.websocket.org") as WebSocket;

Dependance Sequelize

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

Firebase reference is empty after requiring new version

The new documentation provides a straightforward method of initialising firebase, for example, this snipped comes directly from the README.md file:
Install the Firebase npm module:
$ npm init
$ npm install --save firebase
In your code, you can access Firebase using:
var firebase = require('firebase');
var app = firebase.intializeApp({ ... });
// ...
But in my installation this is not working, as in, firebase is empty ({} when console logged).
There's another section in the documentation called Include only the features you need, which provides the following snippet:
var firebase = require('firebase/app');
require('firebase/auth');
require('firebase/database');
var app = firebase.initializeApp({ ... });
// ...
This method works for me, so I'm wondering what could I be doing wrong in the first instance?
I'm using browserify, if that helps, but the docs state: The browser version is designed to be used with
a package bundler (e.g., Browserify,
Webpack).

Resources