Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the installed webpack version

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.

like image 341
doberkofler Avatar asked Jan 15 '17 13:01

doberkofler


People also ask

How do I update my webpack version?

Now let's upgrade webpack to version 5: npm: npm install webpack@latest. Yarn: yarn add webpack@latest.

What is the latest version of webpack CLI?

4.6. 0 (2021-03-27)

Is webpack part of npm?

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.


1 Answers

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 
like image 92
Ricky Avatar answered Sep 16 '22 15:09

Ricky