简体   繁体   English

Nodejs无法找到模块

[英]Nodejs Cannot find module

I'm getting an error when trying to use any global module, exemple: 我在尝试使用任何全局模块时遇到错误,例如:

Error: Cannot find module 'express'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (C:\BitNami\wappstack\...\test\app.js)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)

I installed the express command: 我安装了express命令:

npm install -g express

My app.js: 我的app.js:

var express = require('express');

And run it using windows powershell or node.js command prompt windows: 并使用windows powershell或node.js命令提示符窗口运行它:

node app.js

do not really know what's going wrong, I read something about environment variables in windows, can this be? 我真的不知道出了什么问题,我在windows中阅读了一些关于环境变量的内容,这可以吗?

Resolved / Update 已解决/更新

The problem was: Windows environment variables was not configured for npm folder. 问题是:没有为npm文件夹配置Windows环境变量。 Search for your npm folder and add the path in the environment variables. 搜索您的npm文件夹并在环境变量中添加路径。

Just to quote from here: 只是引用这里:

https://www.npmjs.org/doc/files/npm-folders.html https://www.npmjs.org/doc/files/npm-folders.html

  • Install it locally if you're going to require() it. 如果你想要()它在本地安装它。
  • Install it globally if you're going to run it on the command line. 如果要在命令行上运行它,请全局安装它。
  • If you need both, then install it in both places, or use npm link . 如果您需要两者,请在两个位置安装它,或使用npm链接

You should install Express locally: 您应该在本地安装Express:

npm install express

Then require it as you did: 然后像你一样要求它:

var express = require('express')

I was getting same error on Windows7/x64 and adding following in the environment variable resolved the issue: 我在Windows7 / x64上遇到同样的错误,并在环境变量中添加以下内容解决了问题:

NODE_PATH=C:\Users\[USERNAME]\AppData\Roaming\npm\node_modules

*Replace [USERNAME] with your actual system username *将[USERNAME]替换为您的实际系统用户名

I'm working in Linux, but when I require express, I'm doing so with a relative path to where it is installed and it works fine: 我在Linux工作,但是当我需要快递时,我正在这样做,它有一个相对路径,它安装在哪里,它工作正常:

var express = require('./public/node_modules/express');

I'm sure the same thing would work with a windows path as well. 我确信同样的东西也适用于Windows路径。 If you want to be more explicit and declare an absolute path, that would be the nuclear option to make sure you always know exactly where your module is being loaded from regardless of where your scripts are being run from. 如果你想要更明确并声明一个绝对路径,那么这将是核选项,以确保你始终确切知道模块的加载位置,无论你的脚本在哪里运行。

If you still have a problem after using an explicit path, I don't know what the problem might be. 如果在使用显式路径后仍然有问题,我不知道问题可能是什么。 . .

另一个选择是运行npm install --save express

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM