2

I'm a javascript newbie and I'm trying to figure out why this import statement fails. I don't know if this is relevant but I'm doing this in the context of a Create React app with its default webpack setup. I have these files:

TestClass.js

export default class TestClass {
    sayHello() {
        console.log("Hello World.");
    }
}

TestModule.js

import TestClass from "./TestClass";

module.exports = {
    MyExport: {
        doSomething: function() {
            let testClass = new TestClass();
            testClass.sayHello();
        }
    }
}

And then my main React Application file, App.js:

import React, { Component } from "react";
import "./App.css";
import { MyExport } from "./TestModule"

class App extends Component {
    constructor(props) {
        super(props);

        MyExport.doSomething();

        ... bla bla bla ...
}

When I attempt to run this in Node.js, it returns

Failed to compile.

./src/App.js
Attempted import error: 'MyExport' is not exported from './TestModule'.

What am I doing wrong?

2 Answers 2

1

You are mixing es6 modules with Commonjs style imports. Your TestModule should be exported as follows:

import { TestClass } from "./TestClass";

export const MyExport = {
  doSomething: function () {
    let testClass = new TestClass();
    testClass.sayHello();
  }
};

This should work while allowing the rest of your code to remain the same

Sign up to request clarification or add additional context in comments.

4 Comments

You're right. This works - thanks. I'd like to understand more, though. Is mixing of imports this way prohibited? Is this because webpack and/or Node looks at a file when compiling and makes some determination about what kind of "thing" it is?
@user12798778 The require type syntax is mainly used within NodeJS and is supported natively by that environment. ES6 style imports as you are attempted to use are resolved during build time by Webpack, which essentially resolves all your imports and pastes them together in one big file. While webpack is able to support both CommonJS and ES6 types of import you are not able to mix and match these different module methods as you have done in your code. You can read more about how Webpack does this here webpack.js.org/api/module-methods
@user12798778 the main takeaway is that if you export your module using module.exports you should import it using require('...'), or you can export using the es6 module export syntax which means you have to import your module by import ... from .... Just dont mix different export and import methods because Webpack won't know how to handle these cases.
Got it. Thanks - much appreciated.
1

You can try this:

import { TestClass } from "./TestClass";

const Object = {
   MyExport: {
    doSomething: function() {
      let testClass = new TestClass();
      testClass.sayHello();
    }
  }
}
export default MyExport

As you are using two different type of import export

Or if you have to use module.exports you can try this:

import { TestClass } from "./TestClass";

module.exports = function () {
    return {
        MyExport: {
          doSomething: function() {
           let testClass = new TestClass();
           testClass.sayHello();
          }
       }
    }
}

You can try exporting function instead of object

2 Comments

Is that true that module.exports should export a function, not an object? It seems like I can find lots of examples where that is not the case (e.g. sitepoint.com/understanding-module-exports-exports-node-js).
I am not saying you can't use object, I was just suggesting you to use function. Changing that statement in my answer as it can be misguiding.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.