63

I'm developing a map functionality using ReactJS, My app.js file is:

import React, { Component } from 'react';
import './Map';
class App extends Component {
   render() {
     return (
         <div className="App">
            <Map/>
         </div>
     );
   }
}
export default App;

The error is:

./src/App.js
Line 8:  'Map' is not defined  react/jsx-no-undef

Search for the keywords to learn more about each error.

How can I solve this problem?

KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
Sarvesh Kulkarni
  • 971
  • 3
  • 10
  • 22
  • 2
    `import Map from './Map';` if you used default export in map file otherwise `import {Map} from './Map';` for named export. – Mayank Shukla Jun 08 '17 at 17:04

11 Answers11

62

Try using

import Map from './Map';

When you use import 'module' it will just run the module as a script. This is useful when you are trying to introduce side-effects into global namespace, e. g. polyfill newer features for older/unsupported browsers.

ES6 modules are allowed to define default exports and regular exports. When you use the syntax import defaultExport from 'module' it will import the default export of that module with alias defaultExport.

For further reading on ES6 import - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

shawon191
  • 1,945
  • 13
  • 28
21

you should do import Map from './Map' React is just telling you it doesn't know where variable you are importing is.

Demon
  • 826
  • 7
  • 22
10

This happens to me occasionally, usually it's just a simple oversight. Just pay attention to details, simple typos, etc. For example when copy/pasting import statements, like this: enter image description here

Mirza Sisic
  • 2,401
  • 4
  • 24
  • 38
  • 2
    Higher/lower case is also important. In my case, I mistakenly had tried to import DropDown instead of Dropdown. – Onat Korucu Feb 11 '20 at 08:45
6

The Syntax for the importing any module is

import {  } from "module";

or

import module-name from "module";

Before error (cakeContainer with small "c")

cakeContainer with small c

After Fix

Fixed with the changing the case

Aravinth Raja
  • 429
  • 4
  • 9
5

You have to tell it which component you want to import by explicitly giving the class name..in your case it's Map

import Map from './Map';

class App extends Component{
    /*your code here...*/
}
Shahrukh Anwar
  • 2,544
  • 1
  • 24
  • 24
  • This is seemingly the exact same answer as the first answer to this question given by @Demon. – NiFi Apr 23 '19 at 11:00
  • I am just answering to make things simple, as long as the answer is correct it may seem to be same with others. – Shahrukh Anwar Apr 24 '19 at 05:43
  • Appreciate the effort. A better approach next time might be to improve an existing answer, if no new view points are brought up warranting a separate answer. – NiFi Apr 24 '19 at 05:51
5

in map.jsx or map.js file, if you exporting as default like:

export default MapComponent;

then you can import it like

import MapComponent from './map'

but if you do not export it as default like this one here

export const MapComponent = () => { ...whatever }

you need to import in inside curly braces like

import { MapComponent } from './map'

**Here we get into your problem:**
sometimes in our project (most of the time that I work with react) we need to import our styles in our javascript files to use it. in such cases we can use that syntax because we have a bundler like Webpack that takes care of it, then later on, when we want to bundle our app, Webpack is going to extract our CSS files and put it in a separate (for example) app.css file. In those situations, we can use such syntax to import our CSS files into our javascript modules.

like below:

import './css/app.css'

if you are using sass all you need to do is just use sass loader with webpack!

amdev
  • 6,703
  • 6
  • 42
  • 64
2

Strangely enough, the reason for my failure was about the CamelCase that I was applying to the component name. MyComponent was giving me this error but then I renamed it to Mycomponent and voila, it worked!!!

Marco
  • 2,445
  • 2
  • 25
  • 15
2

should write like this -->

import Map from './map';

  1. look in this Map should have first later is capital .(important note)
  2. inset the single 'just mention your file location correctly'.
shiva
  • 5,083
  • 5
  • 23
  • 42
0

Here you have not specified class name to be imported from Map.js file. If Map is class exportable class name exist in the Map.js file, your code should be as follow.

import React, { Component } from 'react';
import Map from  './Map';
class App extends Component {
   render() {
     return (
         <div className="App">
            <Map/>
         </div>
     );
   }
}
export default App;
santosh sutar
  • 156
  • 1
  • 3
0

its very similar with nodejs

var User= require('./Users');

in this case its React:

import User from './User'

right now you can use

return (
      <Users></Users>
    )
-1

I hope this algorithm will help .

  1. npm install reactjs

  2. npx create-react-app ded-moroz.

  3. Enter to directory ‘ded-moroz’.

  4. npx create-react-component Weapon (pay attention component should be uppercase).

  5. Move component to ‘src’ directory

  6. Enter to jsx file, edit "Weapon" to "Snow"( up to you, we are testing).

  7. Open App.js file and put import Weapon from './Weapon/Weapon';

  8. Put tag <Weapon/> in your App method within <div className="App"> .

  9. Run npm start

Kirill Shur
  • 280
  • 2
  • 4