Why am I unable to import my component in react? - reactjs

I am doing a basic import of a component and it keeps on giving me this error:
./src/Nav.js
Module not found: You attempted to import ..\node_modules\#pmmmwh\react-refresh-webpack-plugin\lib\runtime\RefreshUtils.js which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
But my Nav component is inside the /src.
my import
import React from 'react'
const Nav = () => {
return(
<header class="header">
</header>
)
}
export default Nav
Here is my package.json:
{
"name": "react-timesheet-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#babel/core": "^7.12.16",
"#babel/preset-env": "^7.12.16",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-helmet": "^6.1.0",
"react-scripts": "4.0.2",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"babel-loader": "^8.2.2",
"webpack": "^5.22.0"
}
}
I recently had a similar problem with missing packages, if that is the reason for the error, why do I keep on getting them?

Related

why antd switch rendering is not working?

I try to make typescript react setting
and then just install antd
$ yarn add antd
and fix app.tsx like this
import React from "react";
import logo from "./logo.svg";
import "./App.css";
import { Switch } from "antd";
function App() {
function onChange(checked: any) {
console.log(`switch to ${checked}`);
}
return (
<div className="App">
<Switch defaultChecked onChange={onChange} />
</div>
);
}
export default App;
but i can't see right switch
https://i.stack.imgur.com/rmGjJ.png
i want to look like this
https://i.stack.imgur.com/xKVWW.png
and this is my package.json
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^12.0.0",
"#testing-library/user-event": "^13.2.1",
"#types/jest": "^27.0.1",
"#types/node": "^16.7.13",
"#types/react": "^17.0.20",
"#types/react-dom": "^17.0.9",
"antd": "^4.18.8",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"typescript": "^4.4.2",
"web-vitals": "^2.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
How to solve this rendering problem?

"Cannot find module 'custom-module' or its corresponding type declarations" for module added from GitHub

I used yarn add github:username/custom-module to add a custom module to my project. The module and its contents appears in my node_modules folder, but I'm unable to import anything from it.
I import it into my file like using import { Component } from "custom-module";, but TypeScript returns, "Module not found: Error: Can't resolve 'custom-module' in 'filepath...'".
My project's package.json:
{
"name": "project-name",
"version": "0.1.0",
"private": true,
"dependencies": {
"#ant-design/icons": "^4.7.0",
"#dnd-kit/core": "^4.0.2",
"#dnd-kit/sortable": "^5.1.0",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"#types/express": "^4.17.13",
"#types/lodash": "^4.14.176",
"#types/react": "^17.0.38",
"#types/react-dom": "^17.0.11",
"#types/react-router-dom": "^5.3.2",
"#types/styled-components": "^5.1.15",
"#types/ws": "^8.2.2",
"eslint-plugin-react-hooks": "^4.3.0",
"esm": "^3.2.25",
"express": "^4.17.2",
"mobx": "^6.3.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-quick-reactions": "github:username/custom-module",
"react-router-dom": "^5.2.0",
"react-scripts": "5.0.0",
"styled-components": "^5.2.3",
"typescript": "^4.4.4",
"web-vitals": "^1.0.1",
"ws": "^8.4.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"serve": "node -r esm src/backend/server.ts"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:8080",
"devDependencies": {
"#typescript-eslint/eslint-plugin": "^5.9.1",
"#typescript-eslint/parser": "^5.9.1",
"eslint-plugin-react": "^7.28.0"
}
}
And the package.json from the module:
{
"name": "custom-module",
"version": "0.1.0",
"private": false,
"description": "A description.",
"license": "MIT",
"keywords": [
"react",
"react-component",
],
"homepage": "https://github.com/username/custom-module#readme",
"bugs": {
"url": "https://github.com/username/custom-module/issues"
},
"repository": "github:username/custom-module",
"peerDependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#types/react": "^17.0.39",
"typescript": "^4.5.5"
}
}
As Phil suggested in this comment, the custom-module package.json did not contain main, files, or directories. I had to import directly from "custom-module/file/path/Component".

Can't find anything using "screen" in react-testing-library

Whenever I try to find the element i.e. expect(screen.getByRole("heading", { level: 2 })).toBeInTheDocument(); I get the message :
TestingLibraryElementError: Unable to find an accessible element with the role "heading"
However if I do
const { getByRole } = render(<NewCommentForm />);
expect(getByRole("heading", { level: 2 })).toBeInTheDocument();
It works.
Here is my package.json file:
{
"name": "client",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:3001/",
"dependencies": {
"#testing-library/jest-dom": "^5.11.5",
"#testing-library/user-event": "^7.1.2",
"axios": "^0.21.1",
"jest-axe": "^4.0.0",
"moment": "^2.29.0",
"prettier": "^2.2.1",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#testing-library/dom": "^7.28.1",
"#testing-library/react": "^11.2.5"
}
}
And setupTests.js
import "#testing-library/jest-dom/extend-expect";
Can you tell me why screen is not working properly when call getByRole method on it? It seems like screen.getByText works fine.

Font Awesome icon in React

I was unable to get Font Awesome icon working when using string as value for icon parameter.
This working:
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome';
import { faCoffee } from '#fortawesome/free-solid-svg-icons'
<FontAwesomeIcon icon={faCoffee} />
However this does not:
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome';
<FontAwesomeIcon icon="coffee" />
And according to this documentation it should.
But I am new to React so may be missing something.
Using javascript. Project created with react cli.
Package.json:
{
"name": "test",
"version": "0.1.0",
"private": true,
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.34",
"#fortawesome/free-brands-svg-icons": "^5.15.2",
"#fortawesome/free-regular-svg-icons": "^5.15.2",
"#fortawesome/free-solid-svg-icons": "^5.15.2",
"#fortawesome/react-fontawesome": "^0.1.14",
"#testing-library/jest-dom": "^5.11.9",
"#testing-library/react": "^11.2.3",
"#testing-library/user-event": "^12.6.2",
"font-awesome": "^4.7.0",
"node-sass": "^4.14.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-metismenu": "^1.4.0",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"rsuite": "^4.8.8",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
By "not working" I mean that icon is not visible on the page and console have an error: "Could not find icon {prefix: "fas", iconName: "coffee"}"
You'll need to import library like this: import { library } from '#fortawesome/fontawesome-svg-core'
Then add the icon(s) you wish to use.
import { faCoffee } from '#fortawesome/free-solid-svg-icons'
library.add(faCoffee /*, faCheckSquare, etc*/)
After this, you should be able to use the icon as "coffee". If you are not adding the icons to the library, they will not be included in the bundle, hence the error.

./src/index.js Module not found: Can't resolve '~antd/dist/antd.css'

I have the issue ./src/index.js Module not found: Can't resolve '~ antd / dist / antd.css', I can not fix it.
I tried to put the #import '~ antd / dist / antd.css'; . In css file.
Without success.
package.json:
{
"name": "time-clock",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.11.6",
"#testing-library/react": "^11.2.2",
"#testing-library/user-event": "^12.5.0",
"antd": "^4.9.2",
"firebase": "^8.1.2",
"polished": "^4.0.5",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.1",
"styled-components": "^5.2.1",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
#import '~antd/dist/antd.css';
is only meant for css or scss files.
if you started your app with -> npx create-react-app yourAppName
don't import 'antd/dist/antd.css'; into your index.js it will fail !!!
Solution
DO NOT USE THE GET STARTED DOCS
Instead use the Use in create-react-app found here
Add #import '~antd/dist/antd.css'; to the top of App.css file
Then you should be able to use it on any component like import { Button } from 'antd';
Add in App.js
import 'antd/dist/reset.css';
documentation

Resources