Unable to load node.js package - node.js

I have a node.js project where I'm trying to load and parse .xml files. In an attempt to do this, I was going to rely on the DOM Parser via the following command:
npm install xmldom
The package was successfully installed. However, when I attempt to execute my code via mocha, I get an error that says: "Error: Cannot find module 'xmldom'". My code works fine until I add the following line at the top of my code:
var dom = require('xmldom');
If I comment out the line, my code works. But, if I remove the commments, to make the code active, I get the error again. I haven't written anything to actually parse the xml file yet. I'm just trying to import the dom parser at this point.
I also tried loading the xmldom package globally with the following command:
npm install -g xmldom
That did not solve my problem either. What am I missing?

I wrote a blog post explaining what to look for when you get a Cannot find module 'xmldom' error.

Related

NodeJS require doesn't work. Cannot import downloaded npm modules

I have a slight problem with a basic Node.JS method. When I'm trying to use "require()' method to import any downloaded (with 'npm install ..) module/library, I get a Visual Studio Code message that asks 'to convert 'require'(which is a Common JS module) into ES. If I do it, it transforms 'require()' into 'import..' though I want keep using 'require()'. Why is it so ? I read somewhere that Common JS is already included in the Node.JS environment though.
Then, when trying to compile my index.js file (with 'node index.js'), I obviously get an error about 'require' not recognized method.
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users...index.js from C:\Users...index.js not supported.
I tried then to install Webpack to fix this issue, but nothing change. I also kind of installed CommonJS (npm i common-js)..
Another common answer was to remove 'type':'module' from package.json file which normally should allow to use 'require', but I don't even have such a line in the file.
On top of that I've recently read that 'require' is not compatible with browser development tools (it's ok). But I'm trying to import a downloaded module/npm package in VSC terminal, not even in a browser.
As you understand I'm new to Node.JS, but I don't really get what's going on in this case...

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

Twilio Video Startup

I am fairly new to React and am trying to run an example to get started with understanding how Twilio interfaces with React/Node. I am currently using the repo https://github.com/twilio/twilio-video.js, and am trying to run the test > framework > twilio-video-react, but when I npm install then npm start, the server starts, but throws the error:
Failed to compile
./src/App.js
Module not found: Can't resolve 'twilio-video'
I did `npm install twilio-video`, but still throwing error. What am I missing here? Can anyone else get this example started? I want a base example with Twilio Video working with React so I can learn from there, any other resources would help, thanks!
I tried these two examples as well, but they are outdated:
https://www.twilio.com/blog/2018/03/video-chat-react.html
https://www.twilio.com/blog/2016/03/building-a-react-powered-video-chat.html
Okay, got an example up and running with a little fudging.
I followed this example: https://www.twilio.com/blog/2018/03/video-chat-react.html
But there were a few old libraries that were causing errors.
Posted a working version on my Github linked here.

Node.js module is looking for node-modules instead of node_modules

I have the following based on instructions from here:
const Pubgapi = require('pubg-api');
const apiInstance = new Pubgapi('api-key');
For some reason when I run it, the console says unable to resolve path but it's looking for a node-modules which I don't have. This is an API wrapper that I am trying to use by the way. What could cause it to give me this error?
I did an npm install as the instructions said and nothing else before the statements above.
Error message:
Unable to resolve ../node-modules/pubg-api" from ".//components/Main.js`: The module `../node-modules/pubg-api` could not be found"
I have checked the node_modules folder. It is indeed pubg-api, not pubg_api. Requiring pubg_api gives me this error.
This error is probably related to the HTTPS library that the library you are trying to install depends on. Node.js libraries are not compatible with react-native since it is not working on a Node.js platform.
To overcome this issue you can try to use react-native-http library, but I did not try to use it before, so I'm not sure if it's going to work or not.

Generation of svg on server side using highcharts

I did require('jsdom') in node js but i constantly get jsdom module not found.
More over i want to generate a svg document using highcharts on the server so that i can later on use that image in my pdf( I will convert that svg to image using batik).
Is there a link that may help me with this.
I have read through the http://blog.davidpadbury.com/2010/10/03/using-nodejs-to-render-js-charts-on-server/
but the prototype didnt make much sense. Is there a module which has been implemented rather than just a prototype.
You need to install all the required modules first. First download the tar file from his github https://github.com/davidpadbury/node-highcharts. Go to the directory where you saved the file and hit
npm install davidpadbury-node-highcharts-576f763.tar.gz
if you need to install additional modules like jsdom, try
npm install jsdom
His prototype seem to work fine with older version of highchart. but i am having some problem using it with latest highchart.
I hope this helped

Resources