Vue cli 3 display info from the package.json










1















In a vue cli 3 project I want to display a version number in the webpage. The version number lies in the package.json file.



The only reference to this that I found is this link in the vue forum.



However, I can't get the proposed solution to work.



Things I tried



  1. Use webpack.definePlugin as in the linked resource:


vue.config.js



const webpack = require('webpack');

module.exports =
lintOnSave: true,

configureWebpack: config =>
return
plugins: [
new webpack.DefinePlugin(
'process.env':
VERSION: require('./package.json').version,

)
]

,



Then in main.ts I read process.env, but it does not contain VERSION (I tried several variants to this, like generating a PACKAGE_JSON field like in the linked page, and generating plain values like 'foo' instead of reading from package-json). It never worked, it is like the code is being ignored. I guess the process.env is being redefined later by vue webpack stuff.



The process log in main.ts contains, however, all the stuff that process usually contains in a vue-cli project, like the mode and the VUE_APP variables defined in .env files.



  1. Try to write to process right on the configure webpack function,


like:



 configureWebpack: config => 
process.VERSION = require('./package.json').version
,


(to be honest I did not have much hope with this, but had to try).



  1. Tried the other solution proposed in the linked page,


like:



// vue.config.js
module.exports =
chainWebpack: config =>
config.plugin('define').tap( ([options = ]) =>
return [
...options, // these are the env variables from your .env file, if any arr defined
VERSION: JSON.stringify(require('./package.json').version)
]
)




But this fail silently too.



  1. Use the config.plugin('define') syntax suggested by @Oluwafemi,


like:



chainWebpack: (config) => 
return config.plugin('define').tap(
args => merge(args, [VERSION])
)
,


Where VERSION is a local variable defined as:



const pkgVersion = require('./package.json').version;

const VERSION =
'process.env':
VUE_APP_VERSION: JSON.stringify(pkgVersion)




But this is not working either.




I am re-starting the whole project everytime, so that's not the reason why the process stuff does not show up.



My vue-cli version is 3.0.1.










share|improve this question
























  • Have you tried process.VERSION = JSON.parse(require('./package.json')).version?

    – connexo
    Nov 14 '18 at 19:50











  • Yeah, tried that too. But VERSION does not show up in process anyway.

    – Sergeon
    Nov 14 '18 at 19:54















1















In a vue cli 3 project I want to display a version number in the webpage. The version number lies in the package.json file.



The only reference to this that I found is this link in the vue forum.



However, I can't get the proposed solution to work.



Things I tried



  1. Use webpack.definePlugin as in the linked resource:


vue.config.js



const webpack = require('webpack');

module.exports =
lintOnSave: true,

configureWebpack: config =>
return
plugins: [
new webpack.DefinePlugin(
'process.env':
VERSION: require('./package.json').version,

)
]

,



Then in main.ts I read process.env, but it does not contain VERSION (I tried several variants to this, like generating a PACKAGE_JSON field like in the linked page, and generating plain values like 'foo' instead of reading from package-json). It never worked, it is like the code is being ignored. I guess the process.env is being redefined later by vue webpack stuff.



The process log in main.ts contains, however, all the stuff that process usually contains in a vue-cli project, like the mode and the VUE_APP variables defined in .env files.



  1. Try to write to process right on the configure webpack function,


like:



 configureWebpack: config => 
process.VERSION = require('./package.json').version
,


(to be honest I did not have much hope with this, but had to try).



  1. Tried the other solution proposed in the linked page,


like:



// vue.config.js
module.exports =
chainWebpack: config =>
config.plugin('define').tap( ([options = ]) =>
return [
...options, // these are the env variables from your .env file, if any arr defined
VERSION: JSON.stringify(require('./package.json').version)
]
)




But this fail silently too.



  1. Use the config.plugin('define') syntax suggested by @Oluwafemi,


like:



chainWebpack: (config) => 
return config.plugin('define').tap(
args => merge(args, [VERSION])
)
,


Where VERSION is a local variable defined as:



const pkgVersion = require('./package.json').version;

const VERSION =
'process.env':
VUE_APP_VERSION: JSON.stringify(pkgVersion)




But this is not working either.




I am re-starting the whole project everytime, so that's not the reason why the process stuff does not show up.



My vue-cli version is 3.0.1.










share|improve this question
























  • Have you tried process.VERSION = JSON.parse(require('./package.json')).version?

    – connexo
    Nov 14 '18 at 19:50











  • Yeah, tried that too. But VERSION does not show up in process anyway.

    – Sergeon
    Nov 14 '18 at 19:54













