简体   繁体   English

为什么在Node.js中找不到模块?

[英]How come I can't find a module in nodejs?

So I'm working off of https://github.com/mhart/react-server-example/blob/master/server.js to figure out server side rendering in React. 所以我正在研究https://github.com/mhart/react-server-example/blob/master/server.js以弄清楚React中的服务器端渲染。 It takes advantage of browserify. 它利用了browserify的优势。

The example is a flat folder structure cause it's just that, an example. 该示例是一个平面文件夹结构,仅此而已。 So below here are some of my changes that are confusing me. 因此,下面是一些令我困惑的更改。

/server/controllers/bundles.jsx has this /server/controllers/bundles.jsx有这个

browserify()
  .add('./app/scripts/checkout.jsx')
  .transform(literalify.configure({
    'react': 'window.React',
    'react-dom': 'window.ReactDOM',
  }))
  .bundle()
  .pipe(res)

At this point I gather that even though I'm nested in a folder, browserify is working off of root. 至此,即使我嵌套在一个文件夹中,我仍然认为browserify不能在root上运行。 Meanwhile inside checkout.jsx I have this: 同时在checkout.jsx我有这个:

require('./app/scripts/components/checkout/layout');

When I boot up my server, it can't find the file. 当我启动服务器时,它找不到文件。 I also tried doing ./components/checkout/layout since that's where it's sitting in relation to checkout.jsx . 我还尝试做./components/checkout/layout因为那是与checkout.jsx

No matter what I try, my server says it can't find the layout module. 无论我尝试什么,我的服务器都说找不到布局模块。

Update I added console.log for __dirname and saw /app/scripts so it makes me think require('components/checkout/layout') should hit /app/scripts/components/checkout/layout but it's still not found. 更新我为__dirname添加了console.log并看到了/app/scripts因此使我认为require('components/checkout/layout')应该达到/app/scripts/components/checkout/layout但仍未找到。 I should also note that the file does do module.exports = Layout and is being required just fine in another file while being used on the server. 我还应注意,该文件确实执行module.exports = Layout并且在服务器上使用该文件时,在另一个文件中也需要该文件。

You need to add the file extension ( .jsx ). 您需要添加文件扩展名( .jsx )。

Basically, require in node has a bunch of rules it goes through, all outlined in the docs here . 基本上, require in node具有一系列规则, 所有规则都在此处的文档中概述

In your case, the most relevant is this part: 就您而言,最相关的部分是:

require(X) from module at path Y
1. If X is a core module,
   a. return the core module
   b. STOP
2. If X begins with './' or '/' or '../'
   a. LOAD_AS_FILE(Y + X)
   b. LOAD_AS_DIRECTORY(Y + X)
3. LOAD_NODE_MODULES(X, dirname(Y))
4. THROW "not found"

LOAD_AS_FILE(X)
1. If X is a file, load X as JavaScript text.  STOP
2. If X.js is a file, load X.js as JavaScript text.  STOP
3. If X.json is a file, parse X.json to a JavaScript Object.  STOP
4. If X.node is a file, load X.node as binary addon.  STOP

You're hitting #2, and then going to the LOAD_AS_FILE , and there's no case for jsx files by default there, so you need the extension to make it match #1. 您到达#2,然后转到LOAD_AS_FILE ,并且默认情况下jsx文件没有大小写,因此需要扩展名使其与#1匹配。

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

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