Reputation: 639
I'm making the Meteor.js app and I have to get data from mssql database. I use Meteor 1.8 and a npm package - mssql(version 5.1.0). That's my code:
const sql = require('mssql')
const config = {something}
export default function fetch(query) {
const config = {something}
sql.connect(config, err => {
if (err) {
console.error(err)
return
}
// create Request object
var request = new sql.Request()
// query to the database and get the records
request.query(query, (err, recordset) => {
if (err) {
console.error(err)
return
}
return recordset
})
})
}
And I have such error
TypeError: sql.connect is not a function
I don't know what's going on, I tried to do it in many ways and I failed. When I use ConnectionPool I see an error saying that ConnectionPool is not a constructor. What is the proper way to do this?
In my config file I have: user, password, server, database, port.
Upvotes: 3
Views: 4112
Reputation: 1074959
It appears to be because you're mixing your module systems, you're using require
(which is CommonJS-like) but in something that's apparently an ECMAScript module (ESM) (from the export default ...
). Node.js itself won't let you do that, but if you're using some kind of bundler or transpiler, perhaps it might...
Changing your require
to:
import sql from "mssql";
...and making sure Node.js is using ESM¹, I don't get any error with sql.connect
not being a function.
¹ In my case, since I'm using Node.js v12, I do that by having "type": "module"
in my package.json
and running it via node --experimental-modules filename.js
, more here.
Upvotes: 5