Search code examples
node.jssql-serverexpresstedious

Connect to SQL Server Express edition from node.js driver mssql (Tedious wrapper)


TypeError [ERR_INVALID_URL]: Invalid URL:
mssql://testuser:[email protected]\SQLEXPRESS/MyTestDB

The node package is: https://www.npmjs.com/package/mssql

As per their Quick Example

await sql.connect('mssql://username:password@localhost/database')

I am doing this

await sql.connect('mssql://testuser:[email protected]\\SQLEXPRESS/MyTestDB');

My details are:

  • SQL Server instance: 192.168.1.70\SQLEXPRESS
  • DB name: MyTestDB
  • uid/pwd: testuser, testuser

I have no issues connecting from SQL Server Management Studio from a remote machine, so what is the problem in this code?


Solution

  • json object works, not plain string !

        let config = {
            user: 'testuser',
            password: 'testuser',
            server: '192.168.1.70\\SQLEXPRESS',  
            database: 'MyTestDB'
        }
    

    Update: From a github issue it became clearer the Quick Example format is a Uri, not a connection string.

    So instead of:

    'mssql://testuser:[email protected]\\SQLEXPRESS/MyTestDB'
    

    I should use the below, which I've since confirmed also works!

    'mssql://testuser:[email protected]/SQLEXPRESS/MyTestDB'