Reputation: 73
I'm beginner in Node.js, I want to connect Node.js with local SQL server and I got this sentence:
Cannot find module 'mssql' nodejs
Thanks for your support.
Upvotes: 7
Views: 14538
Reputation: 37633
Nowdays (2021) you have to read this manual.
And basically you have to install Microsoft SQL Server client for Node.js like
npm install mssql
You can test it by following code.
test-mssql-server.js
var app = require('express')();
app.get('*', (req, res) => {
var sql = require("mssql");
// config for your database
var config = {
user: 'sa',
password: 'mypssssss1!',
server: 'MyComputerName001\\SQL2016Instance',
database: 'MyDbName'
};
(async function () {
try {
let pool = await sql.connect(config)
let result1 = await pool.request()
.query('select * from Devices')
// console.dir(result1)
// send records as a response
res.send(result1);
} catch (err) {
// error checks
}
})();
sql.on('error', err => {
// error handler
console.log(err);
});
});
//start listening
var port = process.env.PORT || 5321;
app.listen(port, function () {
console.log('Application started on ' + new Date());
console.log("Listening on " + port);
});
After this start your node.js app in command prompt
node test-mssql-server.js
Now open your browser and go to http://localhost:5321/
Upvotes: 3
Reputation: 294
Just run 'npm install mssql
' ---
In case after setup, you got : [...].Connection is not a constructor ==> run 'npm uninstall mssql
' and put version when install => 'npm install [email protected]
'
Hope it can help you.
Upvotes: 17
Reputation: 3924
This is because you don't have the node module installed in your system.
Install this using
npm install mssql
Upvotes: 0