omise/omise-node

Webpack fails to bundle library because of dependency expressions

Closed this issue · 3 comments

Recently I tried using this library with Next.js API Routes, but was unable to do so because the Webpack bundling (which Next.js uses for both frontend and backend by default) did not work properly.

The issue occurs because this library uses 'dependency expressions' - file names are generated and required using a function meaning Webpack cannot resolve them statically.

Specifically, this happens at https://github.com/omise/omise-node/blob/master/lib/apiResources.js#L6

function resourceName(name) {
  return require(['./resources/', name].join(''))(omiseConfig);
}

The fix is to require the files using their full names hardcoded e.g.

require('./resources/Source`)

Slightly more code, but should enable the use of this library with Webpack and therefore Next.js.

I have raised a PR to fix this: #141 #166

Dear Omise team, any update for this PR to fix the #141 issue? This PR could save anybody using NextJS and deploy to the Vercel Hosting.

I just tried this out again by deploying a minimal Next.js app to Vercel. Using this library in an API route causes the route to crash with a 500 error (see below). So just an FYI, this library still doesn't seem to work with Next.js deployed on Vercel.

Should be fixed by #166, although I only tested this fix a long time ago so cannot be 100% sure (given that Next.js versions have increased a lot since then).

2022-11-17T01:19:18.593Z	e7387498-a0d1-4a63-a775-f05eefa8ea88	ERROR	Error: Cannot find module './resources/Source'
Require stack:
- /var/task/node_modules/omise/lib/apiResources.js
- /var/task/node_modules/omise/index.js
- /var/task/.next/server/pages/api/hello.js
- /var/task/node_modules/next/dist/server/next-server.js
- /var/task/___next_launcher.cjs
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.mod._resolveFilename (/var/task/node_modules/next/dist/build/webpack/require-hook.js:23:32)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at resourceName (/var/task/node_modules/omise/lib/apiResources.js:7:10)
    at Object.module.exports.omiseResources (/var/task/node_modules/omise/lib/apiResources.js:40:14)
    at module.exports (/var/task/node_modules/omise/index.js:5:19)
    at handler (/var/task/.next/server/pages/api/hello.js:26:46)
    at Object.apiResolver (/var/task/node_modules/next/dist/server/api-utils/node.js:367:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async NextNodeServer.runApi (/var/task/node_modules/next/dist/server/next-server.js:474:9)
    at async Object.fn (/var/task/node_modules/next/dist/server/next-server.js:736:37)
    at async Router.execute (/var/task/node_modules/next/dist/server/router.js:252:36)
    at async NextNodeServer.run (/var/task/node_modules/next/dist/server/base-server.js:384:29)
    at async NextNodeServer.handleRequest (/var/task/node_modules/next/dist/server/base-server.js:322:20) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/node_modules/omise/lib/apiResources.js',
    '/var/task/node_modules/omise/index.js',
    '/var/task/.next/server/pages/api/hello.js',
    '/var/task/node_modules/next/dist/server/next-server.js',
    '/var/task/___next_launcher.cjs'
  ]
}

Fixed by #190.