#Firebase Functions. Change from CommonJS to NodeNext / ESM - esbuild

5 messages · Page 1 of 1 (latest)

pearl badger
#

Hi All. I am having trouble trying to convert my filebase functions from the default CommonJS to newer such as NodeNextor EsNext. Ive followed some unoffical guides online but i keep getting the below error:

Error: Dynamic require of "fs" is not supported
    at file:///home/liamwears/full-stack-challenge/apps/functions/dist/bundle.js:11:9
    at ../../node_modules/firebase-admin/lib/app/lifecycle.js (file:///home/liamwears/full-stack-challenge/apps/functions/dist/bundle.js:26228:14)
    at __require2 (file:///home/liamwears/full-stack-challenge/apps/functions/dist/bundle.js:17:51)
    at ../../node_modules/firebase-admin/lib/app/firebase-namespace.js (file:///home/liamwears/full-stack-challenge/apps/functions/dist/bundle.js:154409:23)
    at __require2 (file:///home/liamwears/full-stack-challenge/apps/functions/dist/bundle.js:17:51)
    at ../../node_modules/firebase-admin/lib/default-namespace.js (file:///home/liamwears/full-stack-challenge/apps/functions/dist/bundle.js:154710:32)
    at __require2 (file:///home/liamwears/full-stack-challenge/apps/functions/dist/bundle.js:17:51)
    at ../../node_modules/firebase-admin/lib/index.js (file:///home/liamwears/full-stack-challenge/apps/functions/dist/bundle.js:154720:20)
    at __require2 (file:///home/liamwears/full-stack-challenge/apps/functions/dist/bundle.js:17:51)
    at file:///home/liamwears/full-stack-challenge/apps/functions/dist/bundle.js:162859:37
#
require('esbuild')
  .build({
    // the entry point file described above
    entryPoints: ['src/index.ts'],
    // the build folder location described above
    outfile: 'dist/bundle.js',
    bundle: true,
    platform: 'node',
    target: 'node16', // Target a compatible Node.js version
    format: 'esm', // Use CommonJS as the output format
    // Optional and for development only. This provides the ability to
    // map the built code back to the original source format when debugging.
    //sourcemap: 'inline',
    //external: ['./node_modules/*'],
    logLevel: 'info',
    tsconfig: './tsconfig.json',
  })
  .catch(() => process.exit(1))
#
{
  "compilerOptions": {
    /* Base Options: */
    "esModuleInterop": true,
    "skipLibCheck": true,
    "target": "es2022",
    "verbatimModuleSyntax": true,
    "allowJs": true,
    "resolveJsonModule": true,
    "moduleDetection": "force",
    /* Strictness */
    "strict": true,
    "noUncheckedIndexedAccess": true,
    /* If NOT transpiling with TypeScript: */
    "moduleResolution": "Node",
    "module": "ES2022",
    "noEmit": true,
    "types": [
      "node"
    ],
    "allowSyntheticDefaultImports": true,
    /* If your code doesn't run in the DOM: */
    "lib": [
      "es2022"
    ],
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}
#

Firebase Functions. Change from CommonJS to NodeNext / ESM - esbuild

pearl badger
#
require('esbuild')
  .build({
    // the entry point file described above
    entryPoints: ['src/index.ts'],
    // the build folder location described above
    outfile: 'dist/index.js',
    bundle: true,
    platform: 'node',
    target: 'node16', // Target a compatible Node.js version
    format: 'esm', // Use CommonJS as the output format
    // Optional and for development only. This provides the ability to
    // map the built code back to the original source format when debugging.
    //sourcemap: 'inline',
    external: ['../../node_modules/*'],
    logLevel: 'info',
    tsconfig: './tsconfig.json',
  })
  .catch(() => process.exit(1))

the fix was the external parameter pointing to where node modules was for firebase functions