1

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 :-)

M090009
  • 1,129
  • 11
  • 20
Helia Amini
  • 23
  • 1
  • 5
  • are you sure exported file properly? something like this ---- export default script; – Atul Kumar Jul 20 '20 at 10:21
  • `app.js` and `components` must be in the same folder. Are you sure they are in the same directory? Perhaps try [This question](https://stackoverflow.com/questions/44439205/cant-resolve-module-not-found-in-react-js). It might be helpful. – nibble Jul 20 '20 at 10:24

1 Answers1

2

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";
Ali Mert Çakar
  • 290
  • 1
  • 8
  • 15