3

I've installed Node.js, express.js and the express-generator globally.
But when I try to create a new project using the express at the CLI, I get this error:

    module.js:471
        throw err;
        ^

    Error: Cannot find module 'commander'
        at Function.Module._resolveFilename (module.js:469:15)
        at Function.Module._load (module.js:417:25)
        at Module.require (module.js:497:17)
        at require (internal/module.js:20:19)
        at Object.<anonymous> (/usr/lib/nodejs/express-generator/bin/express:3:15)
        at Module._compile (module.js:570:32)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)
        at Function.Module._load (module.js:438:

Error message advises to "re-install express-generator with sudo",
even if I haven't changed the node_modules variable path or anything else.

I haven't yet found a working solution. Please help me out on this

MwamiTovi
  • 2,425
  • 17
  • 25
  • Try removing express generator and reinstalling it again using npm. – Abhishek Singh Sep 27 '17 at 09:36
  • Yes i tried that, but i still get the above error – Himasha Harinda Sep 27 '17 at 09:41
  • I am not sure the issue is with the express generator. The error message says it cannot find module `commander`. `commander` is a dependency of `clean-css` which in turn is a dependency of `pug`. Try generating another project using another template engine, maybe ejs or handlebars let's see how that turns out. – kinsomicrote Sep 27 '17 at 11:00
  • What is so `ubuntu` about this problem? !! Also what you have written is different from what the error says – Suhail Gupta Sep 27 '17 at 12:07
  • Try adding the `commander` module – Suhail Gupta Sep 27 '17 at 12:08
  • @SuhailGupta because i develop my projects on Ubuntu and not in windows. There is no issue when working with windows. Only in Ubuntu. As i'm new to Ubuntu, please suggest me a way to add the commander module. Thanks. – Himasha Harinda Sep 27 '17 at 12:16
  • @HimashaHarinda How do you add in windows? – Suhail Gupta Sep 27 '17 at 12:18
  • @SuhailGupta in wondows i just have to install nodeJS and run `npm install express-generator -g` to install it globally. And then to create a project i just have to run `express` in the command line which cannot be done in Ubuntu as i get the above error. – Himasha Harinda Sep 27 '17 at 12:48
  • @HimashaHarinda You do the same in ubuntu or any OS, that uses npm as a package manager. But the problem here is another module. To install commander use `npm install commander --save` – Suhail Gupta Sep 27 '17 at 13:01
  • @SuhailGupta thanks, but im not saving express in the project, but globally. Therefore i dont think i have to use `--save` here. i ran the `npm install commander -g` but it still gives the same error. – Himasha Harinda Sep 27 '17 at 13:20

3 Answers3

1

I had the same issue as you and this how I fixed I uninstalled express generator then I updated npm and resinstalled express generator ,I re-arrange my path with this command PATH=~/npm/bin:$PATH and it works

0

This error originates from how you installed the express-generator tool.

Either

  • First run, npm uninstall -g express.
    Note that express-generator tool is to bootstrap express@4 projects.
    Thus, you are advised to first uninstall express globally since it only generates express@2 & @3 projects.
    This helps avoid conflicts.
  • Then run, npm install express -g express-generator.
  • For details, see official docs on migrating to express@4

Based on your question, the above two steps should resolve the issues.

Or

In case the errors persist, then consider installing the latest express-generator tool.

For my case, i had express-generator@4.0.0 but got the mentioned errors.

// Env: Windows-10, Node-v10.16.3, npm-v6.11.2

C:\>npm list -g --depth=0
  C:\Users\...\npm
  +-- express-generator@4.0.0
  // other packages

Therefor, i reinstalled the tool...

C:\>npm install -g express-generator
C:\Users\...\npm\express -> C:\Users\...\npm\node_modules\express-generator\bin\express-cli.js
+ express-generator@4.16.1
added 7 packages from 6 contributors, removed 1 package and updated 3 packages in 19.032s

// This command should run, if all went well
C:\>express --version
4.16.1
MwamiTovi
  • 2,425
  • 17
  • 25
0

I had the same issue and was able to resolve it by uninstalling both packages. Don't forget to remove the from the whole system:

sudo npm uninstall -g express
sudo npm uninstall -g express-generator

After this I reinstalled both packages using

sudo npm install -g express express-generator 

In case of error code EEXISTS it's possible that files like a symlink to '/usr/bin/express' were not removed. Those files can be overwriten using --force.

chrisb
  • 1
  • 1