Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown argument: --env #1216

Closed
Namchee opened this issue Feb 11, 2020 · 29 comments
Closed

Unknown argument: --env #1216

Namchee opened this issue Feb 11, 2020 · 29 comments

Comments

@Namchee
Copy link

Namchee commented Feb 11, 2020

Describe the bug

I'm trying to set my webpack env for configuration purposes with

webpack --env.platform=node

But, webpack-cli keeps throwing ⬢ webpack: Unknown argument: --env error to me

What is the current behavior?

webpack-cli refuses to read the flag and throw an error instead

To Reproduce
Steps to reproduce the behavior:

Just try to add --env flag to any webpack command.

Expected behavior

webpack-cli should accept and set this variable correctly

Screenshots

error

Additional context

This works on 3.3.10. I'm currently using 4.0.0-beta.2

@lobsterkatie
Copy link

I am having the exact same problem with the mode flag. And yet the docs still include it, and the migration guide for webpack 4 -> 5 mentions it, so it's pretty clear it hasn't been removed... Very confused with this one.

@ematipico
Copy link
Contributor

ematipico commented Feb 12, 2020

Please don't install 4.0.0-beta.2 just yet. Unfortunately I published an incorrect version on npm, so sorry about that. Please keep using 3.3.10 or 3.3.11.

In 4.0.0-beta.2 we removed --mode and --env. If you feel you'd need that argument back, feel free to leave a feedback here

@robertlevy
Copy link

I'm seeing this issue in the 4.0.0 release

@anshumanv
Copy link
Member

Hi, @robertlevy we've changed the env flag format in v4 because we changed the arg parser, new docs will be released on official site soon. Meanwhile, you can use CLI docs from this deployment - https://vercel.com/webpack-docs/webpack-js-org/e6mjiydwu

@robertlevy
Copy link

@anshumanv thanks but that link is not working for me. could you paste a summary or working example?

@anshumanv
Copy link
Member

anshumanv commented Oct 11, 2020

@robertlevy ofcourse! You should get a good idea from this test - https://github.com/webpack/webpack-cli/blob/master/test/config/type/function-with-env/function-with-env.test.js

Let us know if you run into any problems 😄

@thealjey-eib
Copy link

thealjey-eib commented Oct 12, 2020

@anshumanv
so, env now only supports boolean values?
I need to be able to pass arbitrary strings from the command line.

@snitin315
Copy link
Member

but we cannot pass an arbitrary string from the command line?

@thealjey-eib You can pass any arbitrary string from the command line.

@thealjey-eib
Copy link

thealjey-eib commented Oct 12, 2020

@snitin315 how?

it used to be as simple as
webpack --env.app=foobar --env.title=qwerty
but, now this throws "Unknown argument" errors

this creates variables with names app=foobar and title=qwerty, and values true
webpack --env app=foobar --env title=qwerty

@anshumanv
Copy link
Member

anshumanv commented Oct 12, 2020

@thealjey-eib for now yes, this is how the behaviour was restored when it was removed.

You can use

webpack --env appFoobar --env titleQwerty

And use these variables to write conditional logic in your config, does it not cover your use case? If not, please give an example.

@fabianmarcus
Copy link

fabianmarcus commented Oct 12, 2020

@thealjey-eib for now yes, this is how the behaviour was restored when it was removed.

You can use

webpack --env appFoobar --env titleQwerty

And use these variables to write conditional logic in your config, does it not cover your use case? If not, please give an example.

I am used to do it this way:

npx webpack --mode production --config webpack.config.build.js --env.name="[staging|production]" --env.service="[login|flight|reservation|...]"

Now I get
[webpack-cli] Unknown argument: --env.service=staging
[webpack-cli] Unknown argument: --env.name=login.

What's the working equivalent in Webpack CLI 4.x and Webpack 5.x?

When I change the syntax to your example, the properties in env are just booleans:

npx webpack --mode production --config webpack.config.build.js --env name="[staging|production]" --env service="[login|flight|reservation|...]"

# Result in env
{ 'service=staging': true, 'name=login': true }

But that's not what I need.

@anshumanv
Copy link
Member

anshumanv commented Oct 12, 2020

Thanks for the examples, migration of both config and scripts to use booleans will add a considerable overhead and current format seems not the most verbose thing to work with. I've opened #1932 to track it, will PR soon

Since we've migrated our arg parser to commander so --env.name="staging" --env.service="login" won't be possible but we can do --env name=staging --env service=login Is that something you can work with?

@fabianmarcus
Copy link

