最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【未解决】Azure上运行sql示例代码出错:Error: Cannot find module ‘node-sqlserver’

Node.js crifan 5396浏览 0评论

【问题】

通过参考官网的:

How to: Connect to a SQL Database instance

去写的代码:

//https://www.windowsazure.com/en-us/develop/nodejs/how-to-guides/sql-database/#ConnectionInfo
var sql = require('node-sqlserver');
var conn_str = "Server=tcp:xxx.database.windows.net,1433;Database=xxx;User ID=xxx;Password=xxx;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;";

var http = require('http')
var port = process.env.port||3000;
http.createServer(function (req, res) {
    sql.query(conn_str, "SELECT * FROM TestTable", function (err, results) {
        if (err) {
            res.writeHead(500, { 'Content-Type': 'text/plain' });
            res.write("Got error :-( " + err);
            res.end("");
            return;
        }
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        for (var i = 0; i < results.length; i++) {
            res.write("ID: " + results[i].ID + " Column1: " + results[i].Column1 + " Column2: " + results[i].Column2);
        }
        res.end("; Done.");
    });
}).listen(port);

结果出错:

iisnode encountered an error when processing the request.

HRESULT: 0x2
HTTP status: 500
HTTP reason: Internal Server Error
You are receiving this HTTP 200 response because system.webServer/iisnode/@devErrorsEnabled configuration setting is 'true'.

In addition to the log of stdout and stderr of the node.exe process, consider using debugging and ETW traces to further diagnose the problem.

The last 64k of the output generated by the node.exe process to stderr is shown below:

Application has thrown an uncaught exception and is terminated:
Error: Cannot find module 'node-sqlserver'
    at Function._resolveFilename (module.js:337:11)
    at Function._load (module.js:279:25)
    at Module.require (module.js:359:17)
    at require (module.js:375:17)
    at Object.<anonymous> (C:\DWASFiles\Sites\uyb-dev\VirtualDirectory0\site\wwwroot\server.js:10:11)
    at Module._compile (module.js:446:26)
    at Object..js (module.js:464:10)
    at Module.load (module.js:353:31)
    at Function._load (module.js:311:12)
    at Module.require (module.js:359:17)

【解决过程】

1.  想到了,之前参考:

How to: Connect to a SQL Database instance

中所提到的,下载了

node-sqlserver

并安装,结果实际上sql名为:

msnodesql,对应的package.json中也是这个值:

{
  "name": "msnodesql",
  "description": "Microsoft Driver for Node.js for SQL Server",
  "author": "Microsoft Corp.",
  "contributors": [ "Jonathan Guerin <[email protected]>", "Jay Kint <[email protected]>", "Warren Read <[email protected]>", "George Yan <[email protected]>", "Inga Verbitskaya <[email protected]>"],
  "version": "0.2.0",
  "keywords": ["sql", "database", "mssql", "azure", "sqlserver", "tds", "microsoft", "msnodesql", "node-sqlserver"],
  "homepage": "https://github.com/WindowsAzure/node-sqlserver",
  "bugs": "https://github.com/WindowsAzure/node-sqlserver/issues",
  "licenses": [
    {
      "type": "Apache 2.0",
      "url": "https://raw.github.com/WindowsAzure/node-sqlserver/master/LICENSE"
    }
  ],
  "main": "./lib/sql.js",
  "repository": {
    "type": "git",
    "url": "git://github.com/WindowsAzure/node-sqlserver.git" 
  },
  "engines": {
    "node": ">=0.6"
  },
  "devDependencies": {
     "mocha" : "0.14.x",
     "async" : "0.1.x"
  },
  "os" : [ "win32" ]
}

所以才想到,把上述的

var sql = require(‘node-sqlserver’);

换为:

var sql = require(‘msnodesql’);

结果同样出错:

Error: Cannot find module 'msnodesql'

2.

转载请注明:在路上 » 【未解决】Azure上运行sql示例代码出错:Error: Cannot find module ‘node-sqlserver’

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.207 seconds, using 22.11MB memory