Node:找不到模块Error: Cannot find module
2023-09-14 09:07:16 时间
问题
安装完模块
npm install -g crypto-js
导入模块报错
var CryptoJS = require("crypto-js");
// 运行报错 Error: Cannot find module 'crypto-js'
解决
查看安装路径
$ npm prefix -g # node安装路径
/Users/xxx/.nvm/versions/node/v10.16.0
$ npm -g root # 查看依赖安装路径
/Users/xxx/.nvm/versions/node/v10.16.0/lib/node_modules
通过 module.paths
查看模块查找列表,发现都没有
console.log(module.paths)
[ '/Users/xxx/Desktop/node_modules',
'/Users/xxx/node_modules',
'/Users/node_modules',
'/node_modules' ]
解决
方案1:直接使用模块绝对路径
var CryptoJS = require("/Users/xxx/.nvm/versions/node/v10.16.0/lib/node_modules/crypto-js");
方案2:将node_modules 路径添加到模块查找路径列表
module.paths.push("/Users/xxx/.nvm/versions/node/v10.16.0/lib/node_modules")
var CryptoJS = require("crypto-js");
方案3:添加NODE_PATH环境变量
$ vim ~/.bash_profile
# nodepath
export NODE_PATH="/Users/xxx/.nvm/versions/node/v10.16.0/lib/node_modules"
sublime下使用方案3,没有成功,文件~/.bash_profile
的变量都没有导入
相关文章
- 【nodejs原理&源码赏析(6)】深度剖析cluster模块源码与node.js多进程(下)
- 【nodejs原理&源码赏析(4)】深度剖析cluster模块源码与node.js多进程(上)
- cheerio ==> node中的jquery
- python安装及写一个简单的验证码组件(配合node)
- [Node.js] Gzip + crypto in stream
- Node.js 的模块系统
- node中的require和exports
- Node.js模块
- [Node.js] mongoose schema
- [Node.js] Serve a random static file with Express
- [Docker] Linking Node.js and MongoDB Containers
- [Node.js] What 'npm install' give us?
- 验证:使用node 12.18.4版本安装vue cli4没有问题(先卸载vue cli2再安装)
- why context node binding does not work in pop up window case
- 使用kubectl查看Kubernetes集群里的node节点信息
- node_modules 文件夹下 .bin 隐藏文件夹的作用
- 14 【node js 1/7】
- 【nodejs原理&源码赏析(4)】深度剖析cluster模块源码与node.js多线程(上)
- 【nodejs原理&源码赏析(6)】深度剖析cluster模块源码与node.js多进程
- windows下搭建node.js及npm的工作环境
- Node.js fs模块(四)文件可读流
- Node.js fs模块(三)目录操作
- npm install node-sass 报错问题的解决方案