Especially during the transition from webpack v1 to v2, it would be important to programmatically determine what webpack version is installed, but I cannot seem to find the appropriate API.
Now let's upgrade webpack to version 5: npm: npm install webpack@latest. Yarn: yarn add webpack@latest.
4.6. 0 (2021-03-27)
August 7, 2020. Webpack is a static module bundler for JavaScript applications. It takes modules, whether that's a custom file that we created or something that was installed through NPM, and converts these modules to static assets.
Using webpack CLI: (--version, -v Show version number [boolean])
webpack --version
or:
webpack -v
Using npm list command:
npm list webpack
Results in name@version-range
:
<projectName>@<projectVersion> /path/to/project └── webpack@<version-range>
Using yarn list command:
yarn list webpack
Webpack 2 introduced Configuration Types.
Instead of exporting a configuration object, you may return a function which accepts an environment as argument. When running webpack, you may specify build environment keys via
--env
, such as--env.production
or--env.platform=web
.
We will use a build environment key called --env.version
.
webpack --env.version $(webpack --version)
or:
webpack --env.version $(webpack -v)
For this to work we will need to do two things:
Change our webpack.config.js
file and use DefinePlugin.
The DefinePlugin allows you to create global constants which can be configured at compile time.
-module.exports = { +module.exports = function(env) { + return { plugins: [ new webpack.DefinePlugin({ + WEBPACK_VERSION: JSON.stringify(env.version) //<version-range> }) ] + }; };
Now we can access the global constant like so:
console.log(WEBPACK_VERSION);
Using npm view command will return the latest version available on the registry:
npm view [<@scope>/]<name>[@<version>] [<field>[.<subfield>]...]
For webpack use:
npm view webpack version
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With