question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Browser polyfill for `process.env`

See original GitHub issue

Environment

Nuxt CLI v3.0.0-27375427.d07d572
RootDir: /home/kael/Documents/vuetifyjs/test-projects/nuxt3/nuxt3-app
Nuxt project info:
------------------------------
- Operating System: `Linux`
- Node Version:     `v16.13.2`
- Nuxt Version:     `3.0.0-27375427.d07d572`
- Package Manager:  `yarn@1.22.5`
- Bundler:          `Vite`
- User Config:      `css`, `build`, `vite`
- Runtime Modules:  `-`
- Build Modules:    `-`
------------------------------

Reproduction

Create a plugin with

console.log(typeof process)
console.log(process.env.NODE_ENV)

and look at the browser console.

Describe the bug

process is { dev: true, server: false, client: true }, should be undefined process.env.NODE_ENV throws TypeError: Cannot read properties of undefined (reading 'NODE_ENV'), should be 'development'

Additional context

This works with just vite, idk what you’re doing to disable that. It looks like this is also set up correctly for builds with @rollup/plugin-replace, so it’s only a problem during dev.

Logs

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
madjidtahacommented, Sep 30, 2022

Hello everyone This happen to me as well using nuxt rc11 when trying to use theatre.js, I found a workaround using vite.define object in nuxt.config.ts. After that, process look like this : { dev: true, server: false, client: true, env: {} } and dependencies work as intended and doesn’t seem to be broken.

Here is the nuxt.config.ts file I used, I hope that could be helpful

import svgLoader from 'vite-svg-loader'
import glslLoader from 'vite-plugin-glsl'
export default defineNuxtConfig({
  modules: ['@pinia/nuxt'],
  ssr: false,
  css: ['@/assets/styles/index.css'],
  vite: {
    define: {
      // Fix broken dependencies of theatre.js
      'process.env': {},
    },
    plugins: [glslLoader(), svgLoader()],
  },
})
3reactions
Saul-Mironecommented, May 26, 2022

This seems appear again. The process in browser doesn’t have a env property that breaks some packages.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node process object made available to browser client code
These options configure whether to polyfill or mock certain Node.js globals and modules. This allows code originally written for the Node.js ...
Read more >
Undeclared use of node's process causes exception ... - GitHub
Webpack 5 no longer ships polyfills for nodejs builtin modules, and recommends using this module if a module need util, and the module ......
Read more >
Node Emulation - Parcel
Polyfilling & Excluding Builtin Node Modules ... When targetting the browser and your code, or more likely a dependency, imports builtin Node modules...
Read more >
node-polyfill-webpack-plugin - npm
Polyfill Node.js core modules in Webpack.. Latest version: 2.0.1, last published: 5 months ago. Start using node-polyfill-webpack-plugin in ...
Read more >
Polyfill Node.js built-in modules with Vite | by Fabiano Taioli
Polyfill Node.js built-in modules with Vite · A dev server that serves your source files over native ES modules, with rich built-in features...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found