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.

SyntaxError: Identifier '_<char>' has already been declared

See original GitHub issue

Describe the bug

Hey guys, I already checked this issue https://github.com/vitejs/vite/issues/3363, but doesn’t look helpful at my case. So, I compile the project and then run with command node dist/server-node.cjs.js and sometimes give me the error which is wrote on title or this: TypeError: Cannot read properties of undefined (reading 'isEncoding'), someone has an idea what this could be? I just want move from “tsc” to “vite”, and with “tsc” the things was working perfectly. Thanks.

Reproduction

I don’t know what to put on reproduction but the problem ins’t on my code. So maybe my vite.config.ts could help in something.

package.json
{
  "name": "server-node",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "pg": "^8.7.3",
    "pg-native": "^3.0.0",
    "short-uuid": "^4.2.0"
  },
  "devDependencies": {
    "@types/node": "^18.0.0",
    "@types/pg": "^8.6.5",
    "typescript": "^4.7.4",
    "vite": "^2.9.12"
  }
}
vite.config.ts
import { defineConfig } from 'vite';

export const name = 'server-node';

// https://vitejs.dev/config/
export default defineConfig({
  optimizeDeps: {
    exclude: [
      'perf_hooks',
      'pg-native'
    ]
  },
  build: {
    target: 'ESNext',
    lib: {
      name,
      entry: 'src/index.ts',
      formats: [
        'cjs'
      ],
      fileName: (format) => `${name}.${format}.js`
    },
    rollupOptions: {
      external: [
        'perf_hooks',
        'pg-native'
      ],
      output: {
        globals: {
          perf_hooks: 'perf_hooks'
        }
      }
    }
  }
});

System Info

System:
    OS: macOS 12.4
    CPU: (10) arm64 Apple M1 Pro
    Memory: 1.31 GB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.2.0 - /opt/homebrew/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.11.0 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 102.0.5005.115
    Safari: 15.5
  npmPackages:
    vite: ^2.9.12 => 2.9.12

Used Package Manager

yarn

Logs

Click to expand!
yarn run v1.22.19
vite build --debug
  vite:config bundled config file loaded in 8.92ms +0ms
  vite:config using resolved config: {
  vite:config   optimizeDeps: {
  vite:config     exclude: [ 'perf_hooks', 'pg-native' ],
  vite:config     esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
  vite:config   },
  vite:config   build: {
  vite:config     target: 'ESNext',
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: false,
  vite:config     cssTarget: 'ESNext',
  vite:config     sourcemap: false,
  vite:config     rollupOptions: { external: [Array], output: [Object] },
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: {
  vite:config       name: 'server-node',
  vite:config       entry: 'src/index.ts',
  vite:config       formats: [Array],
  vite:config       fileName: [Function: fileName]
  vite:config     },
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] }
  vite:config   },
  vite:config   configFile: './server_node/vite.config.ts',
  vite:config   configFileDependencies: [
  vite:config     './server_node/vite.config.ts'
  vite:config   ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     build: {}
  vite:config   },
  vite:config   root: './server_node',
  vite:config   base: '/',
  vite:config   resolve: { dedupe: undefined, alias: [ [Object], [Object] ] },
  vite:config   publicDir: './server_node/public',
  vite:config   cacheDir: './server_node/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isWorker: false,
  vite:config   isProduction: true,
  vite:config   plugins: [
  vite:config     'vite:build-metadata',
  vite:config     'alias',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'vite:worker-import-meta-url',
  vite:config     'vite:watch-package-data',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'rollup-plugin-dynamic-import-variables',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:terser',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     fs: { strict: true, allow: [Array], deny: [Array] }
  vite:config   },
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: undefined,
  vite:config     proxy: undefined,
  vite:config     cors: undefined,
  vite:config     headers: undefined
  vite:config   },
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   packageCache: Map(0) { set: [Function (anonymous)] },
  vite:config   createResolver: [Function: createResolver],
  vite:config   worker: {
  vite:config     format: 'iife',
  vite:config     plugins: [
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object]
  vite:config     ],
  vite:config     rollupOptions: {}
  vite:config   }
  vite:config } +6ms
vite v2.9.12 building for production...
✓ 127 modules transformed.
dist/server-node.cjs.js   82.17 KiB / gzip: 26.99 KiB
✨  Done in 0.91s.

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
amitShimon1983commented, Jul 10, 2022

Hey again @amitShimon1983, so in this case, please give us an “reproduction”, so we can analyse it too, you still didn’t us gave a lot information about your problem. Maybe this problem can not be very specific at my case, could be something affecting it and giving same error. I already saw people with basically same error and my fix was literally different them.

Hey I open an issue https://github.com/vitejs/vite/issues/8948 hope this will help

1reaction
0rangeFoxcommented, Jun 26, 2022

I came up with solution. first of all, I’m forced to disabled minify otherwise, you can configure it to use terser and disable the mangle option. Then, following the issue https://github.com/brianc/node-postgres/issues/1906, we should ignore the natives of postgresql library on CJS. So the final result for vite configuration is:

vite.config.ts
import { defineConfig } from 'vite';
import { builtinModules } from 'module';

const name = 'server-node';

// https://vitejs.dev/config/
export default defineConfig({
  build: {
    target: 'ESNext',
    minify: false,
    lib: {
      entry: 'src/index.ts',
      name,
      formats: [
        'cjs'
      ],
      fileName: (format) => `${name}.${format}.js`
    },
    rollupOptions: {
      external: builtinModules
    },
    commonjsOptions: {
      ignore: [
        'pg-native',
        './native'
      ]
    }
  }
});

And thanks for @bluwy, for mentioning the builtinModules, didn’t know about this and for him help too. Happy coding folks ✌️

Read more comments on GitHub >

github_iconTop Results From Across the Web

Browser: Identifier X has already been declared - Stack Overflow
I'm using a custom-file.js file in next.js and to resolve SyntaxError: Identifier 'x' has already been declared error, simply change your const and...
Read more >
Identifier has already been declared Error in JavaScript
The "Identifier has already been declared error" occurs when a variable with the same name has been declared multiple times in the same...
Read more >
SyntaxError: identifier starts immediately after numeric literal
The JavaScript exception "identifier starts immediately after numeric literal" occurs when an identifier started with a digit. Identifiers can only start ...
Read more >
const '{a}' has already been declared - JSLint Error Explanations
The "const '{a}' has already been declared" error is thrown when JSHint encounters a constant declaration with an identifier that has already been...
Read more >
Solved: JavaScript switch/case "Identifier '...' has already been ...
Fix for "Identifier '...' has already been declared" when using JavaScript switch/case ... If you specify the same variable name (created using ...
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