Express module not found when installed with NPM

后端 未结 14 1540
庸人自扰
庸人自扰 2020-12-22 22:59

When I try to run the app.js file created by express, I get the following error:

$ node app.js

node.js:134
        throw e; // process.nextTick         


        
相关标签:
14条回答
  • 2020-12-22 23:17

    I installed gulp and when I ran this gulp command in the command line I got a gulp: command not found error. It appeared that it installed gulp in my local folder that is /home/YOURUSERNAME/.node/lib/node_modules and not in the global npm folder.

    You can check npm root folder by running this command: npm root -g, which was returning my personal directory /home/YOURUSERNAME/.node/lib/node_modules and not the expected /usr/local/lib/node_modules.

    You can fix this by running npm config set prefix /usr/local command.

    0 讨论(0)
  • 2020-12-22 23:18

    It looks like the easiest way to do this is to run npm install from your app's folder. This tells npm to hook everything up.

    It's the last instruction after express <appname>:

    ...
    dont forget to install dependencies:
    $ cd <appname> && npm install
    
    0 讨论(0)
  • 2020-12-22 23:19

    It appears that while npm had been updated to install global modules into /usr/local/lib/node_modules, Node's own require.paths does not yet reflect this change.

    There are two reasonable solutions:

    1. Add the following code to the top of your application:

      require.paths.push('/usr/local/lib/node_modules');
      
      • Pro: non-invasive, easy to add

      • Con: requires discipline, future versions of node will restrict access to require.paths

    2. As root, execute:

      ln -s /usr/local/lib/node_modules /usr/local/lib/node
      
      • Pro: reasonably non-invasive

      • Con: requires root, modifies linux fs, might not survive system updates

    0 讨论(0)
  • 2020-12-22 23:21

    I had the same problem. This worked for me though:

    Seems like npm (now?) installs node modules to /usr/local/lib/node_modules/ and not /usr/local/lib/node/

    What I did was simply to copy everything from node_modules to node: sudo cp -r /usr/local/lib/node_modules/* usr/local/lib/node/ and now it seems to be working for me.

    Hope this helps you :-)

    0 讨论(0)
  • 2020-12-22 23:22

    On Ubuntu 12.04 you have to add the export NODE_PATH=/usr/local/lib/node_modules to your /.bashrc to use globally installed modules.

    0 讨论(0)
  • 2020-12-22 23:23

    for mac users

    cd /usr/local/lib/node
    sudo ln -s ../node_modules/* ./$1
    
    0 讨论(0)
提交回复
热议问题