question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[v5] cannot do truly dynamic import: Property value expected type of string but got null

See original GitHub issue

Describe the bug

This works:

    componentDidMount = () => {
      import('../Test.mdx').then(Thing =>
        this.setState({ Thing: Thing.default })
      );
    };

but this doesnt work

    componentDidMount = () => {
      const path = '../Test.mdx';
      import(path).then(Thing =>
        this.setState({ Thing: Thing.default })
      );
    };

It generates this opaque compile error:

Failed to build! Fix any errors and try again!
./src/containers/Home.js
Module build failed: TypeError: /Users/swyx/Work/react-static-mdx/src/containers/Home.js: Property value expected type of string but got null

I assume it has something to do with this babel plugin: https://github.com/airbnb/babel-plugin-dynamic-import-node/issues/15

Is this “truly dynamic” kind of import supported? or am I doing something wrong?

Desktop (please complete the following information):

  • OS: ios
  • Browser: chrome
  • Version: v5.9.12

Additional context just trying to make dynamically importing components based on route data

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
ScriptedAlchemycommented, Dec 10, 2018

Alrighty! Good luck! I know context switching can get to us when the quantity starts increasing.

Here if you need me!

1reaction
montezumecommented, Dec 1, 2018

@sw-yx if this helps, this seems to work.


const path = "components/MyComponent";

const MyComponent = universal(import(`../${path}`), {
  loading: Loading,
  error: Failed
});

or more similar to your example

  componentDidMount() {
    const path = "info/test.json";
    import(`../${path}`).then(response => {
      console.log(response);
    });
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Property value expected type of string but got null; Dynamic ...
I am having issues using dynamic imports with variables in the path name. Trying to load dynamically the language.
Read more >
Property value expected type of string but got null
The issue was do to the ES6 syntax, that works fine: export default function Icon({ type }) { return ( <div> <img src={iconSrc[type]} ......
Read more >
Module Methods - webpack
A normal import statement cannot be used dynamically within other logic or contain variables. See the spec for more information and import() below...
Read more >
getter - JavaScript - MDN Web Docs - Mozilla
The get syntax binds an object property to a function that will be called when that property is looked up. It can also...
Read more >
Errors | Node.js v19.3.0 Documentation
code property is a string label that identifies the kind of error. error.code is the most stable way to identify an error. It...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found