Why does require not behave the same as import when same structure is given

supersize

It seems I have a fundamental lack of understanding when it comes to import vs require. From what I've read I know the difference is that require is based on a module loaders like CommonJS while import is actually an ES6 feature.

Assuming the following:

import ExamplePost from 'example-post.md'

This is an example import with MDX JS as appropriate loader and I set this in my React render function like:

render () {
  return <ExamplePost />
}

The above works perfectly fine, but as I want to dynamically load different Markdown files and I have read that ES6 import are static I intended to go like:

let postName = 'example-post'
const ExamplePost = require(`${postName}.md`)

Unfortunately it doesn't work, I am getting: ExamplePost is not defined

I'm setting both examples at the top of the document. I also inspected both versions and I can see a difference in the outcome:

ES6 import returns: [Function]

Require returns: Object [Module] { default: [Function] }

Help to get me on the right track on why the require above does not work the same?

UjinT34

You import the default export of module and require the module itself.

const ExamplePost = require(`${postName}.md`).default

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

Also check Can't require() default export value in Babel 6.x

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does my group by clause behave differently for similar data from the same table?

why the elements of the list is not in the same structure?

Why does Calendar.getDisplayName always return the same day of week when given milliseconds?

When int? and Nullable<int> are the same, why do they behave in a different way?

Why does make behave differently when given a absolute path vs a local path for -C option?

Why does "require" behave differently when I am in the same directory or a parent directory?

Why does python's `datetime.strptime` function not behave the same way when called using `functools.partial`?

Why does numpy import behave differently?

Javascript, using require() and import in the same file

Why does java.lang.process's readline() behave differently for reading inputstream on different boxes with the same os

how does openfire server behave when one recipient got packet from multiple sender at the same time?

Why does plot behave differently for same but scaled data?

Does `exec < somefile` behave the same as `source somefile`

Why does PowerShell behave differently than cmd.exe given the same command?

Why whould xterm behave differently than x-terminal-emulator when it points to the same executable?

ES6 Promise - Why does throw from catch() and throw from then() not behave the same way?

ContextFlyout in RelativePanel does not behave same way as in Grid

Why does git behave this way? Inconsistency between OS and VM accessing the same repository

Why does "require" compile but "import" does not?

Why Val and Var behave same for arrays in koltin?

Why does completely same function behave differently in ipython/jupyter?

Why does a match on covariant enum does not behave the same as with a sealed trait?

why does var behave differently in a with statement depending on whether or not the passed object has a property with the same name?

Why does this Python module import a class from the same module?

Why does boost::numeric::interval::widen not behave the same as manually applying the same logic

Why does String.contains behave differently when I import Foundation?

When applying the same style to a component and a 'div' element, why do they behave differently?

Why when I export shapefile to CSV in R the file result does not maintain the same structure?

Why Apache CollectionUtils.containsAny does not require Collection of same type

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive