简体   繁体   English

node.js require 找不到自定义模块

[英]node.js require cannot find custom module

Here is the project structure:这是项目结构:

/
  app.js
  package.json
  /node_modules
  /app
    config.json
    /frontend
      assets and html tpls
    /modules
      couch.js
      raeume.js
      users.js

I require config.json, raeume.js and users.js from app.js and it all works fine.我需要来自 app.js 的 config.json、raeume.js 和 users.js,一切正常。

var config   = require('./app/config');
var raeume   = require('./app/modules/raeume');
var users    = require('./app/modules/users');

Then I require config.json and couch.js from user.js the same way and it won't find anything.然后我以相同的方式从 user.js 中获取 config.json 和 couch.js,但它找不到任何东西。

var couch     = require('./app/modules/couch');
var config    = require('./app/config');

I guess it should find it.我想它应该能找到它。 After some research I saw a diverse landscape of problems inclusive how node is compiled.经过一些研究,我看到了各种各样的问题,包括如何编译节点。 Thus included: I work on osx 10.8 with node v0.10.7.因此包括:我在 osx 10.8 上工作,节点 v0.10.7。

The path is relative to the directory in which you are require ing the files, so it should be something like:该路径相对于您require文件的目录,因此它应该类似于:

var couch = require('./couch');
var config = require('../config');

A bit of clarification, if you write有点澄清,如果你写

var couch = require('./couch');

you are trying to require the couch module which resides in the current directory, if you write你正试图require驻留在当前目录中的沙发模块,如果你写

var couch = require('couch');

you are trying to require the couch module installed via npm .您试图require通过npm安装沙发模块。

这是你如何做到的:

var users    = require('./../modules/users');

It must be:一定是:

var config = require(../../app/config)

var couch = require(./couch) (same directory)

The current accepted answer is correct and properly answers the question.当前接受的答案是正确的,并正确回答了问题。

Although not directly related to the original question, I'd like to add another point here for all those people who got the Cannot find module error for local files although the correct relative path was specified.虽然与原始问题没有直接关系,但我想在这里为所有那些虽然指定了正确的相对路径但收到本地文件Cannot find module错误的人补充一点。

Assuming a file with the name couch.js exists in the current directory,假设当前目录中存在一个名为couch.js的文件,

  • On case insensitive filesystems (like NTFS on Windows), both these lines will work -在不区分大小写的文件系统(如 Windows 上的 NTFS)上,这两行都可以使用 -

     var couch = require('./couch'); var couch = require('./Couch');
  • However, on a case-sensitive filesystem (like ext4 on Linux), require('./couch') will work but require('./Couch') will not.但是,在区分大小写的文件系统(如 Linux 上的 ext4)上, require('./couch')可以工作,但require('./Couch')不行。

There is a page in the NodeJS docs regarding this. NodeJS 文档中一个关于此的页面

I hope this helps someone whose perfectly working code stopped working after moving from Windows/Mac to Linux.我希望这可以帮助那些在从 Windows/Mac 迁移到 Linux 后完美运行的代码停止工作的人。 😅 😅

Also double check the extension on the file being imported.还要仔细检查正在导入的文件的扩展名。 I accidentally used .ts in a JavaScript project, and this prevented it from being found.我不小心在 JavaScript 项目中使用了.ts ,这导致它无法被发现。 Once I changed the extension to .js , require was able to find it fine.一旦我将扩展名更改为.jsrequire就能够很好地找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM