Can't import React component on server-side Next.jsReact js onClick can't pass value to methodHow to import a css file in a react component?How to use React components from local npm packageServing static images with WebpackComponent files not importing in webpack react project?Imported components transformed into “/static” urls in react-create-appCreate react app App fails when importing custom libraryTesting React component with Jest gives 'Cannot find module' errorReact router doesn't work on express serverReact won't import my module “Module not found: Can't resolve ”

Looking after a wayward brother in mother's will

Are there mythical creatures in the world of Game of Thrones?

How can I offer a test ride while selling a bike?

Is there a way to save this session?

What does the behaviour of water on the skin of an aircraft in flight tell us?

What are the problems in teaching guitar via Skype?

Order by does not work as I expect

Looking for an old image of designing a cpu with plan laid out / being edited on a literal floor

Is there an evolutionary advantage to having two heads?

How do I get a list of only the files (not the directories) from a package?

TV show or movie: Diseased people are exiled to a spaceship

How do I get a cleat that's stuck in a pedal, detached from the shoe, out?

Why does my electric oven present the option of 40A and 50A breakers?

If Sweden was to magically float away, at what altitude would it be visible from the southern hemisphere?

Humans meet a distant alien species. How do they standardize? - Units of Measure

How much current can Baofeng UV-5R provide on +V pin?

Can an old DSLR be upgraded to match modern smartphone image quality

How crucial is a waifu game storyline?

How to detach yourself from a character you're going to kill?

Select * from View takes 4 minutes

Is American Express widely accepted in France?

Can a helicopter mask itself from Radar?

How do I set the Verbatim font (or the mono font) to bold by default?

California: "For quality assurance, this phone call is being recorded"



Can't import React component on server-side Next.js


React js onClick can't pass value to methodHow to import a css file in a react component?How to use React components from local npm packageServing static images with WebpackComponent files not importing in webpack react project?Imported components transformed into “/static” urls in react-create-appCreate react app App fails when importing custom libraryTesting React component with Jest gives 'Cannot find module' errorReact router doesn't work on express serverReact won't import my module “Module not found: Can't resolve ”






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















I'm trying to import a very simple component from a library into a Next.js app.



I have stripped down my library to the bare minimum. It contains the following src/components/index.js:



import React from 'react'
const Container = ( children ) => <div>children</div>
export Container


In my Next.js app, I created a pages/index.js that contains the following:



import React from 'react'
import Container from 'mylib'

class HomePage extends React.Component
render()
return <Container>Hello</Container>



export default HomePage


mylib is a local npm package which I have installed in my Next.js app using npm i ../mylib. It has a standard webpack config:



const path = require('path')

module.exports =
entry:
components: './src/components/index.js',
,
output:
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
libraryTarget: 'commonjs2'
,
mode: "production",
module:
rules: [

test: /.js$/,
exclude: /node_modules/,
use:
loader: 'babel-loader',


],
,
;


and then the package.json points to dist/components.js as the main file.



The following does work:



  1. In my Next.js app, replace return <Container>Hello</Container> with return <div>Hello</div> (while keeping the import Container in place)

  2. Refresh the page in the server (I'm running Next's dev server)

  3. Restore the use of <Container> (the opposite of step 1)

  4. Save the file --> hot-reload in the browser with the Container properly displayed (I added a className to be sure)

At this point, if I refresh the browser, I get the following error:




Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.




which suggests I made a bad import/export, but I don't know where. And the error only occurs on the server-side.



What did I do wrong?










