symfony / webpack-encore-bundle

Symfony integration with Webpack Encore!
https://symfony.com/webpack-encore
MIT License
933 stars 83 forks source link

Webpack Encore context directory issue #197

Closed xkzl closed 1 year ago

xkzl commented 1 year ago

Hello,

I have been trying to move node_modules directory from /var/www/my_app/node_modules into /var/www/my_app/var/modules/node_modules. For that I have put package.json into /var/www/my_app/var/modules/ and I am using the following command:

npm --prefix /var/www/my_app/var/modules/
yarn --modules-folder /var/www/my_app/var/modules/node_modules
encore --context /var/www/my_app/var/modules/

This sounded fine at first, but it seems that the encore command has a weird behavior depending on which option I am running.

/var/www/my_app/var/modules/node_modules/@symfony/webpack-encore/lib/config/parse-runtime.js:65
            throw new Error('Cannot determine webpack context. (Are you executing webpack from a directory outside of your project?). Try passing the --context option.');
            ^

Error: Cannot determine webpack context. (Are you executing webpack from a directory outside of your project?). Try passing the --context option.
    at module.exports (/var/www/my_app/var/modules/node_modules/@symfony/webpack-encore/lib/config/parse-runtime.js:65:19)
    at Object.<anonymous> (/var/www/my_app/var/modules/node_modules/@symfony/webpack-encore/bin/encore.js:18:23)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47
www-data@ubuntu:/var/www/my_app$ /var/www/my_app/var/modules/node_modules/.bin/encore --context ./var/modules/ dev
Running webpack ...

[webpack-cli] Failed to load '/var/www/my_app/webpack.config.js' config
[webpack-cli] Error: Cannot find module '@symfony/webpack-encore'
Require stack:
- /var/www/my_app/webpack.config.js
- /var/www/my_app/var/modules/node_modules/webpack-cli/lib/webpack-cli.js
- /var/www/my_app/var/modules/node_modules/webpack-cli/lib/bootstrap.js
- /var/www/my_app/var/modules/node_modules/webpack-cli/bin/cli.js
- /var/www/my_app/var/modules/node_modules/webpack/bin/webpack.js
- /var/www/my_app/var/modules/node_modules/@symfony/webpack-encore/bin/encore.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Module._load (node:internal/modules/cjs/loader:841:27)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/var/www/my_app/webpack.config.js:1:16)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/www/my_app/webpack.config.js',
    '/var/www/my_app/var/modules/node_modules/webpack-cli/lib/webpack-cli.js',
    '/var/www/my_app/var/modules/node_modules/webpack-cli/lib/bootstrap.js',
    '/var/www/my_app/var/modules/node_modules/webpack-cli/bin/cli.js',
    '/var/www/my_app/var/modules/node_modules/webpack/bin/webpack.js',
    '/var/www/my_app/var/modules/node_modules/@symfony/webpack-encore/bin/encore.js'
  ]
}
www-data@georges:/var/www/my_app/var/modules$ /var/www/my_app/var/modules/node_modules/.bin/encore dev
Running webpack ...

assets by status 0 bytes [cached] 1 asset

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value.
Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/

ERROR in main
Module not found: Error: Can't resolve './src' in '/var/www/my_app/var/modules'
resolve './src' in '/var/www/my_app/var/modules'
  using description file: /var/www/my_app/var/modules/package.json (relative path: .)
    Field 'browser' doesn't contain a valid alias configuration
    using description file: /var/www/my_app/var/modules/package.json (relative path: ./src)
      no extension
        Field 'browser' doesn't contain a valid alias configuration
        /var/www/my_app/var/modules/src is not a file
      .js
        Field 'browser' doesn't contain a valid alias configuration
        /var/www/my_app/var/modules/src.js doesn't exist
      .json
        Field 'browser' doesn't contain a valid alias configuration
        /var/www/my_app/var/modules/src.json doesn't exist
      .wasm
        Field 'browser' doesn't contain a valid alias configuration
        /var/www/my_app/var/modules/src.wasm doesn't exist
      as directory
        existing directory /var/www/my_app/var/modules/src
          using description file: /var/www/my_app/var/modules/package.json (relative path: ./src)
            using path: /var/www/my_app/var/modules/src/index
              using description file: /var/www/my_app/var/modules/package.json (relative path: ./src/index)
                no extension
                  Field 'browser' doesn't contain a valid alias configuration
                  /var/www/my_app/var/modules/src/index doesn't exist
                .js
                  Field 'browser' doesn't contain a valid alias configuration
                  /var/www/my_app/var/modules/src/index.js doesn't exist
                .json
                  Field 'browser' doesn't contain a valid alias configuration
                  /var/www/my_app/var/modules/src/index.json doesn't exist
                .wasm
                  Field 'browser' doesn't contain a valid alias configuration
                  /var/www/my_app/var/modules/src/index.wasm doesn't exist

webpack 5.75.0 compiled with 1 error and 1 warning in 226 ms

The difference in the last attempt is that my encore command is running not from my application root but the ./var/modules directory. So this looks weird. I would expected that the --context option was changing directory. However this still not works as it seems to me to be looking for some unexpected files