Repository: napi-rs/napi-rs
File: cli/src/api/templates/js-binding.ts (TypeScript)

Code before:
${createCommonBinding(localName, pkgName, packageVersion)}
const { ${idents.join(', ')} } = nativeBinding
${idents.map((ident) => `export { ${ident} }`).join('\n')}
`
}

const bindingHeader = `// prettier-ignore
/* eslint-disable */
// @ts-nocheck
/* auto-generated by NAPI-RS */
`

function createCommonBinding(
  localName: string,
  pkgName: string,
  packageVersion?: string,
): string {
  function requireTuple(tuple: string, identSize = 8) {
    const identLow = ' '.repeat(identSize - 2)
    const ident = ' '.repeat(identSize)
    const versionCheck = packageVersion
      ? `
${identLow}try {
${ident}const binding = require('${pkgName}-${tuple}')
${ident}const bindingPackageVersion = require('${pkgName}-${tuple}/package.json').version
${ident}if (bindingPackageVersion !== '${packageVersion}' && (process.env.NAPI_RS_ENFORCE_VERSION_CHECK === '0' || !process.env.NAPI_RS_ENFORCE_VERSION_CHECK)) {
${ident}  throw new Error(\`Native binding package version mismatch, expected ${packageVersion} but got \${bindingPackageVersion}. You can reinstall dependencies to fix this issue.\`)
${ident}}
${ident}return binding
${identLow}} catch (e) {
${ident}loadErrors.push(e)
${identLow}}`
      : `
${identLow}try {
${ident}return require('${pkgName}-${tuple}')
${identLow}} catch (e) {
${ident}loadErrors.push(e)
${identLow}}`
    return `try {
${ident}return require('./${localName}.${tuple}.node')
${identLow}} catch (e) {
${ident}loadErrors.push(e)
${identLow}}${versionCheck}`
  }

  return `const { readFileSync } = require('node:fs')
let nativeBinding = null
const loadErrors = []

const isMusl = () => {
  let musl = false

Review comment (suggestion):
The condition logic appears to have the opposite behavior of what's intended. Currently, the error is thrown when version checking is ENABLED (env var is set and not '0'), but the error message suggests this should happen when there's a version mismatch and checking is NOT disabled. The condition should be: `bindingPackageVersion !== '${packageVersion}' && (!process.env.NAPI_RS_ENFORCE_VERSION_CHECK || process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0')`
```suggestion
${ident}if (bindingPackageVersion !== '${packageVersion}' && (!process.env.NAPI_RS_ENFORCE_VERSION_CHECK || process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0')) {
```

Code after:
${createCommonBinding(localName, pkgName, packageVersion)}
const { ${idents.join(', ')} } = nativeBinding
${idents.map((ident) => `export { ${ident} }`).join('\n')}
`
}

const bindingHeader = `// prettier-ignore
/* eslint-disable */
// @ts-nocheck
/* auto-generated by NAPI-RS */
`

function createCommonBinding(
  localName: string,
  pkgName: string,
  packageVersion?: string,
): string {
  function requireTuple(tuple: string, identSize = 8) {
    const identLow = ' '.repeat(identSize - 2)
    const ident = ' '.repeat(identSize)
    const versionCheck = packageVersion
      ? `
${identLow}try {
${ident}const binding = require('${pkgName}-${tuple}')
${ident}const bindingPackageVersion = require('${pkgName}-${tuple}/package.json').version
${ident}if (bindingPackageVersion !== '${packageVersion}' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
${ident}  throw new Error(\`Native binding package version mismatch, expected ${packageVersion} but got \${bindingPackageVersion}. You can reinstall dependencies to fix this issue.\`)
${ident}}
${ident}return binding
${identLow}} catch (e) {
${ident}loadErrors.push(e)
${identLow}}`
      : `
${identLow}try {
${ident}return require('${pkgName}-${tuple}')
${identLow}} catch (e) {
${ident}loadErrors.push(e)
${identLow}}`
    return `try {
${ident}return require('./${localName}.${tuple}.node')
${identLow}} catch (e) {
${ident}loadErrors.push(e)
${identLow}}${versionCheck}`
  }

  return `const { readFileSync } = require('node:fs')
let nativeBinding = null
const loadErrors = []

const isMusl = () => {
  let musl = false