130

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.

Ricky Ruiz
  • 25,455
  • 6
  • 44
  • 53
doberkofler
  • 9,511
  • 18
  • 74
  • 126

8 Answers8

198

Version Installed:

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

How to do it programmatically?

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);

Latest version available:

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
Ricky Ruiz
  • 25,455
  • 6
  • 44
  • 53
  • 3
    So webpack has no API that returns it's version and I have to use a shell command? – doberkofler Jan 28 '17 at 08:05
  • 1
    this works fine via the cli.... but how would one do this programmatically? (aka: inside a script file) – Maurice Mar 10 '17 at 01:18
  • I have included a way to programmatically extract webpack's version and use it as a global constant inside any file. – Ricky Ruiz Apr 20 '17 at 02:05
  • well, I had to type `npx webpack --version` – Paolo Sep 09 '18 at 16:48
  • @doberkofler has the (now correct) answer, and it's even marked as the right answer! https://stackoverflow.com/a/52143627/3221576 You can't rely on `webpack -v` because that assumes your current shell's `webpack` is the same as what's running in your program. – KCE Oct 25 '19 at 14:19
23

For those who are using yarn

yarn list webpack will do the trick

$ yarn list webpack
yarn list v0.27.5
└─ webpack@2.6.1
Done in 1.24s.
gates
  • 4,465
  • 7
  • 32
  • 60
23

Just another way not mentioned yet:

If you installed it locally to a project then open up the node_modules folder and check your webpack module.

< /node_modules/webpack/package.json

Open the package.json file and look under version

enter image description here

Isaac Pak
  • 4,467
  • 3
  • 42
  • 48
9

webpack 4 now offers a version property that can be used!

doberkofler
  • 9,511
  • 18
  • 74
  • 126
  • 3
    What this means is that wherever you have the `webpack` variable provided (like in your setup/config file) you can just call `webpack.version` and you'll get back a version string of the form `"x.y.z"`. – KCE Oct 25 '19 at 14:17
9

If using Angular CLI v7+, the webpack version is printed in the output of ng version:

-> ng version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 7.0.6
Node: 11.0.0
OS: darwin x64
Angular: 7.1.0
... animations, cdk, common, compiler, compiler-cli, core, forms
... http, language-service, material, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.10.6
@angular-devkit/build-angular     0.10.6
@angular-devkit/build-optimizer   0.10.6
@angular-devkit/build-webpack     0.10.6
@angular-devkit/core              7.0.6
@angular-devkit/schematics        7.0.6
@angular/cli                      7.0.6
@ngtools/webpack                  7.0.6
@schematics/angular               7.0.6
@schematics/update                0.10.6
rxjs                              6.3.3
typescript                        3.1.6
webpack                           4.19.1
trebor
  • 903
  • 1
  • 12
  • 23
  • 3
    In angular 10, webpack is not listed with `ng version`. If you're using custom webpack config with `@angular-builders/custom-webpack`, try `npm list webpack`, it showed me the used version of webpack. – Tony Dec 05 '20 at 09:23
5

In CLI

$ webpack --version
    
webpack-cli 4.1.0
    
webpack 5.3.2

In Code (node runtime)

process.env.npm_package_devDependencies_webpack // ^5.3.2

or

process.env.npm_package_dependencies_webpack // ^5.3.2

In Plugin

compiler.webpack.version // 5.3.2
Ahmed Kamal
  • 2,660
  • 3
  • 21
  • 36
4

Put webpack -v into your package.json:

{
  "name": "js",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack -v",
    "dev": "webpack --watch"
  }
}

Then enter in the console:

npm run build

Expected output should look like:

> npm run build

> js@1.0.0 build /home/user/repositories/myproject/js
> webpack -v

4.42.0
Sergey Chepurnov
  • 1,397
  • 14
  • 23
-2

npm webpack --version

works for me and it throws something like:

PS C:\Users\alfon\OneDrive\Escritorio\sandbox\fimusicv4\with-redux-app> npm webpack --version npm WARN config global --global, --local are deprecated. Use --location=global instead. 8.5.5

Fo Nko
  • 620
  • 10
  • 22