fabianmarcus commented Oct 12, 2020

Is that something you can work with?

Yeah, of course. Thanks!

@thealjey-eib
Copy link

thealjey-eib commented Oct 12, 2020

@anshumanv my example is a multi-application project
I have an "applications" folder and I want to build any one of them by name.
Maybe env is not ideal, I do not know, but I have not been able to find a better way to pass custom variables into webpack.config.js
I cannot hardcode the names of every app in webpack.config.js, and even if I did a boolean value is just not suitable for getting a single value from a list.

@anshumanv
Copy link
Member

anshumanv commented Oct 12, 2020

@thealjey-eib

If you have a config per application, you can use flag --config-name to run webpack for just that config with that name, maybe it suits your use case better, can you try using that?

@thealjey-eib
Copy link

I don't want a config per application either, because they would all be the same.
It would only complicate things.

@anshumanv
Copy link
Member

Ah okay so you have a single config for all applications, then env is the way to go 👍

@evolopment
Copy link

The documentation still has the old format, I've just had the same problem for following the docs:
https://webpack.js.org/guides/environment-variables/

@chenxsan
Copy link
Member

@evolopment Maybe you can help fixing it :) Here's the source page https://github.com/webpack/webpack.js.org/blob/master/src/content/guides/environment-variables.md to edit.

@evolopment
Copy link

PR sent. Thanks.

@ghost
Copy link

ghost commented Oct 21, 2020

Can there be some easy to find simple fix for this? A lot of people are going to be running into this now, I read the comments and I'm still not sure what the exact fix is

@snitin315
Copy link
Member

@cranphin see #1939 for new --env format.

@ZebraFlesh
Copy link

This is a breaking change that really needs to be called out as such. In addition, I don't think webpack-cli 4.x can claim compatibility with webpack v4 with this change -- the --env.prod=foo form is explicitly called out in the webpack v4 docs. If there is no desire to undo this change, then the peerDependencies should change.

@AsebWebDev
Copy link

So, setting up the enviroment variables like this is the way to go: --env prod=foo - no dot notation.

@snitin315
Copy link
Member

@AsebWebDev yep exactly!

tmarkley pushed a commit to tmarkley/OpenSearch-Dashboards that referenced this issue Feb 9, 2022
* Upgrades `webpack-cli` from v3.3.10 to v4.9.2
  * [CHANGELOG](https://github.com/webpack/webpack-cli/blob/webpack-cli%404.9.2/CHANGELOG.md)
  * `webpack-cli` changed the way that the `--env` flag is parsed. See
webpack/webpack-cli#1216 for more information.
* Upgrades `webpack-dev-server` from v3.11.2 to v4.7.4
  * [CHANGELOG](https://github.com/webpack/webpack-dev-server/blob/v4.7.4/CHANGELOG.md)
  * [Migration guide](https://github.com/webpack/webpack-dev-server/blob/master/migration-v4.md)

Resolves opensearch-project#1065

Signed-off-by: Tommy Markley <markleyt@amazon.com>
tmarkley pushed a commit to opensearch-project/OpenSearch-Dashboards that referenced this issue Feb 11, 2022
* Upgrades `webpack-cli` from v3.3.10 to v4.9.2
  * [CHANGELOG](https://github.com/webpack/webpack-cli/blob/webpack-cli%404.9.2/CHANGELOG.md)
  * `webpack-cli` changed the way that the `--env` flag is parsed. See
webpack/webpack-cli#1216 for more information.
* Upgrades `webpack-dev-server` from v3.11.2 to v4.7.4
  * [CHANGELOG](https://github.com/webpack/webpack-dev-server/blob/v4.7.4/CHANGELOG.md)
  * [Migration guide](https://github.com/webpack/webpack-dev-server/blob/master/migration-v4.md)

Resolves #1065

Signed-off-by: Tommy Markley <markleyt@amazon.com>
@ghost
Copy link

ghost commented Jun 15, 2022

I have installed git for angular in vs code and after installing, ng serve shows unknown argument: serve

@ghost
Copy link

ghost commented Jun 15, 2022

Screenshot_2022_0615_204718

@alexander-akait
Copy link
Member

Please open an issue in angular-cli repo

@Carles11
Copy link

(with versions "webpack": "^5.73.0" and "webpack-cli": "^4.10.0")

This solved the issue for me:

/package.json -->
"scripts": {
"start": "webpack --mode=development && nodemon server",
"build": "webpack --mode=production && NODE_ENV=production",
...
}

Hope it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests