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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/mouday/article/details/100168155

问题

安装完模块

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

文章创建于:
展开阅读全文

Error: Cannot find module '../build/Debug/nodegit.node'

02-05

Error: Cannot find module '../build/Debug/nodegit.node'rn at Function.Module._resolveFilename (module.js:538:15)rn at Function.Module._load (module.js:468:25)rn at Module.require (module.js:587:17)rn at require (internal/module.js:11:18)rn at Object. (/home/slave/doit2/node_modules/nodegit/dist/nodegit.js:18:12)rn at Module._compile (module.js:643:30)rn at Object.Module._extensions..js (module.js:654:10)rn at Module.load (module.js:556:32)rn at tryModuleLoad (module.js:499:12)rn at Function.Module._load (module.js:491:3)rn at Module.require (module.js:587:17)rn at require (internal/module.js:11:18)rn at Object. (/home/slave/doit2/app/controller/zookeeper.js:11:13)rn at Module._compile (module.js:643:30)rn at Object.Module._extensions..js (module.js:654:10)rn at Module.load (module.js:556:32) code: 'MODULE_NOT_FOUND' rnrn-------------如下rnrn[root@ doit2]# npm install nodegit -frnnpm WARN using --force I sure hope you know what you are doing.rnrn> nodegit@0.20.3 install /home/slave/doit2/node_modules/nodegitrn> node lifecycleScripts/preinstall && node lifecycleScripts/installrnrn[nodegit] Running pre-install scriptrn[nodegit] Configuring libssh2.rn Error: Command failed: /home/slave/doit2/node_modules/nodegit/vendor/libssh2/configure --with-libssl-prefix=/home/slave/doit2/node_modules/nodegit/vendor/openssl/opensslrn/home/slave/doit2/node_modules/nodegit/vendor/libssh2/missing: Unknown `--is-lightweight' optionrnTry `/home/slave/doit2/node_modules/nodegit/vendor/libssh2/missing --help' for more informationrnconfigure: WARNING: 'missing' script is too old or missingrnconfigure: error: No crypto library found!rnTry --with-libssl-prefix=PATHrn or --with-libgcrypt-prefix=PATHrn or --with-wincng on Windowsrnrn at ChildProcess.exithandler (child_process.js:275:12)rn at emitTwo (events.js:126:13)rn at ChildProcess.emit (events.js:214:7)rn at maybeClose (internal/child_process.js:925:16)rn at Socket.stream.socket.on (internal/child_process.js:346:11)rn at emitOne (events.js:116:13)rn at Socket.emit (events.js:211:7)rn at Pipe._handle.close [as _onclose] (net.js:554:12)rn killed: false,rn code: 1,rn signal: null,rn cmd: '/home/slave/doit2/node_modules/nodegit/vendor/libssh2/configure --with-libssl-prefix=/home/slave/doit2/node_modules/nodegit/vendor/openssl/openssl' rn/home/slave/doit2/node_modules/nodegit/vendor/libssh2/missing: Unknown `--is-lightweight' optionrnTry `/home/slave/doit2/node_modules/nodegit/vendor/libssh2/missing --help' for more informationrnconfigure: WARNING: 'missing' script is too old or missingrnconfigure: error: No crypto library found!rnTry --with-libssl-prefix=PATHrn or --with-libgcrypt-prefix=PATHrn or --with-wincng on Windowsrnrn[nodegit] ERROR - Could not finish preinstallrn Error: Command failed: /home/slave/doit2/node_modules/nodegit/vendor/libssh2/configure --with-libssl-prefix=/home/slave/doit2/node_modules/nodegit/vendor/openssl/opensslrn/home/slave/doit2/node_modules/nodegit/vendor/libssh2/missing: Unknown `--is-lightweight' optionrnTry `/home/slave/doit2/node_modules/nodegit/vendor/libssh2/missing --help' for more informationrnconfigure: WARNING: 'missing' script is too old or missingrnconfigure: error: No crypto library found!rnTry --with-libssl-prefix=PATHrn or --with-libgcrypt-prefix=PATHrn or --with-wincng on Windowsrnrn at ChildProcess.exithandler (child_process.js:275:12)rn at emitTwo (events.js:126:13)rn at ChildProcess.emit (events.js:214:7)rn at maybeClose (internal/child_process.js:925:16)rn at Socket.stream.socket.on (internal/child_process.js:346:11)rn at emitOne (events.js:116:13)rn at Socket.emit (events.js:211:7)rn at Pipe._handle.close [as _onclose] (net.js:554:12)rn killed: false,rn code: 1,rn signal: null,rn cmd: '/home/slave/doit2/node_modules/nodegit/vendor/libssh2/configure --with-libssl-prefix=/home/slave/doit2/node_modules/nodegit/vendor/openssl/openssl' rnrn> nodegit@0.20.3 postinstall /home/slave/doit2/node_modules/nodegitrn> node lifecycleScripts/postinstallrnrn[nodegit] WARN - Could not finish postinstallrn Error: Command failed: node "/home/slave/doit2/node_modules/nodegit/dist/nodegit.js"rnmodule.js:540rn throw err;rn ^rnrnError: Cannot find module '../build/Debug/nodegit.node'rn at Function.Module._resolveFilename (module.js:538:15)rn at Function.Module._load (module.js:468:25)rn at Module.require (module.js:587:17)rn at require (internal/module.js:11:18)rn at Object. (/home/slave/doit2/node_modules/nodegit/dist/nodegit.js:18:12)rn at Module._compile (module.js:643:30)rn at Object.Module._extensions..js (module.js:654:10)rn at Module.load (module.js:556:32)rn at tryModuleLoad (module.js:499:12)rn at Function.Module._load (module.js:491:3)rnrn at ChildProcess.exithandler (child_process.js:275:12)rn at emitTwo (events.js:126:13)rn at ChildProcess.emit (events.js:214:7)rn at maybeClose (internal/child_process.js:925:16)rn at Socket.stream.socket.on (internal/child_process.js:346:11)rn at emitOne (events.js:116:13)rn at Socket.emit (events.js:211:7)rn at Pipe._handle.close [as _onclose] (net.js:554:12)rn killed: false,rn code: 1,rn signal: null,rn cmd: 'node "/home/slave/doit2/node_modules/nodegit/dist/nodegit.js"' rnnpm WARN ajv-keywords@2.1.1 requires a peer of ajv@^5.0.0 but none is installed. You must install peer dependencies yourself.rnnpm WARN doit@1.0.0 No repository field.rnrn+ nodegit@0.20.3rnupdated 1 package in 35.139s 论坛

没有更多推荐了,返回首页