1












1








1








In a vue cli 3 project I want to display a version number in the webpage. The version number lies in the package.json file.



The only reference to this that I found is this link in the vue forum.



However, I can't get the proposed solution to work.



Things I tried



  1. Use webpack.definePlugin as in the linked resource:


vue.config.js



const webpack = require('webpack');

module.exports =
lintOnSave: true,

configureWebpack: config =>
return
plugins: [
new webpack.DefinePlugin(
'process.env':
VERSION: require('./package.json').version,

)
]

,



Then in main.ts I read process.env, but it does not contain VERSION (I tried several variants to this, like generating a PACKAGE_JSON field like in the linked page, and generating plain values like 'foo' instead of reading from package-json). It never worked, it is like the code is being ignored. I guess the process.env is being redefined later by vue webpack stuff.



The process log in main.ts contains, however, all the stuff that process usually contains in a vue-cli project, like the mode and the VUE_APP variables defined in .env files.



  1. Try to write to process right on the configure webpack function,


like:



 configureWebpack: config => 
process.VERSION = require('./package.json').version
,


(to be honest I did not have much hope with this, but had to try).



  1. Tried the other solution proposed in the linked page,


like:



// vue.config.js
module.exports =
chainWebpack: config =>
config.plugin('define').tap( ([options = ]) =>
return [
...options, // these are the env variables from your .env file, if any arr defined
VERSION: JSON.stringify(require('./package.json').version)
]
)




But this fail silently too.



  1. Use the config.plugin('define') syntax suggested by @Oluwafemi,


like:



chainWebpack: (config) => 
return config.plugin('define').tap(
args => merge(args, [VERSION])
)
,


Where VERSION is a local variable defined as:



const pkgVersion = require('./package.json').version;

const VERSION =
'process.env':
VUE_APP_VERSION: JSON.stringify(pkgVersion)




But this is not working either.




I am re-starting the whole project everytime, so that's not the reason why the process stuff does not show up.



My vue-cli version is 3.0.1.










share|improve this question
















In a vue cli 3 project I want to display a version number in the webpage. The version number lies in the package.json file.



The only reference to this that I found is this link in the vue forum.



However, I can't get the proposed solution to work.



Things I tried



  1. Use webpack.definePlugin as in the linked resource:


vue.config.js



const webpack = require('webpack');

module.exports =
lintOnSave: true,

configureWebpack: config =>
return
plugins: [
new webpack.DefinePlugin(
'process.env':
VERSION: require('./package.json').version,

)
]

,



Then in main.ts I read process.env, but it does not contain VERSION (I tried several variants to this, like generating a PACKAGE_JSON field like in the linked page, and generating plain values like 'foo' instead of reading from package-json). It never worked, it is like the code is being ignored. I guess the process.env is being redefined later by vue webpack stuff.



The process log in main.ts contains, however, all the stuff that process usually contains in a vue-cli project, like the mode and the VUE_APP variables defined in .env files.



  1. Try to write to process right on the configure webpack function,


like:



 configureWebpack: config => 
process.VERSION = require('./package.json').version
,


(to be honest I did not have much hope with this, but had to try).



  1. Tried the other solution proposed in the linked page,


like:



// vue.config.js
module.exports =
chainWebpack: config =>
config.plugin('define').tap( ([options = ]) =>
return [
...options, // these are the env variables from your .env file, if any arr defined
VERSION: JSON.stringify(require('./package.json').version)
]
)




But this fail silently too.



  1. Use the config.plugin('define') syntax suggested by @Oluwafemi,


like:



chainWebpack: (config) => 
return config.plugin('define').tap(
args => merge(args, [VERSION])
)
,


Where VERSION is a local variable defined as:



const pkgVersion = require('./package.json').version;

const VERSION =
'process.env':
VUE_APP_VERSION: JSON.stringify(pkgVersion)




But this is not working either.




I am re-starting the whole project everytime, so that's not the reason why the process stuff does not show up.



My vue-cli version is 3.0.1.







javascript vue.js webpack vuejs2 vue-cli-3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 8:50







Sergeon

















asked Nov 14 '18 at 19:39









SergeonSergeon

2,545818




2,545818












  • Have you tried process.VERSION = JSON.parse(require('./package.json')).version?

    – connexo
    Nov 14 '18 at 19:50











  • Yeah, tried that too. But VERSION does not show up in process anyway.

    – Sergeon
    Nov 14 '18 at 19:54

















  • Have you tried process.VERSION = JSON.parse(require('./package.json')).version?

    – connexo
    Nov 14 '18 at 19:50











  • Yeah, tried that too. But VERSION does not show up in process anyway.

    – Sergeon
    Nov 14 '18 at 19:54
















Have you tried process.VERSION = JSON.parse(require('./package.json')).version?

– connexo
Nov 14 '18 at 19:50





Have you tried process.VERSION = JSON.parse(require('./package.json')).version?

– connexo
Nov 14 '18 at 19:50













Yeah, tried that too. But VERSION does not show up in process anyway.

– Sergeon
Nov 14 '18 at 19:54





Yeah, tried that too. But VERSION does not show up in process anyway.

– Sergeon
Nov 14 '18 at 19:54












3 Answers
3






active

oldest

votes


















2














TLDR



The following snippet in the vue.config.js file will do the trick, and will allow you to access the version of your app as APPLICATION_VERSION:



module.exports = 
configureWebpack: config =>
return
plugins: [
new webpack.DefinePlugin(
'APPLICATION_VERSION': JSON.stringify(require('./package.json').version),
)
]

,



TIP:



Don't even try to add some key to process.env via webpack.definePlugin: it won't work as you probably expect.



 Why my previous efforts did not work



At the end, I solved the issue via webpack.DefinePlugin. The main issue I had is that the original solution I found was using definePlugin to write to a process.env.PACKAGE_JSON variable.



This suggests that definePlugin somehow allows to add variables to process or process.env, which is not the case. Whenever I did log process.env in the console, I didn't find the variables I was trying to push into process.env : so I though the definePlugin tech was not working.



Actually, what webpack.definePlugin does is to check for strings at compile time and change them to its value right on your code. So, if you define an ACME_VERSION variable via:



module.exports = 
lintOnSave: true,

configureWebpack: config =>
return
plugins: [
new webpack.DefinePlugin(
'ACME_VERSION': 111,
)
]

,



and then, in main.js you print console.log(ACME_VERSION), you will get 111 properly logged.



Now, however, this is just a string change at compile time. If instead of ACME_VERSION you try to define process.env.VUE_APP_ACME_VERSION...



when you log process.env the VUE_APP_ACME_VERSION key won't show up in the object. However, a raw console.log('process.env.VUE_APP_ACME_VERSION') will yield 111 as expected.



So, basically, original link and the proposed solutions were correct to some degree. However, nothing was really being added to the process object. I was logging proccess.env during my initial tries, so I didn't see anything working.



Now, however, since the process object is not being modified, I strongly suggest anyone trying to load variables to their vue app at compile time not to use it. Is misleading at best.






