How add modul to node.js - node.js

I want to ask how can I add modul to node.js. I me I've installed node.js and want to create a simple form for uploading image. When I require a 'formidable' module and create a new object from it, i get an error 'cannot find the modul formidabe' .

Via npm (the node package manager), try this from your command line:
npm install formidable
as explained in the docs. After that it should work.

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...

How do I use zlib in a react app? or what is zlib_binding and why is it missing?

I have created a brand new react app using VS2022 and create-react-app.
I have installed zlib using npm install zlib.
getting the following error when running npm run start:
ERROR in ./node_modules/zlib/lib/zlib.js 1:0-43
Module not found: Error: Can't resolve './zlib_bindings' in 'c:\projects\mall\mall\node_modules\zlib\lib'
To troubleshoot, I created a test.js file with just require('zlib') in it and it works.
file ./node_modules/zlib/lib/zlib.js has import './zlib_bindings' as a single line in it. But there is no ./zlib_bindings.js.
Questions:
How do I fix this?
How does that work when I run node test.js?
Your test.js file works because you run it with node.
The zlib module provides compression functionality implemented using
Gzip and Deflate/Inflate. It is the part of nodejs core module written
in c++
This module can't be used outside of node.js
You can however try to use react-zlib-js and it should work!

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

Nodejs - can't find the .node_libraries folder

I'm new to node.js, I would like to use Step.js
https://github.com/creationix/step
The install instructions are very simple:
"Simply copy or link the lib/step.js file into your $HOME/.node_libraries folder."
The problem is that I can't find the .node_libraries folder anywhere.
I tried to create the folder myself and upload the step.js file but I get the following error:
ReferenceError: Step is not defined
I tried to create the following directories:
home/service/.node_libraries
root/.node_libraries
/usr/local/bin/.node_libraries
but none of them works.
Also, Is it possible to load Step as a module?
Thankyou
They have an npm module.
Simply run npm install --save step in the same dir your package.json resides and then you can require() it in your code like this:
var Step = require('step');
The documentation is outdated.
you should use the node package manager.
open the command line in the folder and write:
npm install step
also remember to include the module in your javascript file:
var Step = require('step');

Unable to load node.js package

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.

Resources