Node:找不到模块Error: Cannot find module

简介: Node:找不到模块Error: Cannot find module

问题

安装完模块

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 require模块找不到怎么解决?

            </div>
目录
相关文章
|
JavaScript
node下的two.js调用one.js出现无法编译问题 Cannot find module ‘c:
node下的two.js调用one.js出现无法编译问题 Cannot find module ‘c:
146 0
|
JavaScript
Syntax Error: Error: Cannot find module ‘node-sass‘
Syntax Error: Error: Cannot find module ‘node-sass‘
624 0
|
小程序
小程序wepy踩坑-Cannot find module 'D:\node_modules\npm\bin\npm-cli.js'
小程序wepy踩坑-Cannot find module 'D:\node_modules\npm\bin\npm-cli.js'
384 0
|
12月前
|
JavaScript
node环境之Error: Cannot find module ‘chalk’ 报错无法解决的问题—-网上说让你npm install chalk 基本是没有用的-优雅草央千澈解决方案
node环境之Error: Cannot find module ‘chalk’ 报错无法解决的问题—-网上说让你npm install chalk 基本是没有用的-优雅草央千澈解决方案
829 13
node环境之Error: Cannot find module ‘chalk’ 报错无法解决的问题—-网上说让你npm install chalk 基本是没有用的-优雅草央千澈解决方案
|
资源调度 JavaScript
yarn错误The engine “node“ is incompatible with this module
yarn错误The engine “node“ is incompatible with this module
751 0
报错/ ./node_modules/axios/lib/platform/index.js Module parse failed: Unexpected token (5:2)怎么解决?
报错/ ./node_modules/axios/lib/platform/index.js Module parse failed: Unexpected token (5:2)怎么解决?
|
资源调度
Cannot find module ‘\node_modules\ejs\postinstall.js
Cannot find module ‘\node_modules\ejs\postinstall.js
372 0
|
前端开发
【前端异常】Module build failed: Error: ENOENT: no such file or directory, scandir ‘G:\OPWeb\public\node_m
【前端异常】Module build failed: Error: ENOENT: no such file or directory, scandir ‘G:\OPWeb\public\node_m
683 0
若依修改,This relative module was not found:* ./@/assets/logo/logo.png in ./node_modules/css-loader/dist
若依修改,This relative module was not found:* ./@/assets/logo/logo.png in ./node_modules/css-loader/dist
若依修改,This relative module was not found:* ./@/assets/logo/logo.png in ./node_modules/css-loader/dist