axe-me / vite-plugin-node

Vite plugin to run your node dev server with HMR!
990 stars 46 forks source link

SSR build not work #72

Open Farnsi opened 1 year ago

Farnsi commented 1 year ago

I build my app without an error, but when run it with node command, it shows:

TypeError: Cannot read properties of undefined (reading 'MAX_STRING_LENGTH')

Node.js v18.13.0 fastify v4.11.0

vite.config

export default defineConfig({
  build: {
    emptyOutDir: true,
    outDir: './dist',
    sourcemap: true,
  },
  plugins: [
    ...VitePluginNode({
      adapter: 'fastify',
      appPath: './src/index.ts',
      exportName: 'viteNodeApp',
    }),
  ],
  ssr: {
    format: 'cjs',
  }
})

Here are all the places in the compiled index.js where the var buffer$1 is used:

var buffer$1 = {};
.
.
.
/*!
 * The buffer module from node.js, for the browser.
 *
 * @author   Feross Aboukhadijeh <https://feross.org>
 * @license  MIT
 */
(function(exports2) {
.
.
.
})(buffer$1);
.
.
.
const buffer = buffer$1;
const assert$4 = require$$0$6;
const kImpl = Symbol("kImpl");
const MAX_STRING = buffer.constants.MAX_STRING_LENGTH;
class FakeWeakRef {
  constructor(value) {
    this._value = value;
  }

buffer$1 contains:

{
  Buffer: [Function: Buffer2] {
    TYPED_ARRAY_SUPPORT: true,
    poolSize: 8192,
    from: [Function (anonymous)],
    alloc: [Function (anonymous)],
    allocUnsafe: [Function (anonymous)],
    allocUnsafeSlow: [Function (anonymous)],
    isBuffer: [Function: isBuffer],
    compare: [Function: compare2],
    isEncoding: [Function: isEncoding],
    concat: [Function: concat],
    byteLength: [Function: byteLength2]
  },
  SlowBuffer: [Function: SlowBuffer],
  INSPECT_MAX_BYTES: 50,
  kMaxLength: 2147483647
}

I really don't have an idea what causes that problem: node, a module, vite, vite-plugin-node, did you have any suggestion?

axe-me commented 1 year ago

can you make a repro or just paste the code snippet that using buffer.

qeleb commented 1 year ago

i had this same problem when outputting as CommonJS

qeleb commented 1 year ago

the debugger seemed to suggest this issue is in Vite but I’m not sure either