7

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.

3 Answers 3

17

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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/

Comments

0

This is because you don't have the node module installed in your system. Install this using
npm install mssql

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.