Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use without 'require'? #305

Open
jasong-au opened this issue Sep 25, 2023 · 1 comment
Open

Cannot use without 'require'? #305

jasong-au opened this issue Sep 25, 2023 · 1 comment

Comments

@jasong-au
Copy link

jasong-au commented Sep 25, 2023

The whole way that mssql and msnodesqlv8 work together seems wonky. It's taken a lot of time, but I got it to simply connect with plain javascript and using require. However I am trying to use it in a module (server-size sveltekit, but trying to get it working in node .mjs) and can't figure out how to get it to work without using 'require' which isn't supported. Any help or samples would be appreciated.

// This works in a plain .js file, but throws this error in an .mjs file
// *** ReferenceError: require is not defined in ES module scope, you can use import instead ***
// const sql = require('mssql/msnodesqlv8')

// Trying to change it to an import gives an error:
// *** Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\git\t\mssql\node_modules\mssql\msnodesqlv8' imported from C:\git\t\mssql\index.mjs ***
import * as sql from 'mssql/msnodesqlv8'
@TimelordUK
Copy link
Owner

I am afraid i do not have any association with this module, so cannot help - does this work?

import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const sql= require('mssql/msnodesqlv8')
const connectionString  = "Driver={ODBC Driver 17 for SQL Server};Server=(localdb)\\node;Database=scratch;Trusted_Connection=yes;"

const config = {
    connectionString: connectionString
}

async function runner() {
    const pool = await sql.connect(config)
    const res = await pool.query('select top 3 * from syscolumns')
    console.log(JSON.stringify(res, null, 4))
    await pool.close()
}

runner().then(() => {
    console.log('done')
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants