Nodejs找不到模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21047030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:37:43  来源:igfitidea点击:

Nodejs Cannot find module

node.jsnode-modules

提问by Mateus Vahl

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. Search for your npm folder and add the path in the environment variables.

问题是:没有为 npm 文件夹配置 Windows 环境变量。搜索您的 npm 文件夹并在环境变量中添加路径。

采纳答案by Hien Khieu

You should install Express locally:

您应该在本地安装 Express:

npm install express

Then require it as you did:

然后像你一样要求它:

var express = require('express')

回答by Luca Reghellin

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 locallyif you're going to require() it.
  • Install it globallyif you're going to run it on the command line.
  • If you need both, then install it in both places, or use npm link.
  • 如果您要 require() 它,请在本地安装它。
  • 如果要在命令行上运行它,请全局安装它。
  • 如果两者都需要,则在两个地方都安装它,或者使用npm link

回答by Ovais

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] 替换为您的实际系统用户名

回答by Brian

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 上工作,但是当我需要 express 时,我使用的是它安装位置的相对路径,并且工作正常:

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

I'm sure the same thing would work with a windows path as well. 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.

我相信同样的事情也适用于 Windows 路径。如果您想更明确并声明绝对路径,这将是确保您始终确切知道从哪里加载模块的核心选项,而不管您的脚本从哪里运行。

If you still have a problem after using an explicit path, I don't know what the problem might be. . .

如果使用显式路径后仍然有问题,我不知道可能是什么问题。. .

回答by coderwithattitude

another option will be to run npm install --save express

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