-1

I am working with Reactjs,I want to link "js" file means i want to include "js" file in my project but its not working, I put my code in "app.js" file,My js files exist in "public" folder,I put code in "_app.js" file and Here is my current code in "_app.js" file

import '../styles/globals.css'
import '../public/css/style.css'
import '../public/css/fonts.css'
import '../public/css/bootstrap.css'
import '../public/css/fonts2.css'
import Script from 'next/script'
function MyApp({ Component, pageProps }) {
  return (
             <> 
                <script src="../js/core.min.js"></script>
                <script src="../js/script.js"></script>
            <Component {...pageProps} />
            </>
        );  
}

export default MyApp
5
  • Does this answer your question? How to include custom JS files in to React create app Commented Jul 16, 2022 at 9:58
  • 1
    Does this answer your question? How to include dom-manipulating scripts into SSR Next.js App Commented Jul 16, 2022 at 10:58
  • @NikolaiKiselev no actually i dont want to add "third party scrips" i just want to include/use "js file" like we do in other framework Commented Jul 16, 2022 at 11:07
  • 1
    @Vyom, you can use the method from this answer. It can be a local script too. Commented Jul 16, 2022 at 11:10
  • As mentioned, inject your script using next/script, e.g. <Script src="/js/script.js" />. Note that if you have your files in the public folder you should reference them as /js/scripts.js and /js/core.min.js respectively. Commented Jul 17, 2022 at 17:02

1 Answer 1

0

There are several solutions described here: Add js script with react

Here is one:

import '../styles/globals.css'
import '../public/css/style.css'
import '../public/css/fonts.css'
import '../public/css/bootstrap.css'
import '../public/css/fonts2.css'
import Script from 'next/script'
function MyApp({ Component, pageProps }) {
    const script = document.createElement("script");
    script.src = "../js/core.min.js";
    script.async = true;
    document.body.appendChild(script);
    const script2 = document.createElement("script");
    script2.src = "../js/script.js";
    script2.async = true;
    document.body.appendChild(script2);
  return (
             <> 
            <Component {...pageProps} />
            </>
        );  
}

export default MyApp

Sign up to request clarification or add additional context in comments.

5 Comments

Please tell me that we must include our "css/js/" file in "_app.js" file ? And what should we write in _document.js, i am confuse between these two specially about "document.js"
You can also use "import 'file.js'"
as imported "css" files ?
its giving me following error "ReferenceError: document is not defined"
After refresh page,css is not working fine

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.