Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs Cannot find module

Tags:

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:

npm install -g express 

My app.js:

var express = require('express'); 

And run it using windows powershell or node.js command prompt windows:

node app.js 

do not really know what's going wrong, I read something about environment variables in windows, can this be?

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.

like image 672
Mateus Vahl Avatar asked Jan 10 '14 14:01

Mateus Vahl


People also ask

Can not find module in node JS?

If you are getting the "Cannot find module" error when trying to run a local file, make sure that the path you passed to the node command points to a file that exists. For example, if you run node src/index. js , make sure that the path src/index. js points to an existing file.

Can not find module npm?

To fix Cannot find module errors, install the modules properly by running a npm install command in the appropriate directory as your project's app. js or index. js file. or delete the node_modules folder and package-lock.

Can not find the module Express?

To solve the error "Cannot find module 'express'", install the package by running the command npm install express in the root directory of your project. If you don't have a package. json file, create one by running npm init -y . The error occurs when we try to import the express package without installing it.


1 Answers

Just to quote from here:

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.
like image 127
Luca Reghellin Avatar answered Sep 30 '22 03:09

Luca Reghellin