I have a nextjs app and I'm trying to use some of node crypto methods like hkdf, scrypt, generateKeyPairs... The problem is I get an error saying there's no such functions. I import crypto from 'crypto' then console.log(crypto) and I notice those functions were missing from the crypto object Image of this. I create a new javascript file, initialize node, does the same thing and all the functions are there. Image of this I'm really confused on why this happen... Please help.
Related
I have a node-js API that's part of an nx workspace and I'm trying to use the core crypto node-js module, however I think it's getting stripped out by webpack.
My code uses the following:
crypto.getRandomValues(new Uint32Array(1))[0].toString(16)
However I get a runtime error:
ReferenceError: crypto is not defined
Am I right in thinking this issue is likely being caused by webpack? And if so, how can I use crypto in my API?
I'm very new to webpack, so I'm pretty clueless here. My nx project uses the #nrwl/node:webpack executor (the default for node-js APIs), and I've followed the steps here on how to customize this projects webpack configuration, so my custom webpack config is being used, I just dont know what to do with it on order to allow my API to use crypto.
Any help on this would be appreciated.
For completeness, here's what my custom webpack config looks like, but it makes no difference to the problem. Note that his was crafted based on some of the suggested answers here:
const { merge } = require('webpack-merge');
/** #type { import('webpack').Configuration } */
const webpackConfig = {
target: 'node',
resolve: {
fallback: {
"crypto": false
}
}
};
module.exports = (config, context) => merge(config, webpackConfig);
Edit: As a workaround, I've imported the randomUUID function from crypto and can use this to do what I need to do. It seems having this import means crypto gets included in the webpack.
import { randomUUID } from 'crypto';
randomUUID().split('-')[0];
In your above code + runtime error, it seems like you haven't imported crypto correctly.
Furthermore you are mixing up some concepts. Webpack is normally used for frontend projects, as Node.js supports requiring modules out of the box and you don't need to compile your code, as you don't need to generate a javascript bundle that's servable by browsers.
Because your project runs in the browser, but your tests run in Node, which has a different global namespace, you get issues. In the browser crypto is defined globally, but in Node you need to import the crypto standard library, the "suggested answers" you are linking to talks about this concept.
To fill in the gap between browser and Node, to ensure the code you write works for test + browser, you use polyfills.
In your usecase you have a Node.js project. Crypto is a standard library for Node.js and you can just require it, why should you configure webpack to create fallbacks for it or use polyfills?
I am building a React Native app and tried to simply:
var cloudinary = require('cloudinary');
However, I keep getting an error:
Unable to resolve module url from node_modules/cloudinary/lib/config.js: Invalid directory /Users/node_modules/url
I have tried many of the suggestions for the Github thread on this including rebuilding the project, however, I still get the same error.
My gut is that this has something to do with the way lodash or other modules are being required. The same issue happened when I tried to require passport but not async.
What am I missing here? Any advice on what to look for when npm'ing files would be greatly appreciated.
I have a NodeJS server code written in JSX where I'm importing superagent like so:
import request from 'superagent';
When server starts after build, I get the following error:
TypeError: require is not a function. And this happens at the following line in my compiled code:
var crypto = require('crypto');
On tracing back I realized that crypto is required by 'formidable', which is required by 'superagent'.
I have the same superagent import in my client side javascript code but that works fine. I diffed the compile JS code for node(server), and client, and both are the same. So it doesn't seem like an issue with the way its build.
Has anyone seen this error before and would you know what needs to be done?
Found a solution to this here:
https://github.com/visionmedia/superagent/wiki/Superagent-for-Webpack
Adding the said plugin to web pack solved the issue.
How can I use the 'request' node.js module on the front-end?
normally I would retrieve it like so:
var request = require('request');
but this is not possible on the front-end since require is not recognized.
What is the best way to solve this?
To use node modules in the browser you can use a library called Browserify . This allows you to work with the common module pattern as well as the you can use this package browser-request to get the features of request module
I should use a plugin named hapi-mongoose-db-connector into my hapijs application. In the repository page the developers suggest the ways you can import correctly it. It says that the following way is the bad way:
# from the server
mongoose = server.pack.plugins['hapi-mongoose-db-connector'].mongoose
# or from a plugin
mongoose = plugin.plugins['hapi-mongoose-db-connector'].mongoose
and discourages using it. Instead he recommends to do in the following way:
You do nothing and just require mongoose in your plugins. As npm
requires are singletons (the code is loaded only once this works very
well)
but he doesn't show any examples. At this point I'm not pretty sure how to use it. I wouldn't call in every js files mongoose. I would call it once in my application somewhere and in my js files where I create models for the database, use it. Do you know any best practices in those cases?
Actually, first one is the hapi way doing this kind of thing.
But as the mongoose module is a singleton, that plugin just require mongoose and initialize it [1] after load that plugin into hapi, you can use mongoose in any file;
var mongoose = require("mongoose");