HTML parser for node typescript - node.js

I am creating a node.js application written in Typescript. Its one of the functionality is to parse HTML file. But I can't seem to find the HTML parser written in Typescript.
As, normal npm modules specified in HTML-parser on Node.js are not importing in my application.
I have tried installing the node.js module like
npm install domhandler
and, then imported the module in my main.ts file like
import * as parser from "htmlparser2"
It was throwing error
Cannot find module 'htmlparser2'
Is there any way I can import these normal npm modules in my Typescript node application?
Or, is there any typescript module which I can use to parse HTML file?

Related

Electron + React: Clear fs module

I have an Electron + React app based on create-react-app. In the React code, I'd like to use excel4node to generate an Excel file. The issue is that excel4node tries to import modules such as fs and http which are not available on the renderer side. This results in the following Webpack error:
[0] ERROR in ./node_modules/image-size/dist/index.js 7:11-24
[0] Module not found: Error: Can't resolve 'fs' in 'C:\[...]\node_modules\image-size\dist'
I do not want to use any methods from excel4node which are accessing these modules, such as writing the Excel file to disk for example -- I would be fine with these functions erroring if they were called.
Is it possible to overwrite the problematic modules with something like an empty module just so that the import works? Since I used create-react-app I do not have easy access to the webpack configuration, so if there is a different way that I would prefer that.

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!

Node.js: How to use npm ES6 modules (import) in commonjs (require) app.js

I'm kind of a newbie with node.js, so please, be merciful :)
I use node ver 14.15.1
The problem I'm facing is to get a npm module("ghost-cursor", https://www.npmjs.com/package/ghost-cursor) that is written in ES6 ("import") working in commonjs (require).
I need to pack my node.js app in an exe file and the npm "pkg" doesn't seem to work with ES6. With nexe I can't get to pack all dependencies in order to work outside of the project folder and on other machines... I have tried to transpile the ES6 module to commonjs with babel:
babel --presets=es2015 ./folder/filename.js > ./folder/filenameCJ.js
but I get the error: SyntaxError: Invalid or unexpected token
I have also tried the solutions posted in:
How to use ES6 modules in CommonJS?
but I can't get it to work.
Are there any other ways, except "nexe" and "pkg" to make an exe file from node.js?
Is it possible to use an ES6 module from npm in commonjs with require? If so, how?
Thanky you for your help and replies.

How can I run an ExpressJS API using module instead of commonjs and has been bundled by Webpack?

In my express app I use Import and Export statements. I have the "type": "module" line in my package.json file. I can run the app using Node before bundling it with Webpack.
After I've bundled it into one JS file, Webpack converts the Imports to "require". Then I get the "ReferenceError: require is not defined" error when I try to run the bundled JS file using Node.I would like to use modules instead of converting my project to commonjs.
Webpack v.5
Node v.14
Express v.4

Resources