share|improve this answer






























    1














    When building the Vue app, environment variables that don't begin with the VUE_APP_ prefix are filtered out. NODE_ENV and BASE_URL environment variables are the exception.



    The above information applies when the environment variables are set prior to building the Vue app and not in this situation.



    In a situation where environment variables are set during the build, it's important to look at what Vue CLI is doing.



    The Vue CLI uses webpack.DefinePlugin to set environment variables using the object returned from the call to resolveClientEnv.



    resolveClientEnv returns




    'process.env':



    This means when configuring your environment variables at build time, you need to come upon a way to merge with the existing one.
    You need to perform a deep merge of both arrays, so that value for process.env key is an object containing keys from the resolved client environment and your keys.



    chainWebpack key in the default export for vue.config.js is just about one of the ways to get this done.



    The arguments passed to initialize the DefinePlugin can be merged with new environment variables that you like to configure using the underlying webpack-chain API. Here is an example:



    // vue.config.js

    const merge = require('deepmerge');
    const pkgVersion = require('./package.json').version;

    const VERSION =
    'process.env':
    VERSION: JSON.stringify(pkgVersion)



    module.exports =
    chainWebpack: config =>
    config
    .plugin('define')
    .tap(
    args => merge(args, [VERSION])
    )






    share|improve this answer

























    • This actually makes sense, because the webpack define plugin documentation (webpack.js.org/plugins/define-plugin) explicitly indicates to prefer this syntax. However, it did not work :-/

      – Sergeon
      Nov 14 '18 at 20:13











    • I have updated the answer with my recent findings.

      – Oluwafemi Sule
      Nov 14 '18 at 22:26











    • Thank you! I'll try tomorrow

      – Sergeon
      Nov 14 '18 at 22:32











    • Tried the deepmerge strategy, but it didn't work. I will try in a fresh new vue-cli 3 project, to check if some changes in my project are breaking the thing out.

      – Sergeon
      Nov 15 '18 at 8:52











    • Thank you, I solved the issue at the end and you answer helped me a lot.

      – Sergeon
      Nov 15 '18 at 9:42


















    0














    Your initial attempt was fine, you were just missing the JSON.stringify part:



    const webpack = require('webpack');

    module.exports =
    configureWebpack: config =>
    return
    plugins: [
    new webpack.DefinePlugin(
    'process.env':
    VERSION: JSON.stringify(require('./package.json').version),

    )
    ]

    ,



    Edit: although the webpack docs recommend the 'process.env.VERSION' way (yellow panel):



    new webpack.DefinePlugin(
    'process.env.VERSION': JSON.stringify(require('./package.json').version),
    ),





    share|improve this answer

























    • Thank you. As you say, JSON.stringify was needed for the whole thing to work: otherwise, you'll get an error when trying to print it. However, it was not the reason the VERSION key was not showing up in my process.env.

      – Sergeon
      Nov 15 '18 at 9:42










    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',
    autoActivateHeartbeat: false,
    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%2f53307636%2fvue-cli-3-display-info-from-the-package-json%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









    2














    TLDR



    The following snippet in the vue.config.js file will do the trick, and will allow you to access the version of your app as APPLICATION_VERSION:



    module.exports = 
    configureWebpack: config =>
    return
    plugins: [
    new webpack.DefinePlugin(
    'APPLICATION_VERSION': JSON.stringify(require('./package.json').version),
    )
    ]

    ,



    TIP:



    Don't even try to add some key to process.env via webpack.definePlugin: it won't work as you probably expect.



     Why my previous efforts did not work



    At the end, I solved the issue via webpack.DefinePlugin. The main issue I had is that the original solution I found was using definePlugin to write to a process.env.PACKAGE_JSON variable.



    This suggests that definePlugin somehow allows to add variables to process or process.env, which is not the case. Whenever I did log process.env in the console, I didn't find the variables I was trying to push into process.env : so I though the definePlugin tech was not working.



    Actually, what webpack.definePlugin does is to check for strings at compile time and change them to its value right on your code. So, if you define an ACME_VERSION variable via:



    module.exports = 
    lintOnSave: true,

    configureWebpack: config =>
    return
    plugins: [
    new webpack.DefinePlugin(
    'ACME_VERSION': 111,
    )
    ]

    ,



    and then, in main.js you print console.log(ACME_VERSION), you will get 111 properly logged.



    Now, however, this is just a string change at compile time. If instead of ACME_VERSION you try to define process.env.VUE_APP_ACME_VERSION...



    when you log process.env the VUE_APP_ACME_VERSION key won't show up in the object. However, a raw console.log('process.env.VUE_APP_ACME_VERSION') will yield 111 as expected.



    So, basically, original link and the proposed solutions were correct to some degree. However, nothing was really being added to the process object. I was logging proccess.env during my initial tries, so I didn't see anything working.



    Now, however, since the process object is not being modified, I strongly suggest anyone trying to load variables to their vue app at compile time not to use it. Is misleading at best.






    share|improve this answer



























      2














      TLDR



      The following snippet in the vue.config.js file will do the trick, and will allow you to access the version of your app as APPLICATION_VERSION:



      module.exports = 
      configureWebpack: config =>
      return
      plugins: [
      new webpack.DefinePlugin(
      'APPLICATION_VERSION': JSON.stringify(require('./package.json').version),
      )
      ]

      ,



      TIP:



      Don't even try to add some key to process.env via webpack.definePlugin: it won't work as you probably expect.



       Why my previous efforts did not work



      At the end, I solved the issue via webpack.DefinePlugin. The main issue I had is that the original solution I found was using definePlugin to write to a process.env.PACKAGE_JSON variable.



      This suggests that definePlugin somehow allows to add variables to process or process.env, which is not the case. Whenever I did log process.env in the console, I didn't find the variables I was trying to push into process.env : so I though the definePlugin tech was not working.



      Actually, what webpack.definePlugin does is to check for strings at compile time and change them to its value right on your code. So, if you define an ACME_VERSION variable via:



      module.exports = 
      lintOnSave: true,

      configureWebpack: config =>
      return
      plugins: [
      new webpack.DefinePlugin(
      'ACME_VERSION': 111,
      )
      ]

      ,



      and then, in main.js you print console.log(ACME_VERSION), you will get 111 properly logged.



      Now, however, this is just a string change at compile time. If instead of ACME_VERSION you try to define process.env.VUE_APP_ACME_VERSION...



      when you log process.env the VUE_APP_ACME_VERSION key won't show up in the object. However, a raw console.log('process.env.VUE_APP_ACME_VERSION') will yield 111 as expected.



      So, basically, original link and the proposed solutions were correct to some degree. However, nothing was really being added to the process object. I was logging proccess.env during my initial tries, so I didn't see anything working.



      Now, however, since the process object is not being modified, I strongly suggest anyone trying to load variables to their vue app at compile time not to use it. Is misleading at best.






      share|improve this answer

























        2












        2








        2







        TLDR



        The following snippet in the vue.config.js file will do the trick, and will allow you to access the version of your app as APPLICATION_VERSION:



        module.exports = 
        configureWebpack: config =>
        return
        plugins: [
        new webpack.DefinePlugin(
        'APPLICATION_VERSION': JSON.stringify(require('./package.json').version),
        )
        ]

        ,



        TIP:



        Don't even try to add some key to process.env via webpack.definePlugin: it won't work as you probably expect.



         Why my previous efforts did not work



        At the end, I solved the issue via webpack.DefinePlugin. The main issue I had is that the original solution I found was using definePlugin to write to a process.env.PACKAGE_JSON variable.



        This suggests that definePlugin somehow allows to add variables to process or process.env, which is not the case. Whenever I did log process.env in the console, I didn't find the variables I was trying to push into process.env : so I though the definePlugin tech was not working.



        Actually, what webpack.definePlugin does is to check for strings at compile time and change them to its value right on your code. So, if you define an ACME_VERSION variable via:



        module.exports = 
        lintOnSave: true,

        configureWebpack: config =>
        return
        plugins: [
        new webpack.DefinePlugin(
        'ACME_VERSION': 111,
        )
        ]

        ,



        and then, in main.js you print console.log(ACME_VERSION), you will get 111 properly logged.



        Now, however, this is just a string change at compile time. If instead of ACME_VERSION you try to define process.env.VUE_APP_ACME_VERSION...



        when you log process.env the VUE_APP_ACME_VERSION key won't show up in the object. However, a raw console.log('process.env.VUE_APP_ACME_VERSION') will yield 111 as expected.



        So, basically, original link and the proposed solutions were correct to some degree. However, nothing was really being added to the process object. I was logging proccess.env during my initial tries, so I didn't see anything working.



        Now, however, since the process object is not being modified, I strongly suggest anyone trying to load variables to their vue app at compile time not to use it. Is misleading at best.






        share|improve this answer













        TLDR



        The following snippet in the vue.config.js file will do the trick, and will allow you to access the version of your app as APPLICATION_VERSION:



        module.exports = 
        configureWebpack: config =>
        return
        plugins: [
        new webpack.DefinePlugin(
        'APPLICATION_VERSION': JSON.stringify(require('./package.json').version),
        )
        ]

        ,



        TIP:



        Don't even try to add some key to process.env via webpack.definePlugin: it won't work as you probably expect.



         Why my previous efforts did not work



        At the end, I solved the issue via webpack.DefinePlugin. The main issue I had is that the original solution I found was using definePlugin to write to a process.env.PACKAGE_JSON variable.



        This suggests that definePlugin somehow allows to add variables to process or process.env, which is not the case. Whenever I did log process.env in the console, I didn't find the variables I was trying to push into process.env : so I though the definePlugin tech was not working.



        Actually, what webpack.definePlugin does is to check for strings at compile time and change them to its value right on your code. So, if you define an ACME_VERSION variable via:



        module.exports = 
        lintOnSave: true,

        configureWebpack: config =>
        return
        plugins: [
        new webpack.DefinePlugin(
        'ACME_VERSION': 111,
        )
        ]

        ,



        and then, in main.js you print console.log(ACME_VERSION), you will get 111 properly logged.



        Now, however, this is just a string change at compile time. If instead of ACME_VERSION you try to define process.env.VUE_APP_ACME_VERSION...



        when you log process.env the VUE_APP_ACME_VERSION key won't show up in the object. However, a raw console.log('process.env.VUE_APP_ACME_VERSION') will yield 111 as expected.



        So, basically, original link and the proposed solutions were correct to some degree. However, nothing was really being added to the process object. I was logging proccess.env during my initial tries, so I didn't see anything working.



        Now, however, since the process object is not being modified, I strongly suggest anyone trying to load variables to their vue app at compile time not to use it. Is misleading at best.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 9:40









        SergeonSergeon

        2,545818




        2,545818























            1














            When building the Vue app, environment variables that don't begin with the VUE_APP_ prefix are filtered out. NODE_ENV and BASE_URL environment variables are the exception.



            The above information applies when the environment variables are set prior to building the Vue app and not in this situation.



            In a situation where environment variables are set during the build, it's important to look at what Vue CLI is doing.



            The Vue CLI uses webpack.DefinePlugin to set environment variables using the object returned from the call to resolveClientEnv.



            resolveClientEnv returns




            'process.env':



            This means when configuring your environment variables at build time, you need to come upon a way to merge with the existing one.
            You need to perform a deep merge of both arrays, so that value for process.env key is an object containing keys from the resolved client environment and your keys.



            chainWebpack key in the default export for vue.config.js is just about one of the ways to get this done.



            The arguments passed to initialize the DefinePlugin can be merged with new environment variables that you like to configure using the underlying webpack-chain API. Here is an example:



            // vue.config.js

            const merge = require('deepmerge');
            const pkgVersion = require('./package.json').version;

            const VERSION =
            'process.env':
            VERSION: JSON.stringify(pkgVersion)



            module.exports =
            chainWebpack: config =>
            config
            .plugin('define')
            .tap(
            args => merge(args, [VERSION])
            )






            share|improve this answer

























            • This actually makes sense, because the webpack define plugin documentation (webpack.js.org/plugins/define-plugin) explicitly indicates to prefer this syntax. However, it did not work :-/

              – Sergeon
              Nov 14 '18 at 20:13











            • I have updated the answer with my recent findings.

              – Oluwafemi Sule
              Nov 14 '18 at 22:26











            • Thank you! I'll try tomorrow

              – Sergeon
              Nov 14 '18 at 22:32











            • Tried the deepmerge strategy, but it didn't work. I will try in a fresh new vue-cli 3 project, to check if some changes in my project are breaking the thing out.

              – Sergeon
              Nov 15 '18 at 8:52











            • Thank you, I solved the issue at the end and you answer helped me a lot.

              – Sergeon
              Nov 15 '18 at 9:42















            1














            When building the Vue app, environment variables that don't begin with the VUE_APP_ prefix are filtered out. NODE_ENV and BASE_URL environment variables are the exception.



            The above information applies when the environment variables are set prior to building the Vue app and not in this situation.



            In a situation where environment variables are set during the build, it's important to look at what Vue CLI is doing.



            The Vue CLI uses webpack.DefinePlugin to set environment variables using the object returned from the call to resolveClientEnv.



            resolveClientEnv returns




            'process.env':



            This means when configuring your environment variables at build time, you need to come upon a way to merge with the existing one.
            You need to perform a deep merge of both arrays, so that value for process.env key is an object containing keys from the resolved client environment and your keys.



            chainWebpack key in the default export for vue.config.js is just about one of the ways to get this done.



            The arguments passed to initialize the DefinePlugin can be merged with new environment variables that you like to configure using the underlying webpack-chain API. Here is an example:



            // vue.config.js

            const merge = require('deepmerge');
            const pkgVersion = require('./package.json').version;

            const VERSION =
            'process.env':
            VERSION: JSON.stringify(pkgVersion)



            module.exports =
            chainWebpack: config =>
            config
            .plugin('define')
            .tap(
            args => merge(args, [VERSION])
            )






            share|improve this answer

























            • This actually makes sense, because the webpack define plugin documentation (webpack.js.org/plugins/define-plugin) explicitly indicates to prefer this syntax. However, it did not work :-/

              – Sergeon
              Nov 14 '18 at 20:13











            • I have updated the answer with my recent findings.

              – Oluwafemi Sule
              Nov 14 '18 at 22:26











            • Thank you! I'll try tomorrow

              – Sergeon
              Nov 14 '18 at 22:32











            • Tried the deepmerge strategy, but it didn't work. I will try in a fresh new vue-cli 3 project, to check if some changes in my project are breaking the thing out.

              – Sergeon
              Nov 15 '18 at 8:52











            • Thank you, I solved the issue at the end and you answer helped me a lot.

              – Sergeon
              Nov 15 '18 at 9:42













            1












            1








            1







            When building the Vue app, environment variables that don't begin with the VUE_APP_ prefix are filtered out. NODE_ENV and BASE_URL environment variables are the exception.



            The above information applies when the environment variables are set prior to building the Vue app and not in this situation.



            In a situation where environment variables are set during the build, it's important to look at what Vue CLI is doing.



            The Vue CLI uses webpack.DefinePlugin to set environment variables using the object returned from the call to resolveClientEnv.



            resolveClientEnv returns




            'process.env':



            This means when configuring your environment variables at build time, you need to come upon a way to merge with the existing one.
            You need to perform a deep merge of both arrays, so that value for process.env key is an object containing keys from the resolved client environment and your keys.



            chainWebpack key in the default export for vue.config.js is just about one of the ways to get this done.



            The arguments passed to initialize the DefinePlugin can be merged with new environment variables that you like to configure using the underlying webpack-chain API. Here is an example:



            // vue.config.js

            const merge = require('deepmerge');
            const pkgVersion = require('./package.json').version;

            const VERSION =
            'process.env':
            VERSION: JSON.stringify(pkgVersion)



            module.exports =
            chainWebpack: config =>
            config
            .plugin('define')
            .tap(
            args => merge(args, [VERSION])
            )






            share|improve this answer















            When building the Vue app, environment variables that don't begin with the VUE_APP_ prefix are filtered out. NODE_ENV and BASE_URL environment variables are the exception.



            The above information applies when the environment variables are set prior to building the Vue app and not in this situation.



            In a situation where environment variables are set during the build, it's important to look at what Vue CLI is doing.



            The Vue CLI uses webpack.DefinePlugin to set environment variables using the object returned from the call to resolveClientEnv.



            resolveClientEnv returns




            'process.env':



            This means when configuring your environment variables at build time, you need to come upon a way to merge with the existing one.
            You need to perform a deep merge of both arrays, so that value for process.env key is an object containing keys from the resolved client environment and your keys.



            chainWebpack key in the default export for vue.config.js is just about one of the ways to get this done.



            The arguments passed to initialize the DefinePlugin can be merged with new environment variables that you like to configure using the underlying webpack-chain API. Here is an example:



            // vue.config.js

            const merge = require('deepmerge');
            const pkgVersion = require('./package.json').version;

            const VERSION =
            'process.env':
            VERSION: JSON.stringify(pkgVersion)



            module.exports =
            chainWebpack: config =>
            config
            .plugin('define')
            .tap(
            args => merge(args, [VERSION])
            )







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 14 '18 at 22:48

























            answered Nov 14 '18 at 20:06









            Oluwafemi SuleOluwafemi Sule

            12.1k1533




            12.1k1533












            • This actually makes sense, because the webpack define plugin documentation (webpack.js.org/plugins/define-plugin) explicitly indicates to prefer this syntax. However, it did not work :-/

              – Sergeon
              Nov 14 '18 at 20:13











            • I have updated the answer with my recent findings.

              – Oluwafemi Sule
              Nov 14 '18 at 22:26











            • Thank you! I'll try tomorrow

              – Sergeon
              Nov 14 '18 at 22:32











            • Tried the deepmerge strategy, but it didn't work. I will try in a fresh new vue-cli 3 project, to check if some changes in my project are breaking the thing out.

              – Sergeon
              Nov 15 '18 at 8:52











            • Thank you, I solved the issue at the end and you answer helped me a lot.

              – Sergeon
              Nov 15 '18 at 9:42

















            • This actually makes sense, because the webpack define plugin documentation (webpack.js.org/plugins/define-plugin) explicitly indicates to prefer this syntax. However, it did not work :-/

              – Sergeon
              Nov 14 '18 at 20:13











            • I have updated the answer with my recent findings.

              – Oluwafemi Sule
              Nov 14 '18 at 22:26











            • Thank you! I'll try tomorrow

              – Sergeon
              Nov 14 '18 at 22:32











            • Tried the deepmerge strategy, but it didn't work. I will try in a fresh new vue-cli 3 project, to check if some changes in my project are breaking the thing out.

              – Sergeon
              Nov 15 '18 at 8:52











            • Thank you, I solved the issue at the end and you answer helped me a lot.

              – Sergeon
              Nov 15 '18 at 9:42
















            This actually makes sense, because the webpack define plugin documentation (webpack.js.org/plugins/define-plugin) explicitly indicates to prefer this syntax. However, it did not work :-/

            – Sergeon
            Nov 14 '18 at 20:13





            This actually makes sense, because the webpack define plugin documentation (webpack.js.org/plugins/define-plugin) explicitly indicates to prefer this syntax. However, it did not work :-/

            – Sergeon
            Nov 14 '18 at 20:13













            I have updated the answer with my recent findings.

            – Oluwafemi Sule
            Nov 14 '18 at 22:26





            I have updated the answer with my recent findings.

            – Oluwafemi Sule
            Nov 14 '18 at 22:26













            Thank you! I'll try tomorrow

            – Sergeon
            Nov 14 '18 at 22:32





            Thank you! I'll try tomorrow

            – Sergeon
            Nov 14 '18 at 22:32













            Tried the deepmerge strategy, but it didn't work. I will try in a fresh new vue-cli 3 project, to check if some changes in my project are breaking the thing out.

            – Sergeon
            Nov 15 '18 at 8:52





            Tried the deepmerge strategy, but it didn't work. I will try in a fresh new vue-cli 3 project, to check if some changes in my project are breaking the thing out.

            – Sergeon
            Nov 15 '18 at 8:52













            Thank you, I solved the issue at the end and you answer helped me a lot.

            – Sergeon
            Nov 15 '18 at 9:42





            Thank you, I solved the issue at the end and you answer helped me a lot.

            – Sergeon
            Nov 15 '18 at 9:42











            0














            Your initial attempt was fine, you were just missing the JSON.stringify part:



            const webpack = require('webpack');

            module.exports =
            configureWebpack: config =>
            return
            plugins: [
            new webpack.DefinePlugin(
            'process.env':
            VERSION: JSON.stringify(require('./package.json').version),

            )
            ]

            ,



            Edit: although the webpack docs recommend the 'process.env.VERSION' way (yellow panel):



            new webpack.DefinePlugin(
            'process.env.VERSION': JSON.stringify(require('./package.json').version),
            ),





            share|improve this answer

























            • Thank you. As you say, JSON.stringify was needed for the whole thing to work: otherwise, you'll get an error when trying to print it. However, it was not the reason the VERSION key was not showing up in my process.env.

              – Sergeon
              Nov 15 '18 at 9:42















            0














            Your initial attempt was fine, you were just missing the JSON.stringify part:



            const webpack = require('webpack');

            module.exports =
            configureWebpack: config =>
            return
            plugins: [
            new webpack.DefinePlugin(
            'process.env':
            VERSION: JSON.stringify(require('./package.json').version),

            )
            ]

            ,



            Edit: although the webpack docs recommend the 'process.env.VERSION' way (yellow panel):



            new webpack.DefinePlugin(
            'process.env.VERSION': JSON.stringify(require('./package.json').version),
            ),





            share|improve this answer

























            • Thank you. As you say, JSON.stringify was needed for the whole thing to work: otherwise, you'll get an error when trying to print it. However, it was not the reason the VERSION key was not showing up in my process.env.

              – Sergeon
              Nov 15 '18 at 9:42













            0












            0








            0







            Your initial attempt was fine, you were just missing the JSON.stringify part:



            const webpack = require('webpack');

            module.exports =
            configureWebpack: config =>
            return
            plugins: [
            new webpack.DefinePlugin(
            'process.env':
            VERSION: JSON.stringify(require('./package.json').version),

            )
            ]

            ,



            Edit: although the webpack docs recommend the 'process.env.VERSION' way (yellow panel):



            new webpack.DefinePlugin(
            'process.env.VERSION': JSON.stringify(require('./package.json').version),
            ),





            share|improve this answer















            Your initial attempt was fine, you were just missing the JSON.stringify part:



            const webpack = require('webpack');

            module.exports =
            configureWebpack: config =>
            return
            plugins: [
            new webpack.DefinePlugin(
            'process.env':
            VERSION: JSON.stringify(require('./package.json').version),

            )
            ]

            ,



            Edit: although the webpack docs recommend the 'process.env.VERSION' way (yellow panel):



            new webpack.DefinePlugin(
            'process.env.VERSION': JSON.stringify(require('./package.json').version),
            ),






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 14 '18 at 23:00

























            answered Nov 14 '18 at 22:50









            yuriy636yuriy636

            4,45721729




            4,45721729












            • Thank you. As you say, JSON.stringify was needed for the whole thing to work: otherwise, you'll get an error when trying to print it. However, it was not the reason the VERSION key was not showing up in my process.env.

              – Sergeon
              Nov 15 '18 at 9:42

















            • Thank you. As you say, JSON.stringify was needed for the whole thing to work: otherwise, you'll get an error when trying to print it. However, it was not the reason the VERSION key was not showing up in my process.env.

              – Sergeon
              Nov 15 '18 at 9:42
















            Thank you. As you say, JSON.stringify was needed for the whole thing to work: otherwise, you'll get an error when trying to print it. However, it was not the reason the VERSION key was not showing up in my process.env.

            – Sergeon
            Nov 15 '18 at 9:42





            Thank you. As you say, JSON.stringify was needed for the whole thing to work: otherwise, you'll get an error when trying to print it. However, it was not the reason the VERSION key was not showing up in my process.env.

            – Sergeon
            Nov 15 '18 at 9:42

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f53307636%2fvue-cli-3-display-info-from-the-package-json%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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto