Importing ES6 module in LWC project (sfdx)Lightning Web Components Easy Spaces sample application LWC component codevscode/sfdx doesn't see my projectlwc: sfdx unable to retrieve lwc filesImport ES modules in LWCLWC - Unit Testing NavigationMixin.GenerateUrlimport es6 from an extra javascript file in another component bundle in LWC'git' is not recognized as an internal or external command with salesforce cli

What are the pros and cons of Einstein-Cartan Theory?

Overwrite file only if data

Church Booleans

Why does my house heat up, even when it's cool outside?

Is it appropriate for a prospective landlord to ask me for my credit report?

Vacuum collapse -- why do strong metals implode but glass doesn't?

Why didn’t Doctor Strange stay in the original winning timeline?

Are illustrations in novels frowned upon?

Why is Boris Johnson visiting only Paris & Berlin if every member of the EU needs to agree on a withdrawal deal?

Taking out number of subarrays from an array which contains all the distinct elements of that array

Is there such a thing as too inconvenient?

Can I submit a paper under an alias so as to avoid trouble in my country?

Starships without computers?

Metal that glows when near pieces of itself

Why would the US President need briefings on UFOs?

How to "know" if I have a passion?

Why doesn't mathematics collapse even though humans quite often make mistakes in their proofs?

Was 'help' pronounced starting with a vowel sound?

How to think about joining a company whose business I do not understand?

Ask for a paid taxi in order to arrive as early as possible for an interview within the city

Can pay be witheld for hours cleaning up after closing time?

How to avoid using System.String with Rfc2898DeriveBytes in C#

Can we save the word "unique"?

(Why) May a Beit Din refuse to bury a body in order to coerce a man into giving a divorce?



Importing ES6 module in LWC project (sfdx)


Lightning Web Components Easy Spaces sample application LWC component codevscode/sfdx doesn't see my projectlwc: sfdx unable to retrieve lwc filesImport ES modules in LWCLWC - Unit Testing NavigationMixin.GenerateUrlimport es6 from an extra javascript file in another component bundle in LWC'git' is not recognized as an internal or external command with salesforce cli






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















Trying to do something relatively simple.
I've 2 LWC component under the lwc folder: lwc/cmp1 and lwc/cmp2



How can I create a new ES6 module that will serve as a utils.js file and then import it to each of cmp1 and cmp2?



