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

2
  • are you sure exported file properly? something like this ---- export default script; Commented Jul 20, 2020 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. It might be helpful. Commented Jul 20, 2020 at 10:24

1 Answer 1

3

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";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.