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>
目录
相关文章
|
3月前
|
JavaScript
node下的two.js调用one.js出现无法编译问题 Cannot find module ‘c:
node下的two.js调用one.js出现无法编译问题 Cannot find module ‘c:
48 0
|
20天前
报错/ ./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)怎么解决?
|
5月前
|
资源调度 JavaScript
yarn错误The engine “node“ is incompatible with this module
yarn错误The engine “node“ is incompatible with this module
|
4月前
(node)Warning: Accessing non-existent property ‘xxx‘ of module exports inside circular depen
(node)Warning: Accessing non-existent property ‘xxx‘ of module exports inside circular depen
|
5月前
vue-demi@0.13.11: The engine “node“ is incompatible with this module. Expected version “>=12“. Got “
vue-demi@0.13.11: The engine “node“ is incompatible with this module. Expected version “>=12“. Got “
|
5月前
|
JavaScript
Node.js Error: Cannot find module express
Node.js Error: Cannot find module express
node Express.js node:internal/modules/cjs/loader:988 throw err; ^ Error: Cannot find module ‘
node Express.js node:internal/modules/cjs/loader:988 throw err; ^ Error: Cannot find module ‘
1014 0
node Express.js node:internal/modules/cjs/loader:988 throw err; ^ Error: Cannot find module ‘
|
10月前
|
JavaScript 前端开发
前端工程化的Node.js之代码的组织/部署的模块 module
随着前端项目越来越复杂,前端工程化变得愈加重要。Node.js 作为前端工程化的重要组成部分,其模块机制在代码的组织和部署方面扮演了至关重要的角色。
64 0
【node报错解决方案】Error: Cannot find module http-errors
【node报错解决方案】Error: Cannot find module http-errors
263 0
【node报错解决方案】Error: Cannot find module http-errors
|
JavaScript
Node:找不到模块Error: Cannot find module
Node:找不到模块Error: Cannot find module
73 0