HOW TO FIX SyntaxError: Cannot use import statement outside a module

Sdílet
Vložit
  • čas přidán 8. 08. 2022
  • Are you using imports in a node.js application and running into the SyntaxError: Cannot use import statement outside a module error?
    Here are 3 ways to fix this error.
    If you're developing a JavaScript application, then add "type": "module" into your package.json file.
    If you're developing a TypeScript application, then make sure "module" is set to "commonjs" in your tsconfig.json file.
    If you're importing a script in a HTML file, then add type="module" into the script tag.

Komentáře • 26

  • @toastybits.
    @toastybits.  Před rokem +3

    Have you run into this error? We all have! Leave a comment if this helped you solve the issue in your project.

    • @adrian-ioanmunteanu3027
      @adrian-ioanmunteanu3027 Před rokem

      Hey, what if it doesn't fix the issue?
      I'm using Js, and I already had the "type":"module" set inside my package.json
      What other reason might there be for the issue to occur? :(

    • @exoticme4760
      @exoticme4760 Před rokem

      ​@@adrian-ioanmunteanu3027 samee, I'm facing that error right now😢

  • @LearnWithUs5
    @LearnWithUs5 Před rokem

    thansk for give me this information i have try to fix that error from previous 5 days i search more but finfally recive your vidoe and i have fixed it

  • @cruzegaming151
    @cruzegaming151 Před 10 měsíci

    Bro I am sick getting this error finally you helped me thanx

  • @user-nd7sk2ph4f
    @user-nd7sk2ph4f Před 10 měsíci

    Thank you so much for your simple solution💙

  • @Jose-ub1yp
    @Jose-ub1yp Před rokem +2

    The best you really helped me ❤

  • @De_Brainiac
    @De_Brainiac Před 11 měsíci +1

    Thanks man, It worked!

  • @KINGMODEOFFICIAL
    @KINGMODEOFFICIAL Před 5 měsíci

    Hi, i am creating a widget that is heavily dependent on firebase. I put the firebase initialize command in a separate js file from the main js file.
    For some reason, anytime i run the local host it throws this exact error. even after inputing the "type"="module" type="module" in json and html respectively, i am still getting the error. Has anyone found another solution please?

  • @v3t3r4an9
    @v3t3r4an9 Před 2 měsíci

    i ran into this another errror: Failed to resolve module specifier "mysql2". Relative references must start with either "/", "./", or "../". How can i fix it, i am trying to connect to sql database for my project. Thanks

  • @KimHogeling
    @KimHogeling Před 5 měsíci

    Thank you! I added Jest and couldn't get it to run. Adding "type" into package.json fixed it! I didn't find this in the Jest documentation..

  • @malikrasheed-9937
    @malikrasheed-9937 Před 5 měsíci

    shout out to you , you solve my problem😍

  • @stockprooo
    @stockprooo Před 6 měsíci

    I have created a new file Header.js .... created a function header.. exported it .... imported inside the app.js...... but unable to use it as a tag in app.js ... like how r we supposed to use it in react...

  • @1990priyankasingh
    @1990priyankasingh Před rokem +3

    Hi,
    I am still facing issues after this fix. Forced clean cache too. Working on .ts. target and module set inside compiler options as per the video but still. Any ideas?

    • @atuloraon9610
      @atuloraon9610 Před 11 dny

      How did you fix it? I still get the error.

  • @stodani
    @stodani Před 8 měsíci

    what about "SyntaxError: Identifier 'sequelize' has already been declared" ?

  • @RaviKumar-py2bs
    @RaviKumar-py2bs Před rokem

    It is woking, but if import is inside called function, then it is giving error.

  • @AbdullahSaid-wj8kh
    @AbdullahSaid-wj8kh Před rokem +1

    not even using nodejs and encoounter this, pls be specific on the title!

  • @DJ_DeenoB
    @DJ_DeenoB Před 9 měsíci +1

    and what do you do if you dont have the node file

    • @DJ_DeenoB
      @DJ_DeenoB Před 9 měsíci

      im new to this shit how do i create the module

  • @tommyshaw2420
    @tommyshaw2420 Před 5 měsíci

    What I dont get, is why doesnt it install the module/cjs type from the get go? ITs the stupidest thing

  • @andresespinoza3159
    @andresespinoza3159 Před rokem

    great video bro !!!!

  • @lucasshira
    @lucasshira Před 9 měsíci

    Thanks!

  • @morji8664
    @morji8664 Před rokem +1

    its not working

  • @uctoannguyen2091
    @uctoannguyen2091 Před 11 měsíci

    thanks alot

  • @xushenxin
    @xushenxin Před 2 dny

    bullshit