Using request with Gatsby - node.js

I am trying to add the request module to fetch some data, but I am having trouble, given, that it somehow relies on the fs module
Installing it and requiring it lands me this error:
This dependency was not found: fs, tls, net
To install it, you can run: npm install --save fs tls net
Installing tls and net seems to fix them (as they don't appear after that) but the fs issue remains. I've checked multiple issues about Webpack and request, but modifying my webpack config didn't help much.
How can I get request to work in a Gatsby website?
Thank you and good day

Request doesn't work on the frontend.
Use https://github.com/axios/axios instead.

Related

Cassandra-Driver webpack BREAKING CHANGE in React to access datastaxdb

I have a react app and have been installing packages using npm. I have a datastax db that I am trying to access in my react scripts using cassandra-driver, and make requests from using axios. I ran "npm install cassandra-driver" successfully. The line
const cassandra = require('cassandra-driver');
Throws 23 errors. All errors are breaking changes of the webpack version not including polyfills for node.js core modules by default. Such as
ERROR in ./node_modules/cassandra-driver/lib/streams.js 23:4-21
Module not found: Error: Can't resolve 'stream' in 'C:\Users\Base\Documents\py1\node_modules\cassandra-driver\lib'
Other posts about the webpack polyfill problems instruct to alter items in the webpack.config file. My problem is I don't know where to find the webpack config file as in my node_modules there are several files and a folder named webpack, and I am not new to programming but I am new to webdev & react. If there is an alternate solution for connecting my scripts to datastax that would be helpful as well. Thank you
OK I realize my error. I'm trying to access the db from the client side, and cassandra-driver is designed for server-side.

Using node-fetch in React app - "Cannot find module 'node:http'"

I have a simple setup with Strapi as a backend and React as a frontend. My goal is to get the result from the api I have in my backend using any tool out there. I first tried with Axios but seems to have an issue doing GET call because the 'Content-Lenght' wasn't generated automatically and I couldn't set it manually somehow.
I decided to use something else and one that I found that seems popular is node-fetch.
However, I can't even begin to use it. I install it locally to my project using:
npm i node-fetch --save
Then in my .js file:
import fetch from 'node-fetch';
const App = () => {
const result = fetch('https://api.github.com/');
}
export default App;
Then I try compiling or running the project and I get the following error message:
.\node_modules\node-fetch\src\index.js
Cannot find module: 'node:http'. Make sure this package is installed.
If I check the actual file in the node-fetch module. It is easy to see that the said module is imported there. However, I am unsure what this format for importing is even about with the 'node:xxxx'. I supose this is some kind of way to import basic node module but then, why would they be missing from my installation.
I tried it on me personal dev PC first and it failed. I then went ahead and troubleshooted for hours as to what it could be. I ended cleaning my entire project node_modules, rebuilding, uninstalling node.js and everything module installed globally. Reinstalling through nvm instead. Still didn't work.
I also tried installing "npm i http --save", which did nothing.
and today, I just tried this on a fresh project on my work computer and I have the exact same thing.
what could it be? Is this just a node-fetch issue in general?
("node-fetch": "^3.2.3")
The package node-fetch is a fetch implementation for Node.js not for the browser. In React you should be able to use fetch without installing and importing any package because fetch is part of the browser API and is present on the window object (window.fetch).
For browsers that don't support fetch you need a polyfill, check Can I Use for browser support and use whatwg-fetch as a polyfill.
See this for a polyfill solution for CRA:
https://github.com/facebook/create-react-app/blob/main/packages/react-app-polyfill/README.md
See this for examples on how to use fetch in React:
https://reactjs.org/docs/faq-ajax.html

Node.js Error: Cannot find module 'ssl-root-cas/latest'

Working with Node.js (Version 8.2.1) and Express, I am running an SSL-secured webapp which was working fine, but all of a sudden I am getting this error regarding the "ssl-root-cas" module when trying to restart the application:
Error: Cannot find module 'ssl-root-cas/latest'
This is the code for including the ssl certificates into my Node.js/Express application:
var rootCas = require('ssl-root-cas/latest').create();
rootCas
.addFile('../foo/foo.key')
.addFile('../foo/foo.crt')
;
https.globalAgent.options.ca = rootCas;
rootCas.inject();
All I did was installing the "compression" module via NPM. Ever since this installation, I keep getting this error. Uninstalling the "compression" module, restarting the server, nothing worked out. Again, prior to installing the compression module, everything worked fine. Any ideas what might be the problem with the ssl-root-cas?
is the ssl module in your package.json? It's possible that installing the compression module wiped away dependencies in node_modules that weren't in the package file.

react-native attempting to resolve built-in fetch function

I'm attempting to use the fetch function in react-native to grab a piece of data from the web. from what I understand reading the docs, the fetch function is built in to react-native.
when doing so, I get the module resolution error in the attached screenshot, where for whatever reason the react-native bundler is attempting to use the fetch provided by typescript from my ~/.cache directory.
I've previously had typescript 2.6 installed globally via npm, I uninstalled it to see if that might fix things, but the error persists.
I should note that the only things I've done for this project are to create it with create-react-native-app, add a little snippet to the App.js component stolen from the react docs to fetch some JSON, and yarn run eject the project (this error persisted before and after the ejection)
well in my turkey-induced frenzy, it appears I hadn't properly uninstalled the global typescript install on my computer.
having done so properly, the issue is resolved;
however the issue of react-native's module resolution strategy being incompatible with a global typescript installation persists.

Trying to use axios to make API call in a React/Phoenix App (using Brunch)

I'm trying to make use of the axios library to call some API endpoints.
My project setup is a Phoenix app (with brunch for asset management) and a React app (all in the web/static/js directory)
Most npm modules are ok, I do:
npm install --save _module-name_
Then I add the module to the brunch-config.js into the npm whitelist array.
Which I've done with axios, so it doesn't complain that it cannot find the library.
However, when I try and use axios e.g axios.post
I get the following error in the JS Console:
Cannot find module "axios/lib/adapters/xhr"
It's like brunch isn't loading in the axios dependencies (even though I can see that file if i navigate to node_modules/axios/lib/adapters
Has anyone had this issue (with any npm module and brunch/phoenix) and if so how do you go about fixing it?
Update
It seems to be a wider spread problem with brunch.
I also cannot use "React-Bootstrap" I get a similar error where it cannot find the sub-dependencies....
I had the same issue and updated brunch to the latest (2.7.5 at the time), which resolved my issue.
See https://github.com/brunch/brunch/issues/1136

Resources