React import problem, 'Module not found: Can't resolve' - javascript

I have a folder system in my React project, which you can see in the screenshot below:
I am trying to make import to index.js, located in src/components/KeyboardCard/index.js, from file context.js that is located in src/context.js.
I am getting this error message:
Module not found: Can't resolve '.../context.js' in 'C:\React\top-board\src\components\KeyboardCard'.
Help me please.

It's two levels higher:
import AppContext from '../../context';

You can't have triple dots import there, try:
import AppContext from '../../context.js';

You have provided the wrong path.
import AppContext from '../../context.js';

If you want to go 2 levels higher you have to do it like this:
../../context
Also, you don't have to define file format ".js" there, that's not required.

Related

Why am I not able to import the following File

This is my src directory -
And while importing the last file (StateProvider.js) in Product.js, this is the error I get while importing StateProvider -
My import statement from Product.js-
Can anyone guide me, why my import is not working?
The error occurs because StateProvider.js is not in the same directory as Product.js. You have to specify the import statement like this:
import StateProvider from '../../StateProvider';
Probably 2 errors.
First, If you export the default file you're good to go.
export default StateProvider
Second is if you are on the correct directory.
it shouild be
import StateProvider from '../../StateProvider'
import StateProvider from '../../StateProvider'
I also recommend using "Auto Import" extension if you working with Visual Studio Code.

Component exception Element type is invalid

So I'm having an error that I have no idea where it coming from:
I tried finding where is HeaderSegment but it doesn't exist in my project, I don't have-
import HeaderSegment from './example.js'
I did the usual searched in google but the problem in these answers that I don't have any import module that is HeaderSegment. I'm using React native, Admob and Firebase, and I don't really know where the error is located so here the Repo. I tried to remove node_modules and then npm install but no good,
here the stack:
(Ofcourse that doesn't work) I know it hard to answer this question there two files involved they are header.js and homeStack.js.
Any help will make me happy, Thank you.
Issue
Seems you have mixed up default and named imports of your Header component.
Header is default exported from header.js:
export default Header;
but imported as a named import in homeStack.js:
import { Header } from '../shared/header';
Solution
Fix the Header export to match how it's imported (named export/import):
export Header;
or fix the Header import to match how it's exported (default export/import):
import Header from '../shared/header';

How do i import my file to app.js using javascript?

I'v been trying to import a javascript file to app.js found in create-react-app package. I used this code to import my file :
note:my file is located in a folder called components where you find the folder Navigation and its in that folder.
import script from './components/Navigation/script;
and i exported the file using :
export default App;
but in my terminal i got an error saying
Module not found : cant resolve'./components/Navigation/script'
Could someone please tell me how i can fix this error.
Thank you in advance :-)
You got the syntax wrong.
import script from './components/Navigation/script;
actually imports the default export from script.js
export default function() {}
You can also import named exports like this:
import {something} from "./script.js"
Which imports
const something = 42;
export something;
I guess you could import js(and css) files with
import "./scripts.js" //or import "./style.css"
But you generally want to export individual functions, values etc.
You can also import everything from a module like this
import * as React from "react";
But create-react-app configuration allows you to use this as
import React from "react"; //Internally equals to import * as React from "react";

Cannot import Module without curly braces in react even after 'export default'

I have read somewhere that importing module in react with curly braces around imports the entire library instead and effectively increases the bundle size. I was using this concept and was successfull in importing modules without curly braces, like this:
import Jumbotron from 'reactstrap';
and it was working fine. I don't know why the next time I build the code, it started showing me this warning:
WARNING in ./React Coursera/Header.js 5:71-77
export default (imported as Jumbotron) was not found in reactstrap.
Also the app didn't run in browser.
Then I went to node_modules to check if export default is present in jumbotron or not, and I found this statement:
export default Jumbotron;
It means that indeed it was exporting the Jumbotron as default, then why it showed me this warning.
Can you help me guys to fix this problem?
Thanks in advance!
Where did you read that importing with curly will increase the bundle size, it's reverse,
// below line will import everything
import * as reactstrap from 'reactstrap'
But
// this will import only specific module
import { Jumbotron } from 'reactstrap'
By this line :
// will import from /reactstrap/index.js
import Jumbotron from 'reactstrap';
You are importing nothing https://github.com/reactstrap/reactstrap/blob/master/src/index.js , as there is export default
So I don't know how it worked before in your case
Below line :
// and this line is not inside the /reactstrap/index.js but /reactstrap/Jumbotron.js
export default Jumbotron;
is here : https://github.com/reactstrap/reactstrap/blob/master/src/Jumbotron.js
So you can do :
import { Jumbotron } from 'reactstrap'
It depends on your build setup and/or how the library code is setup. Some libraries are built in a way that won't import the entire library when you use curly braces. You can also have something enabled in your build tools called "tree shaking" which will remove all code that is unused.
I'm guessing what you were trying to do was import Jumbotron individually which is a safe bet when you are unsure if the whole lib will be imported. Again, it depends on the file structure of the library but you are probably missing the sub-directory in your import. There should be directories inside of the node_module folder for each component. Might be something like node_modules/reactstrap/Jumbotron. The default export you saw was probably on the Jumbotron file. When you use import Jumbotron from 'reactstrap' you are asking it do find a default export for the "main" file of the library. This would be defined in the package.json file of the library.
What you need to do is add the sub-directory to your import like so (just guessing here) import Jumbotron from 'reactstrap/Jumbotron'. Just think of reactstrap/ being the root directory of the library, you can select any file like you normally would.
If you are using webpack, there's this awesome plugin where you can check to see what is included in your bundles just to make sure you are indeed only importing the code that you need https://github.com/webpack-contrib/webpack-bundle-analyzer

Importing reactstrap components uses wrong syntax

According to the docs, I should be able to do this to import a component for use in my React App:
import { Alert } from 'reactstrap';
However, this will cause webpack to complain like this:
| Module not found: Error: Can't resolve 'reactstrap' in '~/project/client/app/bundles/Frontend/components/team'
Then, I switch the import to this:
import Alert from 'reactstrap/lib/Alert';
And the import & functionality works!
This probably means (?) some configuration or module exports are not working for this module. Where can I start debugging / fixing this?

Categories