ES6 export of a script











up vote
0
down vote

favorite












I'm using D3 v5 in React and has a visualization that uses v3. I'm using npm modules for the v5 but for v3, I'm trying to import D3 to be used in that visualization.



In my component I have:



import * as d3 from './lib/d3/v3/d3';


and in the d3 folder's d3.js, the minified v3 d3 script:



const d3 = !function(){function n(n){return n&&(n.ownerDocument||n....

export default d3;


butI get the following error:



Failed to compile.



./src/components/d3/NetworkTopology/D3_NetworkTopology.js
Attempted import error: 'behavior' is not exported from './lib/d3/v3/d3' (imported as 'd3').


NetworkTopology.js:



  ...
const zoom = d3.behavior
.zoom()


"behavior" was removed in v4 so to me it looks like this is pulling the D3 V5 version instead of V3. How can I set this up to use both versions of D3?



I changed the import to:



import d3 from './lib/d3/v3/d3';


but got these compile errors:
Failed to compile.



./src/components/d3/NetworkTopology/lib/d3/v3/d3.js



  Line 1:   Expected an assignment or function call and instead saw an expression  no-unused-expressions


and I get the same error with the original import * as d3 from './lib/d3/v3/d3'; combined with removing the const and export in the d3.js (just use the original minified file).










share|improve this question
























  • I am not sure if a question I asked earlier helps. Try not exporting d3 and just use it because it seems to be a global variable for all d3 components.
    – jrook
    Nov 7 at 20:26










  • @jrook, I tried that but get a bunch of compile errors (updated the question at the end.)
    – user994165
    Nov 7 at 21:40















up vote
0
down vote

favorite












I'm using D3 v5 in React and has a visualization that uses v3. I'm using npm modules for the v5 but for v3, I'm trying to import D3 to be used in that visualization.



In my component I have:



import * as d3 from './lib/d3/v3/d3';


and in the d3 folder's d3.js, the minified v3 d3 script:



const d3 = !function(){function n(n){return n&&(n.ownerDocument||n....

export default d3;


butI get the following error:



Failed to compile.



./src/components/d3/NetworkTopology/D3_NetworkTopology.js
Attempted import error: 'behavior' is not exported from './lib/d3/v3/d3' (imported as 'd3').


NetworkTopology.js:



  ...
const zoom = d3.behavior
.zoom()


"behavior" was removed in v4 so to me it looks like this is pulling the D3 V5 version instead of V3. How can I set this up to use both versions of D3?



I changed the import to:



import d3 from './lib/d3/v3/d3';


but got these compile errors:
Failed to compile.



./src/components/d3/NetworkTopology/lib/d3/v3/d3.js



  Line 1:   Expected an assignment or function call and instead saw an expression  no-unused-expressions


and I get the same error with the original import * as d3 from './lib/d3/v3/d3'; combined with removing the const and export in the d3.js (just use the original minified file).










share|improve this question
























  • I am not sure if a question I asked earlier helps. Try not exporting d3 and just use it because it seems to be a global variable for all d3 components.
    – jrook
    Nov 7 at 20:26










  • @jrook, I tried that but get a bunch of compile errors (updated the question at the end.)
    – user994165
    Nov 7 at 21:40













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm using D3 v5 in React and has a visualization that uses v3. I'm using npm modules for the v5 but for v3, I'm trying to import D3 to be used in that visualization.



In my component I have:



import * as d3 from './lib/d3/v3/d3';


and in the d3 folder's d3.js, the minified v3 d3 script:



const d3 = !function(){function n(n){return n&&(n.ownerDocument||n....

export default d3;


butI get the following error:



Failed to compile.



./src/components/d3/NetworkTopology/D3_NetworkTopology.js
Attempted import error: 'behavior' is not exported from './lib/d3/v3/d3' (imported as 'd3').


NetworkTopology.js:



  ...
const zoom = d3.behavior
.zoom()


"behavior" was removed in v4 so to me it looks like this is pulling the D3 V5 version instead of V3. How can I set this up to use both versions of D3?



I changed the import to:



import d3 from './lib/d3/v3/d3';


but got these compile errors:
Failed to compile.



./src/components/d3/NetworkTopology/lib/d3/v3/d3.js



  Line 1:   Expected an assignment or function call and instead saw an expression  no-unused-expressions


and I get the same error with the original import * as d3 from './lib/d3/v3/d3'; combined with removing the const and export in the d3.js (just use the original minified file).










share|improve this question















I'm using D3 v5 in React and has a visualization that uses v3. I'm using npm modules for the v5 but for v3, I'm trying to import D3 to be used in that visualization.



In my component I have:



import * as d3 from './lib/d3/v3/d3';


and in the d3 folder's d3.js, the minified v3 d3 script:



const d3 = !function(){function n(n){return n&&(n.ownerDocument||n....

export default d3;


butI get the following error:



Failed to compile.



./src/components/d3/NetworkTopology/D3_NetworkTopology.js
Attempted import error: 'behavior' is not exported from './lib/d3/v3/d3' (imported as 'd3').


NetworkTopology.js:



  ...
const zoom = d3.behavior
.zoom()


"behavior" was removed in v4 so to me it looks like this is pulling the D3 V5 version instead of V3. How can I set this up to use both versions of D3?



I changed the import to:



import d3 from './lib/d3/v3/d3';


but got these compile errors:
Failed to compile.



./src/components/d3/NetworkTopology/lib/d3/v3/d3.js



  Line 1:   Expected an assignment or function call and instead saw an expression  no-unused-expressions


and I get the same error with the original import * as d3 from './lib/d3/v3/d3'; combined with removing the const and export in the d3.js (just use the original minified file).







javascript reactjs d3.js ecmascript-6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 23:00

























asked Nov 7 at 20:22









user994165

3,6231763105




3,6231763105












  • I am not sure if a question I asked earlier helps. Try not exporting d3 and just use it because it seems to be a global variable for all d3 components.
    – jrook
    Nov 7 at 20:26










  • @jrook, I tried that but get a bunch of compile errors (updated the question at the end.)
    – user994165
    Nov 7 at 21:40


















  • I am not sure if a question I asked earlier helps. Try not exporting d3 and just use it because it seems to be a global variable for all d3 components.
    – jrook
    Nov 7 at 20:26










  • @jrook, I tried that but get a bunch of compile errors (updated the question at the end.)
    – user994165
    Nov 7 at 21:40
















I am not sure if a question I asked earlier helps. Try not exporting d3 and just use it because it seems to be a global variable for all d3 components.
– jrook
Nov 7 at 20:26




I am not sure if a question I asked earlier helps. Try not exporting d3 and just use it because it seems to be a global variable for all d3 components.
– jrook
Nov 7 at 20:26












@jrook, I tried that but get a bunch of compile errors (updated the question at the end.)
– user994165
Nov 7 at 21:40




@jrook, I tried that but get a bunch of compile errors (updated the question at the end.)
– user994165
Nov 7 at 21:40












3 Answers
3






active

oldest

votes

















up vote
0
down vote













I don't think this has anything to do with the versions of d3. Your problem is that your script with the minified library has a single export default d3 (an export with the name default bound to a constant with the value of d3), while your component does a namespace import * as d3 which means that d3 will be a module object containing all exports - in your case, only the default one. So d3.default.behaviour could work. But you should just change your import to not use namespace objects but instead have the single value imported directly:



import d3 from './lib/d3/v3/d3';
// which is short for
import { default as d3 } from './lib/d3/v3/d3';





share|improve this answer





















  • I switched it but I got a compile error now (question updated at the end.)
    – user994165
    Nov 7 at 21:28










  • @user994165 Not sure about that, but it doesn't seem to be related to the imports. Might be a problem with running a linter on a minified script?
    – Bergi
    Nov 7 at 22:06


















up vote
0
down vote













you should update



    import d3 from './lib/d3/v3/d3';





share|improve this answer





















  • I switched it but I got a compile error now (question updated at the end.)
    – user994165
    Nov 7 at 21:28










  • Please support your answer with some explanation.
    – Dharman
    Nov 7 at 23:13










  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – Dharman
    Nov 7 at 23:13


















up vote
0
down vote



accepted










While the previous answers got me around the original error in the top portion of my question, by using:




import d3 from './lib/d3/v3/d3




, I ended up getting ES-Lint errors, which I got around by adding




/* eslint-disable */




Howevever, I then got an error caused by Babel inserting "use strict" on the D3 minified code. Since Create React App doesn't allow modification of the Babel configuration without ejecting the application, I was stuck. I thought about putting in a script in the template or an AJAX call to get the D3 script from a CDN, but I'm not sure it would have worked with the two D3 versions in the same app.



What eventually worked was the following. I cloned the earlier version of D3 into a directory at my app's root, then I modified the name in its package.json to be 'd3-v3' and renamed the directory to 'd3-v3'. Scoped npm package naming would have been preferable for the package name.



Then I did a:



npm install file:./d3-v3


From my React component I used the following for D3 v3



import * as d3 from 'd3-v3';


and for v5 used:



import * as d3 from 'd3';





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53197237%2fes6-export-of-a-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I don't think this has anything to do with the versions of d3. Your problem is that your script with the minified library has a single export default d3 (an export with the name default bound to a constant with the value of d3), while your component does a namespace import * as d3 which means that d3 will be a module object containing all exports - in your case, only the default one. So d3.default.behaviour could work. But you should just change your import to not use namespace objects but instead have the single value imported directly:



    import d3 from './lib/d3/v3/d3';
    // which is short for
    import { default as d3 } from './lib/d3/v3/d3';





    share|improve this answer





















    • I switched it but I got a compile error now (question updated at the end.)
      – user994165
      Nov 7 at 21:28










    • @user994165 Not sure about that, but it doesn't seem to be related to the imports. Might be a problem with running a linter on a minified script?
      – Bergi
      Nov 7 at 22:06















    up vote
    0
    down vote













    I don't think this has anything to do with the versions of d3. Your problem is that your script with the minified library has a single export default d3 (an export with the name default bound to a constant with the value of d3), while your component does a namespace import * as d3 which means that d3 will be a module object containing all exports - in your case, only the default one. So d3.default.behaviour could work. But you should just change your import to not use namespace objects but instead have the single value imported directly:



    import d3 from './lib/d3/v3/d3';
    // which is short for
    import { default as d3 } from './lib/d3/v3/d3';





    share|improve this answer





















    • I switched it but I got a compile error now (question updated at the end.)
      – user994165
      Nov 7 at 21:28










    • @user994165 Not sure about that, but it doesn't seem to be related to the imports. Might be a problem with running a linter on a minified script?
      – Bergi
      Nov 7 at 22:06













    up vote
    0
    down vote










    up vote
    0
    down vote









    I don't think this has anything to do with the versions of d3. Your problem is that your script with the minified library has a single export default d3 (an export with the name default bound to a constant with the value of d3), while your component does a namespace import * as d3 which means that d3 will be a module object containing all exports - in your case, only the default one. So d3.default.behaviour could work. But you should just change your import to not use namespace objects but instead have the single value imported directly:



    import d3 from './lib/d3/v3/d3';
    // which is short for
    import { default as d3 } from './lib/d3/v3/d3';





    share|improve this answer












    I don't think this has anything to do with the versions of d3. Your problem is that your script with the minified library has a single export default d3 (an export with the name default bound to a constant with the value of d3), while your component does a namespace import * as d3 which means that d3 will be a module object containing all exports - in your case, only the default one. So d3.default.behaviour could work. But you should just change your import to not use namespace objects but instead have the single value imported directly:



    import d3 from './lib/d3/v3/d3';
    // which is short for
    import { default as d3 } from './lib/d3/v3/d3';






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 7 at 20:42









    Bergi

    358k55531847




    358k55531847












    • I switched it but I got a compile error now (question updated at the end.)
      – user994165
      Nov 7 at 21:28










    • @user994165 Not sure about that, but it doesn't seem to be related to the imports. Might be a problem with running a linter on a minified script?
      – Bergi
      Nov 7 at 22:06


















    • I switched it but I got a compile error now (question updated at the end.)
      – user994165
      Nov 7 at 21:28










    • @user994165 Not sure about that, but it doesn't seem to be related to the imports. Might be a problem with running a linter on a minified script?
      – Bergi
      Nov 7 at 22:06
















    I switched it but I got a compile error now (question updated at the end.)
    – user994165
    Nov 7 at 21:28




    I switched it but I got a compile error now (question updated at the end.)
    – user994165
    Nov 7 at 21:28












    @user994165 Not sure about that, but it doesn't seem to be related to the imports. Might be a problem with running a linter on a minified script?
    – Bergi
    Nov 7 at 22:06




    @user994165 Not sure about that, but it doesn't seem to be related to the imports. Might be a problem with running a linter on a minified script?
    – Bergi
    Nov 7 at 22:06












    up vote
    0
    down vote













    you should update



        import d3 from './lib/d3/v3/d3';





    share|improve this answer





















    • I switched it but I got a compile error now (question updated at the end.)
      – user994165
      Nov 7 at 21:28










    • Please support your answer with some explanation.
      – Dharman
      Nov 7 at 23:13










    • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
      – Dharman
      Nov 7 at 23:13















    up vote
    0
    down vote













    you should update



        import d3 from './lib/d3/v3/d3';





    share|improve this answer





















    • I switched it but I got a compile error now (question updated at the end.)
      – user994165
      Nov 7 at 21:28










    • Please support your answer with some explanation.
      – Dharman
      Nov 7 at 23:13










    • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
      – Dharman
      Nov 7 at 23:13













    up vote
    0
    down vote










    up vote
    0
    down vote









    you should update



        import d3 from './lib/d3/v3/d3';





    share|improve this answer












    you should update



        import d3 from './lib/d3/v3/d3';






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 7 at 20:48









    yasin

    263




    263












    • I switched it but I got a compile error now (question updated at the end.)
      – user994165
      Nov 7 at 21:28










    • Please support your answer with some explanation.
      – Dharman
      Nov 7 at 23:13










    • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
      – Dharman
      Nov 7 at 23:13


















    • I switched it but I got a compile error now (question updated at the end.)
      – user994165
      Nov 7 at 21:28










    • Please support your answer with some explanation.
      – Dharman
      Nov 7 at 23:13










    • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
      – Dharman
      Nov 7 at 23:13
















    I switched it but I got a compile error now (question updated at the end.)
    – user994165
    Nov 7 at 21:28




    I switched it but I got a compile error now (question updated at the end.)
    – user994165
    Nov 7 at 21:28












    Please support your answer with some explanation.
    – Dharman
    Nov 7 at 23:13




    Please support your answer with some explanation.
    – Dharman
    Nov 7 at 23:13












    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – Dharman
    Nov 7 at 23:13




    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – Dharman
    Nov 7 at 23:13










    up vote
    0
    down vote



    accepted










    While the previous answers got me around the original error in the top portion of my question, by using:




    import d3 from './lib/d3/v3/d3




    , I ended up getting ES-Lint errors, which I got around by adding




    /* eslint-disable */




    Howevever, I then got an error caused by Babel inserting "use strict" on the D3 minified code. Since Create React App doesn't allow modification of the Babel configuration without ejecting the application, I was stuck. I thought about putting in a script in the template or an AJAX call to get the D3 script from a CDN, but I'm not sure it would have worked with the two D3 versions in the same app.



    What eventually worked was the following. I cloned the earlier version of D3 into a directory at my app's root, then I modified the name in its package.json to be 'd3-v3' and renamed the directory to 'd3-v3'. Scoped npm package naming would have been preferable for the package name.



    Then I did a:



    npm install file:./d3-v3


    From my React component I used the following for D3 v3



    import * as d3 from 'd3-v3';


    and for v5 used:



    import * as d3 from 'd3';





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      While the previous answers got me around the original error in the top portion of my question, by using:




      import d3 from './lib/d3/v3/d3




      , I ended up getting ES-Lint errors, which I got around by adding




      /* eslint-disable */




      Howevever, I then got an error caused by Babel inserting "use strict" on the D3 minified code. Since Create React App doesn't allow modification of the Babel configuration without ejecting the application, I was stuck. I thought about putting in a script in the template or an AJAX call to get the D3 script from a CDN, but I'm not sure it would have worked with the two D3 versions in the same app.



      What eventually worked was the following. I cloned the earlier version of D3 into a directory at my app's root, then I modified the name in its package.json to be 'd3-v3' and renamed the directory to 'd3-v3'. Scoped npm package naming would have been preferable for the package name.



      Then I did a:



      npm install file:./d3-v3


      From my React component I used the following for D3 v3



      import * as d3 from 'd3-v3';


      and for v5 used:



      import * as d3 from 'd3';





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        While the previous answers got me around the original error in the top portion of my question, by using:




        import d3 from './lib/d3/v3/d3




        , I ended up getting ES-Lint errors, which I got around by adding




        /* eslint-disable */




        Howevever, I then got an error caused by Babel inserting "use strict" on the D3 minified code. Since Create React App doesn't allow modification of the Babel configuration without ejecting the application, I was stuck. I thought about putting in a script in the template or an AJAX call to get the D3 script from a CDN, but I'm not sure it would have worked with the two D3 versions in the same app.



        What eventually worked was the following. I cloned the earlier version of D3 into a directory at my app's root, then I modified the name in its package.json to be 'd3-v3' and renamed the directory to 'd3-v3'. Scoped npm package naming would have been preferable for the package name.



        Then I did a:



        npm install file:./d3-v3


        From my React component I used the following for D3 v3



        import * as d3 from 'd3-v3';


        and for v5 used:



        import * as d3 from 'd3';





        share|improve this answer












        While the previous answers got me around the original error in the top portion of my question, by using:




        import d3 from './lib/d3/v3/d3




        , I ended up getting ES-Lint errors, which I got around by adding




        /* eslint-disable */




        Howevever, I then got an error caused by Babel inserting "use strict" on the D3 minified code. Since Create React App doesn't allow modification of the Babel configuration without ejecting the application, I was stuck. I thought about putting in a script in the template or an AJAX call to get the D3 script from a CDN, but I'm not sure it would have worked with the two D3 versions in the same app.



        What eventually worked was the following. I cloned the earlier version of D3 into a directory at my app's root, then I modified the name in its package.json to be 'd3-v3' and renamed the directory to 'd3-v3'. Scoped npm package naming would have been preferable for the package name.



        Then I did a:



        npm install file:./d3-v3


        From my React component I used the following for D3 v3



        import * as d3 from 'd3-v3';


        and for v5 used:



        import * as d3 from 'd3';






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 23:11









        user994165

        3,6231763105




        3,6231763105






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197237%2fes6-export-of-a-script%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







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud