NodeJS:cannot find module 'socket.io' | nmp全局安装 modules 后在应用中 require 不到的解决方案
2013-05-28 19:58
615 查看
Windows环境下, 通过 npm install -g 安装的全局模块, 可能无法在app中被require引用到, 怎么办?
其实, 只要设置环境变量
我的解决方法是:
我的电脑->属性-> 高级-> 环境变量, 添加一个环境变量名为
将其值设置为全局模块的根目录.
我这里这个目录是:
目录结构如下:
socket.js代码如下:
配置好环境变量 执行 node socket.js成功..
其实, 只要设置环境变量
NODE_PATH指向全局模块根目录即可.
我的解决方法是:
我的电脑->属性-> 高级-> 环境变量, 添加一个环境变量名为
NODE_PATH,
将其值设置为全局模块的根目录.
我这里这个目录是:
C:\Program Files\nodejs\node_modules
目录结构如下:
socket.js代码如下:
// 引入需要的模块:http和socket.io var http = require('http'), io = require('socket.io'); //创建server var server = http.createServer(function(req, res){ // Send HTML headers and message res.writeHead(200,{ 'Content-Type': 'text/html' }); res.end('<h1>Hello Socket Lover!</h1>'); }); //端口9999 server.listen(9999); //创建socket var socket = io.listen(server); //添加连接监听 socket.on('connection', function(client){ //连接成功则执行下面的监听 client.on('message',function(event){ console.log('Received message from client!',event); }); //断开连接callback client.on('disconnect',function(){ console.log('Server has disconnected'); }); });
配置好环境变量 执行 node socket.js成功..
相关文章推荐
- Windows下 NodeJS 全局安装 modules 后在应用中 require 不到的解决方案
- nodejs报错:Cannot find module 'ejs'
- Cordova安装Cannot find module 'bplist-parser'的错误
- macOS 下 npm 全局安装仍然提示 Cannot find module 'xxx' 的解决方案
- nodeJs启动的时候:Error: Cannot find module '../build/Release/bson'的解决办法
- Error- Cannot find module 'gulp-clone'问题的解决
- Error- Cannot find module 'gulp-clone'问题的解决
- Nodejs Error: Cannot find module 'unicode/category/So'
- Error- Cannot find module 'gulp-clone'问题的解决
- Windows安装Nodejs的模块和引用问题(cannot find moudel "xxx")?
- torch7-- errors using gfx in the tutorial--Cannot find module 'pty.js'--[gfx.js] rendering cell
- Cannot find module '../build/Debug/iconv.node
- Android NDK: jni/Android.mk: Cannot find module with tag 'cocos2dx' in import path解决办法
- Cocos2dx Cannot find module with tag 'CocosDenshion/android' in import path
- Error- Cannot find module 'gulp-clone'问题的解决
- Android NDK: jni/Android.mk: Cannot find module with tag 'CocosDenshion/android' in import path
- Windows node.js npm install express -g Cannot find module 'express'
- Android NDK: jni/Android.mk: Cannot find module with tag 'cocos2dx' in import path解决办法
- cocos2dx在Eclipse下编译报错:Cannot find module with tag 'CocosDenshion/android' in import path
- Cocos2d-x 2.2.3 使用NDK配置安卓编译环境问题之 Cannot find module with tag 'CocosDenshion/android' in import path