Node.js – 找不到模块'websocket'

我是新来的node.js,我需要使用一个模块websocket 。 我的服务器:

 $ cat /etc/centos-release CentOS Linux release 7.2.1511 (Core) $ node --version v6.11.5 $ npm --version 3.10.10 

$ npm install websocket安装了一个模块,但是当我尝试我的文件时,它给了我这个:

 $ node /var/www/js/test.js module.js:471 throw err; ^ Error: Cannot find module 'websocket' 

这是我的JS文件的第一行:

 var WebSocketClient = require('websocket').client; 

我也尝试了以下命令:

 $ npm install websocket (with and without -g flag) $ npm link websocket $ npm install -d 

但没有什么帮助。

package.json

 { "name": "root", "version": "1.0.0", "description": "", "main": "index.js", "dependencies": { "websocket": "^1.0.25" }, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" } 

添加test.js,假设它位于你的节点目录的根目录下:

process.chdir(__dirname);

要么:

cd /var/www/js/ && node test.js

工作目录需要是node_modulespackage.json所在的node_modules

要打印节点应用程序的工作目录:

console.log(process.cwd());

你必须尝试这个var websocket = require('websocket'); 在你的文件中