Node.Js找不到module,module.js:471

我今天刚开始学习node.js。 我刚刚下载了最新的版本。 我一路上遇到了几个问题,但除了一个我一直得到的错误之外,我能解决其中的大部分问题:

Error: Cannot find module 'C:\Users\Dennis\sample.js' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Module.runMain (module.js:604:10) at run (bootstrap_node.js:389:7) at startup (bootstrap_node.js:149:9) at bootstrap_node.js:504:3 

这是我的代码,我从W3school获得了一个样本js代码。

 <script> var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World!'); }).listen(8080); </script> 

我的nodejs文件夹位于C:\ Program Files \ nodejs中。 我把这个sample.js文件放在文件夹中,我没有运气。 我甚至复制这个示例文件,并把它放在从C到nodejs的所有path,仍然没有。

任何提示将不胜感激。

我相当肯定,即使w3Schools不会把他们的教程。

你的文件只能包含:

 var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World!'); }).listen(8080); 

<script>标签属于html页面(或模板等)。

另外,没有文件名不需要以.js结尾来解释它。 发生什么事是require()语句要求一个文件,并且不指定扩展名将默认为一个以.js结尾的相同名字的文件。

你必须在控制台中运行,例如我有Linux并在控制台中运行:

nodejs nombre.js

那里nombre.js

var http = require('http');

 http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World!'); 

})听(8080);

那么,我检查

netstat -an

tcp46 0 0 * .8080