node.js, Error: Cannot find module 'express'

后端 未结 18 1764
情话喂你
情话喂你 2020-12-02 08:57

I am new to Node.js, try to learn express to build my first web application. I got stuck on my very first sample code and need some help to get it running. Before I post th

相关标签:
18条回答
  • 2020-12-02 09:42

    I had the same problem. My issue was that I have to change to the Node.js project directory on the command line before installing express.

    cd /Users/feelexit/WebstormProjects/learnnode/node_modules/
    
    0 讨论(0)
  • 2020-12-02 09:43

    Unless you set Node_PATH, the only other option is to install express in the app directory, like npm install express --save. Express may already be installed but node cannot find it for some reason

    0 讨论(0)
  • 2020-12-02 09:44

    Check if you have installed express module. If not, use this command:

    npm install express
    

    and if your node_modules directory is in another place, set NODE_PATH envirnment variable:

    set NODE_PATH=your\directory\to\node_modules;%NODE_PATH%
    
    0 讨论(0)
  • 2020-12-02 09:44

    I'm guessing that this is coursework from Colt Steele's Web Development course... I was looking for the same answer as to why I ended up with that error too.. Colt doesn't say so but you take the node_module folder and move into the new folder you're working in... that's what worked for me.

    0 讨论(0)
  • 2020-12-02 09:46

    if youre main file is located at /Users/feelexit/WebstormProjects/learnnode/node_modules/index.js then express needs to be located at /Users/feelexit/WebstormProjects/learnnode/node_modules/node_modules as node always looks for modules in ./node_modules (and its internal folder) when the path dont start with ./ or / (more info here)

    i think you miss placed youre main file in the module folder

    0 讨论(0)
  • 2020-12-02 09:46

    npm ERR! Error: ENOENT, open '/Users/feelexit/package.json'

    This happens due to missing permissions or unlinked files while npm was working.

    Meaning, that executing npm as this user doesn't have enough rights to read/write from a file, in this case package.json.

    try adding sudo before the entire command - it should resolve.

    $ sudo npm install -g express
    $ Password:*******

    Password would be your admin password of your mac.

    -g flag will install this module (express) in the global context of node - meaning node will/should recognize express module from within any js file without having to provide a full path to the module in use.

    Hope this helps!!

    0 讨论(0)
提交回复
热议问题