Webpack 5+Process is not defined triggered by stream-browserify
Asked Answered
S

1

6

I have to decode a CBOR encoded array using the cbor node module.

When i launch my command:

const decodedData = base45.decode(greenpassBody);
const output = pako.inflate(decodedData);
const results = cbor.decodeAllSync(output); // this line

this error is being triggered:

_stream_readable.js:529 Uncaught ReferenceError: process is not defined
    at emitReadable (_stream_readable.js:529)
    at addChunk (_stream_readable.js:303)
    at readableAddChunk (_stream_readable.js:280)
    at NoFilter.Readable.push (_stream_readable.js:241)
    at NoFilter.Transform.push (_stream_transform.js:139)
    at NoFilter._transform (index.js:220)
    at NoFilter.Transform._read (_stream_transform.js:177)
    at NoFilter.Transform._write (_stream_transform.js:164)
    at doWrite (_stream_writable.js:409)
    at writeOrBuffer (_stream_writable.js:398)

Triggered by the stram-browserify polyfill, necessary to use all of these packages in my web app.

As I read on web, I tried to install the process module in my package.json, then in my webpack config file i tryied in few ways:

fallback: {
  stream: require.resolve('stream-browserify'),
  util: require.resolve('util/'),
  zlib: require.resolve('browserify-zlib'),
  assert: require.resolve('assert/'),
  crypto: require.resolve('crypto-browserify'),
  fs: require.resolve('browserify-fs'),
  path: require.resolve('path-browserify'),
  process: require.resolve('process/browser'),  // <- this
}
alias: {
    'jquery-ui': path.resolve(__dirname, "./node_modules/jquery-ui"),
    'owl.carousel': path.resolve(__dirname, "./node_modules/owl.carousel"),
    modernizr$: path.resolve(__dirname, "./modernizrrc.js"),
    WOW: 'wowjs',
    process: "process/browser", // 1st try
    process: path.resolve(__dirname, "./node_modules/process/browser.js"), // 2nd try
},
    plugins: [
      // ...
      new webpack.DefinePlugin({
         process: "process/browser"
        "process.env": {
          ASSET_PATH: JSON.stringify(ASSET_PATH),
          NODE_ENV: JSON.stringify("development"),
          ENV: JSON.stringify("development"),
        },
      }),

With the DefinePlugin try, the error changed, it said browser is not defined.

Anyone knows how to correctly polyfill this module or if there are other ways to use the CBOR module?

Thanks so much

Sneak answered 27/7, 2021 at 9:43 Comment(0)
S
13

SOLVED

the problem was that the process plugin must be provided, not defined:

it's

new webpack.ProvidePlugin({
  process: 'process/browser',
})

not

new webpack.DefinePlugin({
  process: 'process/browser',
}),
Sneak answered 28/7, 2021 at 9:28 Comment(2)
It raised an error ERROR in Error: Child compilation failed:Customs
can you please describe the context in which this happens?Sneak

© 2022 - 2024 — McMap. All rights reserved.