Search code examples
sql-servernode.jsazurenode-mssql

Using node-sqlserver or node-mssql under Azure


I'm trying to connect to a database on Azure using node-mssql. My development system is Windows 7 and I have had no success at all getting node-sqlserver to install via WebMatrix.

My question is two part: Firstly is there any reason that even when I follow the instructions from Microsoft that I get errors with node-sqlserver? Trying the naive approach using NPM inside WebMatrix I get the following error:

An error occurred.

"C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "node-sqlserver" "--json"


Exit status 1

Failed at the [email protected] install script.
This is most likely a problem with the node-sqlserver package,
not with npm itself.
Tell the author that this fails on your system:
    node-gyp rebuild
You can get their info via:
    npm owner ls node-sqlserver
There is likely additional logging output above.



Failed: npm reported an error.

NodeNpm.NpmException: Failed: npm reported an error.
   at NodeNpm.NpmApi.Install(INpmPackage package)
   at Microsoft.WebMatrix.NpmGallery.CachingPackageManger.InstallPackage(INpmPackage package)
   at Microsoft.WebMatrix.NpmGallery.PackageViewModel.Install()
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()

The machine has VS2012 Professional installed.

The second part of my question is, is there any reason why using node-mssql won't work under Azure? I simply get a failed connection error, even though I've verified the details several times (substituted placeholders for actual values below):

var config = {
  user: '{username}',
  password: '{password}',
  server: 'tcp:{server}.database.windows.net',
  database: '{database}'
}

I get the following error:

{"name":"ConnectionError","message":"connection to tcp:{server}.database.windows.net:1433 >- failed Error: getaddrinfo ENOTFOUND"}

I realise that node-sqlserver is the best way to do it under Azure but I've tried for a couple of days with no success. Is there anything obvious I am missing?


Solution

  • 1) You can solve it by downloading precompiled node-sqlserver driver from this repo. I was not able to compile sources on my machine even with all dev dependencies installed as well.

    2) node-mssql module use three different drivers to communicate with sql server. Your config doesn't specify any driver, so the default one is used - Tedious. You must change your config to make it work correctly:

    var config = {
        user: '{username}',
        password: '{password}',
        server: '{server}.database.windows.net', // simply remove "tcp:"
        database: '{database}'
    }
    

    There is also an option to use node-mssql with node-sqlserver but to make it work, you have to install compiled node-sqlserver driver manually. Config should look like this:

    var config = {
        driver: 'msnodesql',
        user: '{username}',
        password: '{password}',
        server: 'tcp:{server}.database.windows.net',
        database: '{database}'
    }