share|improve this question




























    2















    I'm trying to import a very simple component from a library into a Next.js app.



    I have stripped down my library to the bare minimum. It contains the following src/components/index.js:



    import React from 'react'
    const Container = ( children ) => <div>children</div>
    export Container


    In my Next.js app, I created a pages/index.js that contains the following:



    import React from 'react'
    import Container from 'mylib'

    class HomePage extends React.Component
    render()
    return <Container>Hello</Container>



    export default HomePage


    mylib is a local npm package which I have installed in my Next.js app using npm i ../mylib. It has a standard webpack config:



    const path = require('path')

    module.exports =
    entry:
    components: './src/components/index.js',
    ,
    output:
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js',
    libraryTarget: 'commonjs2'
    ,
    mode: "production",
    module:
    rules: [

    test: /.js$/,
    exclude: /node_modules/,
    use:
    loader: 'babel-loader',


    ],
    ,
    ;


    and then the package.json points to dist/components.js as the main file.



    The following does work:



    1. In my Next.js app, replace return <Container>Hello</Container> with return <div>Hello</div> (while keeping the import Container in place)

    2. Refresh the page in the server (I'm running Next's dev server)

    3. Restore the use of <Container> (the opposite of step 1)

    4. Save the file --> hot-reload in the browser with the Container properly displayed (I added a className to be sure)

    At this point, if I refresh the browser, I get the following error:




    Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.




    which suggests I made a bad import/export, but I don't know where. And the error only occurs on the server-side.



    What did I do wrong?










    share|improve this question
























      2












      2








      2








      I'm trying to import a very simple component from a library into a Next.js app.



      I have stripped down my library to the bare minimum. It contains the following src/components/index.js:



      import React from 'react'
      const Container = ( children ) => <div>children</div>
      export Container


      In my Next.js app, I created a pages/index.js that contains the following:



      import React from 'react'
      import Container from 'mylib'

      class HomePage extends React.Component
      render()
      return <Container>Hello</Container>



      export default HomePage


      mylib is a local npm package which I have installed in my Next.js app using npm i ../mylib. It has a standard webpack config:



      const path = require('path')

      module.exports =
      entry:
      components: './src/components/index.js',
      ,
      output:
      path: path.resolve(__dirname, 'dist'),
      filename: '[name].js',
      libraryTarget: 'commonjs2'
      ,
      mode: "production",
      module:
      rules: [

      test: /.js$/,
      exclude: /node_modules/,
      use:
      loader: 'babel-loader',


      ],
      ,
      ;


      and then the package.json points to dist/components.js as the main file.



      The following does work:



      1. In my Next.js app, replace return <Container>Hello</Container> with return <div>Hello</div> (while keeping the import Container in place)

      2. Refresh the page in the server (I'm running Next's dev server)

      3. Restore the use of <Container> (the opposite of step 1)

      4. Save the file --> hot-reload in the browser with the Container properly displayed (I added a className to be sure)

      At this point, if I refresh the browser, I get the following error:




      Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.




      which suggests I made a bad import/export, but I don't know where. And the error only occurs on the server-side.



      What did I do wrong?










      share|improve this question














      I'm trying to import a very simple component from a library into a Next.js app.



      I have stripped down my library to the bare minimum. It contains the following src/components/index.js:



      import React from 'react'
      const Container = ( children ) => <div>children</div>
      export Container


      In my Next.js app, I created a pages/index.js that contains the following:



      import React from 'react'
      import Container from 'mylib'

      class HomePage extends React.Component
      render()
      return <Container>Hello</Container>



      export default HomePage


      mylib is a local npm package which I have installed in my Next.js app using npm i ../mylib. It has a standard webpack config:



      const path = require('path')

      module.exports =
      entry:
      components: './src/components/index.js',
      ,
      output:
      path: path.resolve(__dirname, 'dist'),
      filename: '[name].js',
      libraryTarget: 'commonjs2'
      ,
      mode: "production",
      module:
      rules: [

      test: /.js$/,
      exclude: /node_modules/,
      use:
      loader: 'babel-loader',


      ],
      ,
      ;


      and then the package.json points to dist/components.js as the main file.



      The following does work:



      1. In my Next.js app, replace return <Container>Hello</Container> with return <div>Hello</div> (while keeping the import Container in place)

      2. Refresh the page in the server (I'm running Next's dev server)

      3. Restore the use of <Container> (the opposite of step 1)

      4. Save the file --> hot-reload in the browser with the Container properly displayed (I added a className to be sure)

      At this point, if I refresh the browser, I get the following error:




      Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.




      which suggests I made a bad import/export, but I don't know where. And the error only occurs on the server-side.



      What did I do wrong?







      javascript reactjs next.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 24 at 11:16









      ArnaudArnaud

      375520




      375520






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323226%2fcant-import-react-component-on-server-side-next-js%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323226%2fcant-import-react-component-on-server-side-next-js%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript