IBM / node-odbc

ODBC bindings for node
MIT License
146 stars 77 forks source link

Next.js v13.5.2 - odbc v2.4.8 - Node v18.18.0 #354

Closed JRichens closed 5 months ago

JRichens commented 1 year ago

On a Windows 10 machine using Node v18.18.0 starting a fresh Next v13.5.2 project (blank) then installing odbc 2.4.8, my project starts fine as seen below.

1

However, in my page.tsx, when I simply start with const odbc = require('odbc') I get the following errors on my project.

2

Would you please point out as to where I am going wrong?

Thank you

stale[bot] commented 9 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

noxify commented 8 months ago

Maybe a bit late, does it work if you use import instead of require?

Example:

// src/app/page.tsx
import odbc from "odbc"

export default function Home() {
  const connectionString = ''
  const connection = await odbc.connect(connectionConfig)
  const queryResult = await connection.query(`SELECT * from testtable`)
  console.log({queryResult})

  return <>Home Page - Check your server log</>
}

If this doesn't work, you could try to use a api route to fetch your data and use this api route then in your page to fetch the content.

JRichens commented 8 months ago

Thanks for the reply. It doesn't work either way, no. I had to create an express node server with an API to it to work in the end. Would have been useful to work out the box in a NEXT.js server component though. Just couldn't figure out why it wouldn't!!

JRichens commented 8 months ago

Hello, I tried using Next.JS API route, but got the following error:

./node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

<!doctype html> | |

Any ideas please?

noxify commented 8 months ago

The main problem is, that nextjs tries to bundle the odbc package.

I tried it quickly and was able to get it running by updating the next.config.mjs to the following:

/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    serverComponentsExternalPackages: ['odbc'],
  },
};

export default nextConfig;

Doc ref: https://nextjs.org/docs/app/api-reference/next-config-js/serverComponentsExternalPackages

stale[bot] commented 5 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.