Skip to content

process is not defined error with Vite/Rollup #31

@lvnam96

Description

@lvnam96

I got error stated in title due to the different between how Webpack & Vite load env vars.

I managed to solve the problem by updating Vite's config:

import { defineConfig, loadEnv } from 'vite';

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '');
  return {
    define: {
      // those two env vars are used by this lib
      // ref: https://github.com/thlorenz/parse-link-header/blob/f380d3f99de4a5411b2d7f8da6069bb7529cbf4a/index.js#L7
      'process.env.PARSE_LINK_HEADER_MAXLEN': JSON.stringify(env.PARSE_LINK_HEADER_MAXLEN),
      'process.env.PARSE_LINK_HEADER_THROW_ON_MAXLEN_EXCEEDED': JSON.stringify(
        env.PARSE_LINK_HEADER_THROW_ON_MAXLEN_EXCEEDED,
      ),
    },
    // ...
}

Refs:

Related issue #26
#28 also addressed this issue & are resolved in another forked repo, see https://github.com/web3-storage/parse-link-header/blob/490a43ab57f2cb890c924a3705b36da14783f797/README.md?plain=1#L53

There might be more env vas used in the future, & it's not great to maintain this inside our code. Should there be a note for Vite's users in documentation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions