2012-01-26 18:57

使用 Node.js 时,如何解决"无法找到模块"错误?



> npm install ../faye


> npm list
└─┬ faye@0.7.1
  ├── cookiejar@1.3.0
  ├── hiredis@0.1.13
  └── redis@0.7.1


> node app.js
        throw e; // process.nextTick error, or 'error' event on first tick
Error: Cannot find module 'faye'
    at Function._resolveFilename (module.js:334:11)
    at Function._load (module.js:279:25)
    at Module.require (module.js:357:17)
    at require (module.js:368:17)
    at Object.<anonymous> (/home/dave/src/server/app.js:2:12)
    at Module._compile (module.js:432:26)
    at Object..js (module.js:450:10)
    at Module.load (module.js:351:31)
    at Function._load (module.js:310:12)
    at Array.0 (module.js:470:10)


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • csdnceshi52 妄徒之命 9年前

    Using npm install installs the module into the current directory only (in a subdirectory called node_modules). Is app.js located under home/dave/src/server/? If not and you want to use the module from any directory, you need to install it globally using npm install -g.

    I usually install most packages locally so that they get checked in along with my project code.

    Update (3/2016):

    I've received a lot of flak for my response, specifically that I check in the packages that my code depends on. A few days ago, somebody unpublished all of their packages (https://medium.com/@azerbike/i-ve-just-liberated-my-modules-9045c06be67c#.kq9s64clp) which broke React, Babel, and just about everything else. Hopefully it's clear now that if you have production code, you can't rely on NPM actually maintaining your dependencies for you.

    点赞 29 评论 复制链接分享
  • csdnceshi75 衫裤跑路 6年前

    I had a very similar issue. Removing the entire node_modules folder and re-installing worked for me:

    rm -rf node_modules
    npm install
    点赞 26 评论 复制链接分享
  • csdnceshi76 斗士狗 4年前

    For TypeScript users, if you are importing a built-in Node module (such as http, path or url) and you are getting an error such as "Cannot find module "x" then the error can be fixed by running

    npm install @types/node --save-dev

    The command will import the NodeJS TypeScript definitions into your project, allowing you to use Node's built-in modules.

    点赞 10 评论 复制链接分享
  • csdnceshi70 笑故挽风 4年前

    This error can be encountered if you are requireing a module that has a missing or incorrect main field in its package.json. Though the module itself is installed, npm/node has to use a single .js file as an entrypoint to your module. If the main field is not there, it defaults to looking for index.js in your module's folder. If your module's main file is not called index.js, it won't be able to require it.

    Discovered while turning a browserify-based module into a CommonJS require-able module; browserify didn't care about the missing main field, and so the error had gone unnoticed.

    点赞 10 评论 复制链接分享
  • csdnceshi75 衫裤跑路 6年前
    npm install --save module_name

    For example, if the error is:

    { [Error: Cannot find module '/root/.npm/form-data'] code: 'MODULE_NOT_FOUND' }

    then you can resolve this issue by executing the command npm install --save form-data.

    点赞 8 评论 复制链接分享
  • csdnceshi66 必承其重 | 欲带皇冠 5年前

    Remove your node_module root folder from your project(eg: myApp). Go to myApp folder and then type below command from terminal

    >myApp>npm install

    It will install all the dependency modules required for your project.

    点赞 7 评论 复制链接分享
  • csdnceshi62 csdnceshi62 4年前

    I faced the same problem when someone else in the team updated package.json in SVN. Merely removing the node_modules directory did not help. How I solved the problem is:

    rm -rf node_modules
    rm package.json
    rm package-lock.json
    svn up
    npm install
    ng build --env=prod

    Hope this helps someone!

    点赞 6 评论 复制链接分享
  • csdnceshi58 Didn"t forge 5年前

    If you use nvm, check that existing node_modules that are bindings to other libraries are compiled for the correct Node.js version.

    I was having the same error. The reason was the following: We use nvm since we're running two apps on a server, one requires Node.js 5.6 because it uses node-gd (which doesn't run on Node.js 6 for now), the other requires Node.js 6. Node.js 6 is the apt-get installation.

    Also we use the pm2 tool to deploy.

    So, the default setup is that the pm2 process starts when nvm is not in effect, so it uses the apt-get installation of Node.js (version 6). So the main pm2 daemon starts with Node.js 6. If I run applications in fork mode they start in separate processes and nvm settings are in effect. When I run applications in cluster mode - they inherit the non-nvm environment.

    So when I tried to switch to the cluster mode the application failed to start because the bindings compiled for 5.6 fail with this message.

    I've fixed that by restarting pm2 when nvm setings are in effect. Also startup scripts should be fixed.

    点赞 5 评论 复制链接分享
  • csdnceshi53 Lotus@ 6年前

    This happens when a first npm install has crashed for some reason (SIGINT of npm), or that the delay was too long, or data is corrupted. Trying an npm install again won't save the problem.

    Something got wrong on the npm first check, so the best choice is to remove the file and to restart npm install.

    点赞 5 评论 复制链接分享
  • csdnceshi54 hurriedly% 5年前

    PRO TIP:

    This error happened to me, while fighting fatigue and mild illness, because I typed node blah instead of npm blah.

    The error message received wasn't angry enough to alert me to my own folly!

    点赞 5 评论 复制链接分享
  • csdnceshi59 ℙℕℤℝ 5年前

    I experienced this error yesterday. Took me a while to realise that the main entry in package.json was pointing to a file that I'd moved. Once I updated that the error disappeared and the package worked.

    点赞 3 评论 复制链接分享
  • csdnceshi57 perhaps? 3年前

    Check if the enviroment variable NODE_PATH is set correctly and pointing to the node_modules path. nodejs uses this variable to search for the libraries

    点赞 3 评论 复制链接分享
  • csdnceshi80 胖鸭 4年前

    Specify the path to the restler folder, which will be inside node_modules folder like : var rest = require('./node_modules/restler');

    This worked for me.

    点赞 3 评论 复制链接分享
  • csdnceshi60 ℡Wang Yan 5年前

    I was trying to publish my own package and then include it in another project. I had that issue because of how I've built the first module. Im using ES2015 export to create the module, e.g lets say the module looks like that:

    export default function(who = 'world'){
        return `Hello ${who}`;

    After compiled with Babel and before been published:

    'use strict';
    Object.defineProperty(exports, "__esModule", {
        value: true
    exports.default = function () {
        var who = arguments.length <= 0 || arguments[0] === undefined ? 'world' : arguments[0];
        return 'Hello ' + who;

    So after npm install module-name in another project (none ES2015) i had to do

    var hello = require('module-name').default;

    To actually got the package imported.

    Hope that helps!

    点赞 评论 复制链接分享
  • csdnceshi61 derek5. 4年前

    Encountered this problem while using webpack with webpack-dev-middleware.

    Had turned a single file into a folder.

    The watcher seemed to not see the new folder and the module was now missing.

    Fixed by restarting the process.

    点赞 评论 复制链接分享
  • csdnceshi76 斗士狗 4年前

    Run the following command only:

    npm install

    It will install all the required modules in the node_modules folder.

    点赞 评论 复制链接分享
  • csdnceshi77 狐狸.fox 3年前

    Maybe like me you set 'view engine' in express to an engine that doesn't exist, or tried to use an unregistered templating engine. Make sure that you use: app.engine('engine name',engine) app.set('view engine','engine name')

    点赞 评论 复制链接分享
  • csdnceshi70 笑故挽风 5年前

    First of all, yes, a part of my answer definitely is helpful to solve the error that is posted by OP. Secondly, after trying the below step, I faced a couple of other errors, and so, have written the solution of those too.

    (Psst! I am not sure if I've successfully helped in solving the above error, or if I've broken some rule or format of answering, but I faced the above error and some others and it took much time for me to find the proper solutions for those errors. I'm writing the complete solution because in case, if someone else also faces these errors, then he'll hopefully get a solution here.)

    So adding to, and elaborating the answer provided by PrashanthiDevi, and also adding my personal experience, here it is:

    I am new to the whole e2e and unit tests part. I started looking into this part from Protractor. Now I already had the files in which tests were written, but I had to run the tests.

    I had already installed all the required softwares and tools, but when I initially ran the code for running the tests, gulp itest, I got this 'Cannot find module' Error. After going through many different questions on SO, I found one answer that I thought could help getting a solution.

    The person had suggested to run the command npm install in my project folder.

    The reason for doing this was to update the node-modules folder, inside our project folder, with all the required and necessary files and dependencies.

    (The below part maybe irrelevant with this question, but might be helpful if anyone came across the same situation that I faced.)

    The above step surely solved my previous error, but threw a new one! This time the error being Could not find chromedriver at '..\node_modules\protractor\selenium\chromedriver'.

    However, the solution of this error was pretty silly (and funny) to me. I already had the chromedriver file in my selenium folder. But, turns out that the above error was coming because my chromedriver files were inside selenium folder and not inside chromedriver folder. So, creating a chromedriver folder and copying the chromedriver files there solved my problem!

    Also, for the error: Timed out waiting for the WebDriver Server, you could add this line of code to conf.js file inside exports.config{}:

    seleniumAddress: 'http://localhost:8080/'

    Hope this helps!

    点赞 评论 复制链接分享
  • csdnceshi53 Lotus@ 3年前

    Please install the new CLI v3 (npm install -g ionic@latest).

    If this issue is still a problem in CLI v3. Thank you!

    点赞 评论 复制链接分享
  • csdnceshi60 ℡Wang Yan 4年前

    In my case I had UNMET PEER DEPENDENCY redux@^3.0.0 causing this error message, see all of them and install missing modules again using --save

    npm install redux --save
    点赞 评论 复制链接分享
  • weixin_41568208 北城已荒凉 4年前

    I can add one more place to check; the package that I was trying to use was another one of my own packages that I had published to a private NPM repo. I had forgotten to configure the 'main' property in the package.json properly. So, the package was there in the node_modules folder of the consuming package, but I was getting "cannot find module". Took me a few minutes to realise my blunder. :-(

    点赞 评论 复制链接分享
  • weixin_41568126 乱世@小熊 4年前

    Just found an unusual scenario that may be of use to someone and is sort of a red herring.

    I was also getting the Cannot Find Module error but oddly everything worked perfectly in my local (Mac hosted) Node.js environment. This problem only appeared when the code was deployed on our Linux server.

    Well... it turned out to be a typo that (apparently) the Mac based Node.js installation was perfectly happy to ignore.

    The include looked like this:

    var S3Uploader = require('./S3Uploader.class');

    But the actual file was called "s3Uploader.class.js"

    Notice the casing difference in the 's' vs. 'S' between the code and the filename.

    So - in the odd chance that none of the other solutions here are solving your problem, triple check that you're not mis-casing the characters in your included filename! :)

    and DUH!

    点赞 评论 复制链接分享
  • csdnceshi70 笑故挽风 5年前

    I got the simple solution to solve this issue.

    Just remove C:\Users\UserName\AppData\Roaming\npm\

    and then install node.

    Now your problem will be solved.

    点赞 评论 复制链接分享