Importing library in react code throws compilation error - javascript

When trying to consume messages from rabbitmq from my react project for which I use the following import
import amqp from 'amqplib'
npm install amqplib is already done. amqplib is the library suggested by rabbitmq official docs itself. But even before adding any code using the installed library, with just the import, I always get the following error: This is the case for
ERROR in ./node_modules/amqplib/lib/connect.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\arun\Desktop\projects\react\amq-test\node_modules\amqplib\lib'
What is the missing piece here?

I think you missed net package. As a solution run this command on your terminal
npm i -s net
After that run your project again. If any error occurred try to remove node_modules folder and run npm install

I have the same problem in my react app. I solved by installing (npm i assert buffer ...) these modules;
assert
buffer
net
querysting
stream
tls
util

Related

How do I solve the problem of modules not found in angular application?

I have an angular application that uses two libraries locally, however, when trying to build everything ending up with an error of modules not found, but I thought it was some problems of undeclared dependencies, or imported, but everything is fine.
For better understanding, I have: Two local libraries that are used by a test application, where this application consumes both libs, but in the compilation it presents the error of modules not found:
`Error: Module not found: Error: Can't resolve '#ngx-formly/example-kendo' in 'C:\SamuelPierre\Example\Repositório\NgBibliotecas\NgBibliotecas\projects\framework-example\src\app\shared'
Error: projects/framework-example/src/app/app.component.ts:1:34 - error TS2307: Cannot find module '#example-library/identity' or its corresponding type declarations.
1 import { UserTokenService } from '#example-library/identity';
~~~~~~~~~~~~~~~~~~~~~~~`
This error appears for all dependencies, but my main file has all the necessary imports and declarations.
Also, "npm install" to install the libraries doesn't help as they are accessed locally. I've already cleaned the node modules, reinstalled npm, everything is ok, but the problem persists.
maybe you should run npm install or when it just one module just do npm install <module name> -save
Try (in Terminal):
npm install

Getting an error when trying to run my test in webdriverio

I have been having an issue when trying to run my webdriverio tests after trying to upgrade my node version. I am now getting the following error
2022-01-11T12:45:05.628Z DEBUG #wdio/config:utils: Couldn't find ts-node package, no TypeScript compiling
2022-01-11T12:45:05.729Z ERROR #wdio/config:ConfigParser: Failed loading configuration file: /Users/eoincorr/Downloads/coding/DSI_UI_TESTS/wdioTest.conf.js: Cannot find module 'csv-stringify/sync'
Can someone please advise me on why this is happening and what to do?
It seems that you didn't install typescript and/or ts-node.
Install missed dependencies:
npm i -D typescript ts-node
Also make sure, that you have installed all dependencies such as csv-stringify

Importing module returns null using expo

I'm trying to import a compass module without success using expo and react native
LINK to library
The error appears when I uncomment the import line, which gives me the error:
import RNSimpleCompass from 'react-native-simple-compass';
null is not an Object (Evaluating 'RNSimpleCompass.start')
I basically install the library using the provided instructions:
npm install react-native-simple-compass --save
I also tried linking it
npx react-native link react-native-simple-compass
then I run
npm start
which starts expo and gives me the error
I usually install and run other npm packages without problem, but this one keeps complaining when importing it.
I checked that the library is in my package.json dependencies. I even tried deleting the node_modules folder and re running npm install to reinstall all dependencies, but the error persist.
Am I missing something?
When you have expo "managed" project (without "ios" and "android" directories) you can't use react-native libraries that contain native code(objc or java). It worked in other cases because you probably were adding js only libraries or libraries that are explicitly supported by expo.
If you want to use that library you would need to eject to bare workflow(expo eject). You will still have access to all expo libraries you are currently using, but you won't be able to build it on Expo servers.

Error when building next.js app: Can't resolve 'next/head'

I am getting the below error when doing next build in my next.js app, after it's been working fine with just next (or npm run dev).
The error:
$ next build
The module 'react-dom' was not found. Next.js requires that you include it in 'dependencies' of your 'package.json'. To add it, run 'npm install --save react-dom'
Creating an optimized production build ...
> Using external babel configuration
> Location: "../.babelrc"
Failed to compile.
./pages/_app.js
Module not found: Can't resolve 'next/head' in '../pages'
> Build error occurred
Error: > Build failed because of webpack errors
at build (.../AppData/Roaming/npm/node_modules/next/dist/build/index.js:7:847)
I started my project by cloning Material UI's next.js example and just use that as it seems updated:
https://github.com/mui-org/material-ui/tree/master/examples/nextjs
However, thinking I just may of have an outdated or broken build, I re-cloned the next.js example and did next build as well, without modifying any files and I get the same error.
How can I fix this?
I just cloned that next.js example and it worked both with npm run dev, as well as npm run build->npm run start.
Judging by your error, I would check that "react-dom": "latest" is present in your package.json's dependencies. Did you run npm install after cloning the example?
The solution to this problem is to Just rerun these two commands again then your project will successfully Run. I also faced this problem but I got the solution.
npm i
yarn install
I ran into a similar problem when I included my nextjs app in a yarn workspace. I got around the issue by deleting the following directories in my nextjs app:
node_modules
.next
build
After removing them I ran yarn from the repo directory and started the nextjs app back up and it was working again.

Node.js, getting cannot find module 'ws' error

I have installed node.js on my Windows-7 PC. I am not able to create websocket connection to a remote server.
I tried to load modeule "ws" in my script :
var WebSocket = require('ws')
It gave an error :
cannot find module 'ws'
So I followed instructions over here : node.js websocket module installed but won't work in scripts
Execute cmd as Administrator (Right click cmd icon-> Run as Administrator) Then type in cmd:
c:\Node Instalation Dir\> npm install -g express
c:\Node Instalation Dir\> npm install websocket --force
Then run my script :--
D:\My Script Folder \> node myscript.js
Again same error. What could be the problem ?
cannot find module 'ws'
If you install websocket, you should require websocket, not ws:
npm install websocket
Then in REPL:
var websocket = require('websocket');
Alternatively, you can use ws module:
npm install ws
Repl/script:
var ws = require('ws');
Take a look at your node_modules directory (only the first level, the dirs right beneath it). You can require each of them by exact name.
require will actually look for a node_modules in current dir, then if not found, then the parent and again parent etc. If it doesn't find it, it will look for modules installed in global path pointed to by NODE_PATH. (And of course, native modules such as http and net.)
Try to install the package locally but not globally, i.e. without the -g option.
Have you installed the module with the -g option? I think that not every module is meant to be installed globally, instead, try installing it locally for the project you are creating (npm install), and check if the error persists. [...]
If you want to just require('something'); it is better to install it locally, otherwise, you have to require('{PREFIX}something'), where prefix is the path to where yo have installed it globally. Check out this blog post , and, as it says, generally the rule of thumb is to install things locally if you are going to use them in your app, and globally if you are going to use them from the command line.
node error cannot find module already installed.
Go to your project root directory and open the package.json file and edit
Add "type":"module";
Go to the top of your js file, delete the require and use the import statement to import 'ws' into your script.
I was also facing the same issue.
Error 1:
Just simply trying npm install ws to the same folder level where I had my server.js worked for me.
Error 2:
If you are getting the error WebSocket is not defined, then try the command npm install ws, after adding the below code. if ws doesn't work then try npm install websocket
Also, add this in your file:
const WebSocket = require('ws');
var conn = new WebSocket('ws://localhost:9090');

Categories

Resources