I tried creating the utils.js file on cmp1 folder but couldn't import it to cmp2. Tried creating a new folder under lwc folder but it still didn't work (can't import).



The error I keep on getting is:



force-app/main/default/lwc/utils/utils.js LWC1011: Failed to resolve import "../utils/utils" from "cmp1.js". Please add "../utils/utils" file to the component folder.


There is no problem with the path given, VSCode easily find the related path and indicates no errors, only when trying to deploy to Salesforce










share|improve this question



















  • 1





    can you show relative code?

    – salesforce-sas
    9 hours ago

















1















Trying to do something relatively simple.
I've 2 LWC component under the lwc folder: lwc/cmp1 and lwc/cmp2



How can I create a new ES6 module that will serve as a utils.js file and then import it to each of cmp1 and cmp2?



I tried creating the utils.js file on cmp1 folder but couldn't import it to cmp2. Tried creating a new folder under lwc folder but it still didn't work (can't import).



The error I keep on getting is:



force-app/main/default/lwc/utils/utils.js LWC1011: Failed to resolve import "../utils/utils" from "cmp1.js". Please add "../utils/utils" file to the component folder.


There is no problem with the path given, VSCode easily find the related path and indicates no errors, only when trying to deploy to Salesforce










share|improve this question



















  • 1





    can you show relative code?

    – salesforce-sas
    9 hours ago













1












1








1








Trying to do something relatively simple.
I've 2 LWC component under the lwc folder: lwc/cmp1 and lwc/cmp2



How can I create a new ES6 module that will serve as a utils.js file and then import it to each of cmp1 and cmp2?



I tried creating the utils.js file on cmp1 folder but couldn't import it to cmp2. Tried creating a new folder under lwc folder but it still didn't work (can't import).



The error I keep on getting is:



force-app/main/default/lwc/utils/utils.js LWC1011: Failed to resolve import "../utils/utils" from "cmp1.js". Please add "../utils/utils" file to the component folder.


There is no problem with the path given, VSCode easily find the related path and indicates no errors, only when trying to deploy to Salesforce










share|improve this question














Trying to do something relatively simple.
I've 2 LWC component under the lwc folder: lwc/cmp1 and lwc/cmp2



How can I create a new ES6 module that will serve as a utils.js file and then import it to each of cmp1 and cmp2?



I tried creating the utils.js file on cmp1 folder but couldn't import it to cmp2. Tried creating a new folder under lwc folder but it still didn't work (can't import).



The error I keep on getting is:



force-app/main/default/lwc/utils/utils.js LWC1011: Failed to resolve import "../utils/utils" from "cmp1.js". Please add "../utils/utils" file to the component folder.


There is no problem with the path given, VSCode easily find the related path and indicates no errors, only when trying to deploy to Salesforce







salesforcedx lightning-web-components






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 10 hours ago









ShaiShai

1174 bronze badges




1174 bronze badges










  • 1





    can you show relative code?

    – salesforce-sas
    9 hours ago












  • 1





    can you show relative code?

    – salesforce-sas
    9 hours ago







1




1





can you show relative code?

– salesforce-sas
9 hours ago





can you show relative code?

– salesforce-sas
9 hours ago










2 Answers
2






active

oldest

votes


















4














You should not use "../utils/utils" for imports from modules. It should be 'c/utils'.



Implementation:



Create utils module:



sfdx force:lightning:component:create --type lwc -n utils -d force-app/main/default/lwc


Delete utils.html file.



Put below code in utils.js:



// mortage.js
const getTermOptions = () =>
return [
label: '20 years', value: 20 ,
label: '25 years', value: 25 ,
];
;

const calculateMonthlyPayment = (principal, years, rate) =>
// Logic
;

export getTermOptions, calculateMonthlyPayment ;


Now deploy this module:



sfdx force:source:deploy -p force-app/main/default/lwc/utils


Now, in your component cmp1 which is already created and deployed, import above utils module like below:



import getTermOptions, calculateMonthlyPayment from 'c/utils';


Now, you can use getTermOptions() inside the class of cmp1.js file.






share|improve this answer


































    2














    you need to create service component (component without *.html file).



    Here is the example from documentation.



    Your utils file.



    // mortage.js
    const getTermOptions = () =>
    return [
    label: '20 years', value: 20 ,
    label: '25 years', value: 25 ,
    ];
    ;

    const calculateMonthlyPayment = (principal, years, rate) =>
    // Logic
    ;

    export getTermOptions, calculateMonthlyPayment ;


    Your import statement in cmp1 or cmp2.



    import getTermOptions, calculateMonthlyPayment from 'c/mortgage';


    For more information follow LWC documentation provided earlier and spec for import statements.



    Hopefully, that will help.






    share|improve this answer





























      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "459"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f274261%2fimporting-es6-module-in-lwc-project-sfdx%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      You should not use "../utils/utils" for imports from modules. It should be 'c/utils'.



      Implementation:



      Create utils module:



      sfdx force:lightning:component:create --type lwc -n utils -d force-app/main/default/lwc


      Delete utils.html file.



      Put below code in utils.js:



      // mortage.js
      const getTermOptions = () =>
      return [
      label: '20 years', value: 20 ,
      label: '25 years', value: 25 ,
      ];
      ;

      const calculateMonthlyPayment = (principal, years, rate) =>
      // Logic
      ;

      export getTermOptions, calculateMonthlyPayment ;


      Now deploy this module:



      sfdx force:source:deploy -p force-app/main/default/lwc/utils


      Now, in your component cmp1 which is already created and deployed, import above utils module like below:



      import getTermOptions, calculateMonthlyPayment from 'c/utils';


      Now, you can use getTermOptions() inside the class of cmp1.js file.






      share|improve this answer































        4














        You should not use "../utils/utils" for imports from modules. It should be 'c/utils'.



        Implementation:



        Create utils module:



        sfdx force:lightning:component:create --type lwc -n utils -d force-app/main/default/lwc


        Delete utils.html file.



        Put below code in utils.js:



        // mortage.js
        const getTermOptions = () =>
        return [
        label: '20 years', value: 20 ,
        label: '25 years', value: 25 ,
        ];
        ;

        const calculateMonthlyPayment = (principal, years, rate) =>
        // Logic
        ;

        export getTermOptions, calculateMonthlyPayment ;


        Now deploy this module:



        sfdx force:source:deploy -p force-app/main/default/lwc/utils


        Now, in your component cmp1 which is already created and deployed, import above utils module like below:



        import getTermOptions, calculateMonthlyPayment from 'c/utils';


        Now, you can use getTermOptions() inside the class of cmp1.js file.






        share|improve this answer





























          4












          4








          4







          You should not use "../utils/utils" for imports from modules. It should be 'c/utils'.



          Implementation:



          Create utils module:



          sfdx force:lightning:component:create --type lwc -n utils -d force-app/main/default/lwc


          Delete utils.html file.



          Put below code in utils.js:



          // mortage.js
          const getTermOptions = () =>
          return [
          label: '20 years', value: 20 ,
          label: '25 years', value: 25 ,
          ];
          ;

          const calculateMonthlyPayment = (principal, years, rate) =>
          // Logic
          ;

          export getTermOptions, calculateMonthlyPayment ;


          Now deploy this module:



          sfdx force:source:deploy -p force-app/main/default/lwc/utils


          Now, in your component cmp1 which is already created and deployed, import above utils module like below:



          import getTermOptions, calculateMonthlyPayment from 'c/utils';


          Now, you can use getTermOptions() inside the class of cmp1.js file.






          share|improve this answer















          You should not use "../utils/utils" for imports from modules. It should be 'c/utils'.



          Implementation:



          Create utils module:



          sfdx force:lightning:component:create --type lwc -n utils -d force-app/main/default/lwc


          Delete utils.html file.



          Put below code in utils.js:



          // mortage.js
          const getTermOptions = () =>
          return [
          label: '20 years', value: 20 ,
          label: '25 years', value: 25 ,
          ];
          ;

          const calculateMonthlyPayment = (principal, years, rate) =>
          // Logic
          ;

          export getTermOptions, calculateMonthlyPayment ;


          Now deploy this module:



          sfdx force:source:deploy -p force-app/main/default/lwc/utils


          Now, in your component cmp1 which is already created and deployed, import above utils module like below:



          import getTermOptions, calculateMonthlyPayment from 'c/utils';


          Now, you can use getTermOptions() inside the class of cmp1.js file.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 9 hours ago

























          answered 9 hours ago









          salesforce-sassalesforce-sas

          6,1011 gold badge2 silver badges23 bronze badges




          6,1011 gold badge2 silver badges23 bronze badges


























              2














              you need to create service component (component without *.html file).



              Here is the example from documentation.



              Your utils file.



              // mortage.js
              const getTermOptions = () =>
              return [
              label: '20 years', value: 20 ,
              label: '25 years', value: 25 ,
              ];
              ;

              const calculateMonthlyPayment = (principal, years, rate) =>
              // Logic
              ;

              export getTermOptions, calculateMonthlyPayment ;


              Your import statement in cmp1 or cmp2.



              import getTermOptions, calculateMonthlyPayment from 'c/mortgage';


              For more information follow LWC documentation provided earlier and spec for import statements.



              Hopefully, that will help.






              share|improve this answer































                2














                you need to create service component (component without *.html file).



                Here is the example from documentation.



                Your utils file.



                // mortage.js
                const getTermOptions = () =>
                return [
                label: '20 years', value: 20 ,
                label: '25 years', value: 25 ,
                ];
                ;

                const calculateMonthlyPayment = (principal, years, rate) =>
                // Logic
                ;

                export getTermOptions, calculateMonthlyPayment ;


                Your import statement in cmp1 or cmp2.



                import getTermOptions, calculateMonthlyPayment from 'c/mortgage';


                For more information follow LWC documentation provided earlier and spec for import statements.



                Hopefully, that will help.






                share|improve this answer





























                  2












                  2








                  2







                  you need to create service component (component without *.html file).



                  Here is the example from documentation.



                  Your utils file.



                  // mortage.js
                  const getTermOptions = () =>
                  return [
                  label: '20 years', value: 20 ,
                  label: '25 years', value: 25 ,
                  ];
                  ;

                  const calculateMonthlyPayment = (principal, years, rate) =>
                  // Logic
                  ;

                  export getTermOptions, calculateMonthlyPayment ;


                  Your import statement in cmp1 or cmp2.



                  import getTermOptions, calculateMonthlyPayment from 'c/mortgage';


                  For more information follow LWC documentation provided earlier and spec for import statements.



                  Hopefully, that will help.






                  share|improve this answer















                  you need to create service component (component without *.html file).



                  Here is the example from documentation.



                  Your utils file.



                  // mortage.js
                  const getTermOptions = () =>
                  return [
                  label: '20 years', value: 20 ,
                  label: '25 years', value: 25 ,
                  ];
                  ;

                  const calculateMonthlyPayment = (principal, years, rate) =>
                  // Logic
                  ;

                  export getTermOptions, calculateMonthlyPayment ;


                  Your import statement in cmp1 or cmp2.



                  import getTermOptions, calculateMonthlyPayment from 'c/mortgage';


                  For more information follow LWC documentation provided earlier and spec for import statements.



                  Hopefully, that will help.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 9 hours ago

























                  answered 9 hours ago









                  ytiqytiq

                  5833 silver badges18 bronze badges




                  5833 silver badges18 bronze badges






























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Salesforce Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f274261%2fimporting-es6-module-in-lwc-project-sfdx%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Sahara Skak | Bilen | Luke uk diar | NawigatsjuunCommonskategorii: SaharaWikivoyage raisfeerer: Sahara26° N, 13° O

                      The fall designs the understood secretary. Looking glass Science Shock Discovery Hot Everybody Loves Raymond Smile 곳 서비스 성실하다 Defas Kaloolon Definition: To combine or impregnate with sulphur or any of its compounds as to sulphurize caoutchouc in vulcanizing Flame colored Reason Useful Thin Help 갖다 유명하다 낙엽 장례식 Country Iron Definition: A fencer a gladiator one who exhibits his skill in the use of the sword Definition: The American black throated bunting Spiza Americana Nostalgic Needy Method to my madness 시키다 평가되다 전부 소설가 우아하다 Argument Tin Feeling Representative Gym Music Gaur Chicken 일쑤 코치 편 학생증 The harbor values the sugar. Vasagle Yammoe Enstatite Definition: Capable of being limited Road Neighborly Five Refer Built Kangaroo 비비다 Degree Release Bargain Horse 하루 형님 유교 석 동부 괴롭히다 경제력

                      19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу