Webpacker: Cannot deploy to staging or demo environments

Created on 15 Feb 2018  路  30Comments  路  Source: rails/webpacker

We have a 'staging' environment set up in our webpacker.yml:

# Note: You must restart bin/webpack-dev-server for changes to take effect

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_output_path: packs
  cache_path: tmp/cache/webpacker

  # Additional paths webpack should lookup modules
  # ['app/assets', 'engine/foo/app/assets']
  resolved_paths: []

  # Reload manifest.json on all requests so we reload latest compiled packs
  cache_manifest: false

  extensions:
    - .coffee
    - .erb
    - .js
    - .jsx
    - .ts
    - .vue
    - .sass
    - .scss
    - .css
    - .png
    - .svg
    - .gif
    - .jpeg
    - .jpg

development:
  <<: *default
  compile: true

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    port: 3035
    public: localhost:3035
    hmr: false
    # Inline should be set to true if using HMR
    inline: true
    overlay: true
    disable_host_check: true
    use_local_ip: false

test:
  <<: *default
  compile: true

  # Compile test packs to a separate directory
  public_output_path: packs-test

production:
  <<: *default

  # Production depends on precompilation of packs prior to booting for performance.
  compile: false

  # Cache manifest.json for performance
  cache_manifest: true
  source_entry_path: pcl/packs
  public_output_path: pcl/packs

staging:
  <<: *default
  # Staging depends on precompilation of packs prior to booting for performance.
  compile: false
  # Cache manifest.json for performance
  cache_manifest: true
  source_entry_path: pcl/packs
  public_output_path: pcl/packs

node_modules/@rails/webpacker/lib/install/config/webpacker.yml does not have a staging environment defined.

We get the following error:

Compilation failed:

/home/user/app/shared/node_modules/@rails/webpacker/package/config.js:15
delete defaultConfig.extensions
^

TypeError: Cannot convert undefined or null to object
at Object.<anonymous> (/home/user/app/shared/node_modules/@rails/webpacker/package/config.js:15:3)
at Module._compile (module.js:641:30)
at Object.Module._extensions..js (module.js:652:10)
at Module.load (module.js:560:32)
at tryModuleLoad (module.js:503:12)
at Function.Module._load (module.js:495:3)
at Module.require (module.js:585:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/user/app/shared/node_modules/@rails/webpacker/package/rules/babel.js:2:24)
at Module._compile (module.js:641:30)
rake stderr: Nothing written

delete defaultConfig.extensions is trying to delete keys in staging (because our own Webpacker config has a staging env defined), but is failing because the default file does not take into account staging environment.

Obviously adding staging won't fix issues for others that have custom name environments.

This is an issue with 3.2.2.

Works fine in 3.2.1.

Most helpful comment

Thanks @gauravtiwari, I've tried deploying my branch to staging env, error message has changed slightly reflecting your changes. Please re-open?

Compilation failed:

/home/user/app/shared/node_modules/@rails/webpacker/package/config.js:16
    delete defaults.extensions
    ^

TypeError: Cannot convert undefined or null to object
    at getConfig (/home/user/app/shared/node_modules/@rails/webpacker/package/config.js:16:5)
    at Object.<anonymous> (/home/user/app/shared/node_modules/@rails/webpacker/package/config.js:27:18)
    at Module._compile (module.js:641:30)
    at Object.Module._extensions..js (module.js:652:10)
    at Module.load (module.js:560:32)
    at tryModuleLoad (module.js:503:12)
    at Function.Module._load (module.js:495:3)
    at Module.require (module.js:585:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/user/app/shared/node_modules/@rails/webpacker/package/rules/babel.js:2:35)
rake stderr: Nothing written

All 30 comments

see #1272 and #1265

Please use 3.3.0

Thanks @gauravtiwari, I've tried deploying my branch to staging env, error message has changed slightly reflecting your changes. Please re-open?

Compilation failed:

/home/user/app/shared/node_modules/@rails/webpacker/package/config.js:16
    delete defaults.extensions
    ^

TypeError: Cannot convert undefined or null to object
    at getConfig (/home/user/app/shared/node_modules/@rails/webpacker/package/config.js:16:5)
    at Object.<anonymous> (/home/user/app/shared/node_modules/@rails/webpacker/package/config.js:27:18)
    at Module._compile (module.js:641:30)
    at Object.Module._extensions..js (module.js:652:10)
    at Module.load (module.js:560:32)
    at tryModuleLoad (module.js:503:12)
    at Function.Module._load (module.js:495:3)
    at Module.require (module.js:585:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/user/app/shared/node_modules/@rails/webpacker/package/rules/babel.js:2:35)
rake stderr: Nothing written

