nrwl / nx-labs

A collection of Nx plugins
MIT License
130 stars 48 forks source link

rspack "process not defined" error when trying to use process.env #361

Open Nick-Lucas opened 6 months ago

Nick-Lucas commented 6 months ago

Currently the rspack plugin seems not to support process out of the box, so the normal configuration approach of .env files in Nx does not work.

141616 commented 4 months ago

Currently the rspack plugin seems not to support process out of the box, so the normal configuration approach of .env files in Nx does not work.

https://www.rspack.dev/guide/language-support.html#node-polyfills

phal0r commented 4 months ago

I stumled upon the same problem. The env files are read properly, but the define plugin is missing.

My workaround (the code must be added in the rspack.config file):

const defines = {}
  Object.keys(process.env).forEach(function(key) {
    if(key.startsWith("NX_")) {
      defines['process.env.' + key] = JSON.stringify(process.env[key])
    }
  });
  config.plugins.push(new rspack.DefinePlugin(defines))

This will replace all process.env.NX_* variables in the frontend code.