Same here with the command RAILS_ENV=staging bin/rails webpacker:compile :(

Have you upgraded npm package? yarn add @rails/webpacker

@gauravtiwari, yeah, upgraded to 3.3.0. Further to this, delete defaults.extensions is only available in your latest code.

Yep but it returns a default env if both NODE_ENV and RAILS_ENV is not defined in webpacker.yml - https://github.com/rails/webpacker/blob/master/package/env.js#L20

Here is a output from new rails app with webpacker 3.3.0

screen shot 2018-03-05 at 12 35 13

screen shot 2018-03-05 at 12 37 14

I'm having the same issue. I've added a staging section to webpacker.yml and a staging.js file and am running RAILS_ENV=staging bin/rails webpacker:compile

Getting the same error as @taylorwilliams

/home/deploy/myapp/releases/343/node_modules/@rails/webpacker/package/config.js:18
    delete defaults.extensions

I'm using 3.3.0

In node_modules/@rails/webpacker/package/config.js safeLoad(readFileSync(defaultConfigPath), 'utf8') has no staging key.

What @akaspick said.. so when it tries to delete the keys, in our non-standard environment, staging; it's not able too because the non-standard environment doesn't exist in the default config.

So the contents of defaultConfigPath ('../lib/install/config/webpacker.yml') don't contain a staging key (that file is part of the webpacker module), but the env variable is set to staging.

https://github.com/rails/webpacker/blob/master/package/config.js#L12

The next line const app = safeLoad(readFileSync(configPath), 'utf8')[env] is completely fine though as it's referring to the the apps config file which has a staging key. Looks like defaults should be falling back to the production env or ignoring the defaults?

@taylorwilliams @akaspick Thanks, I see. Right, it's bit confusing I am going to take a look again in the evening. Technically, on NODE/JS side NODE_ENV has special meaning, for example lot of packages uses development or production (but not staging) to identify what sort of optimisations/tools they should provide (example: React). So, it would probably make sense to stick with standard environment as NODE_ENV

Is there any progress?(webpacker 3.3.0)

I've got same error.

.../node_modules/@rails/webpacker/package/config.js:16
delete defaults.extensions
^

TypeError: Cannot convert undefined or null to object

@khmunkhbat My current workaround is this setup in staging:

export RAILS_ENV=staging
export NODE_ENV=production

@aldrinmartoq Unfortunately your workaround does not load the "staging" part of webpacker.yml. We use that config file to store environment specific key like google analytics or other front end keys.

I'm getting this too (Webpacker 3.3.1) @aldrinmartoq's trick doesn't seem to fix it.

Just released 3.4.0 and there is some documentation added in README. Please try it out and report your feedback.

Tested with 3.4.3 and a staging configuration and the compilation failed error is resolved.

However, with the configuration:

staging:
  # Compile packs to a separate directory
  public_output_path: staging-packs

the command output states Compiled all packs in public/staging-packs but the files actually get compiled into public/packs. I believe this is because webpack_env no longer passes along the NODE_ENV in the call to ./bin/webpack.

I can work around it by renaming the folder in my deploy script after it's been compiled but it does indicate that at the least public_output_path: packs-staging isn't well supported in the current release.

I've tested with 3.4.3 and it still has the same compilation error.

/tmp/build_e6fb42213c0638c04c0ea31fc08445e2/node_modules/@rails/webpacker/package/config.js:16
           delete defaults.extensions
           ^

       TypeError: Cannot convert undefined or null to object
           at getConfig (/tmp/build_e6fb42213c0638c04c0ea31fc08445e2/node_modules/@rails/webpacker/package/config.js:16:5)
           at Object.<anonymous> (/tmp/build_e6fb42213c0638c04c0ea31fc08445e2/node_modules/@rails/webpacker/package/config.js:27:18)
           at Module._compile (module.js:652:30)
           at Object.Module._extensions..js (module.js:663:10)
           at Module.load (module.js:565:32)
           at tryModuleLoad (module.js:505:12)
           at Function.Module._load (module.js:497:3)
           at Module.require (module.js:596:17)
           at require (internal/module.js:11:18)
           at Object.<anonymous> (/tmp/build_e6fb42213c0638c04c0ea31fc08445e2/node_modules/@rails/webpacker/package/rules/babel.js:2:35)

 !
 !     Precompiling assets failed.

@rodloboz Please make sure npm package is updated as well alongside gem.

yarn add @rails/webpacker

@martron I can't reproduce your issue. RAILS_ENV is responsible for loading custom configurations and not NODE_ENV. Made an example repo: https://github.com/gauravtiwari/webpacker-custom-env

Please see screenshots:

screen shot 2018-04-05 at 09 00 34

screen shot 2018-04-05 at 09 00 49

Please, could you make an example repo on Github with your issue.

Thanks all for efforts :)

@gauravtiwari Thanks for your reply. Updating npm worked for me :)

@gauravtiwari I cloned your custom repo and it works as expected on my machine. I also made the repo match my ruby (v2.4.1) and rails (v5.1.4) versions and it continues to compile to public/packs-staging.

I copied config/webpacker.yml and config/webpack/* into my own project but the behaviour is still the same; it still compiles to public/packs. I also ran bundle update with no change in behaviour.

There's something in my project that's causing the issue. I'll continue to look for differences between it and your sample repo and will let you know if I figure it out.

@martron Are you using react_on_rails?

@gauravtiwari no but webpacker-react is in the gemfile. I did remove it, bundle installed and ran assets:precompile but that didn't change anything.

updated "@rails/webpacker": "^3.5.3" and gem 'webpacker', '~> 3.5' and it's working fine now

thanks, guys.

I still have this issue with 3.5.5.

The following does not seem to use the configuration in config/webpack/staging.js....?

RAILS_ENV=staging NODE_ENV=staging bundle exec rails assets:precompile

@nozpheratu Your NODE_ENV should be production.

@akaspick Tried that, in either case it's using the settings in config/webpack/production.js instead of config/webpack/staging.js.

I still have this issue with 3.5.5.

The following does not seem to use the configuration in config/webpack/staging.js....?

RAILS_ENV=staging NODE_ENV=staging bundle exec rails assets:precompile

Same here, NODE_ENV, RAILS_ENV to staging with gem webpacker & @rails/webpacker 3.5.5.
I've a staging entry in webpacker.yml and for compilation webpacker not using staging.js, always using production.js

Was this page helpful?
0 / 5 - 0 ratings

Related issues

suhomlineugene picture suhomlineugene  路  3Comments

FrankFang picture FrankFang  路  3Comments

naps62 picture naps62  路  3Comments

ankitrg picture ankitrg  路  3Comments

amandapouget picture amandapouget  路  3Comments