1

I'm trying to create a custom class called helpers.js which looks like this:

class Httprequest{
    constructor(type, url, username, password,email){
        this.type = type;
        this.url = url;
        this.request = new XMLHttpRequest();
        this.username = username;
        this.password = password;
        this.email = email;
    }
    static sendPostRequest(){

        // function(method,url,async,user,password);
        this.request.open(this.type,this.url);
        this.request.setRequestHeader('Content-Type' ,'application/x-www-form-urlencoded');
        const data = encodeURI('username=' + this.username + '&'+
                                'password=' + this.password + '&'+
                                'email=' + this.email);
        this.request.send(data);
        if(this.request.readyState === 4){
            const status = this.request.status;
                if(status === 201){
                    return this.request.status;
                }else{
                    return "Failed creating account"
                }
        }
    }

}

export default Httprequest;

Then I'm trying to import the class in my React Component called Projects.js

import Httprequest from 'helpers.js';

I get error which looks like this: Failed to compile.

./src/components/Projects.js
Module not found: Can't resolve 'helpers.js' in 'C:\Users\Account\Desktop\front\frontend\src\components'

Both files are in the same folder, what gives?

3 Answers 3

1

could you try:

import Httprequest from './helpers.js';

You need to explicitly indicate your current folder like this.

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

4 Comments

Thank you, this worked. Does the syntax './helpers.js' mean it will go one folder back? Do I always have to add the ./ in front to import classes?
great. the syntax './helpers.js' means the same folder. It is Unix like file system structure. '../helpers.js' would mean the folder above, and '../../helpers.js' two folders above.
Oh, so the amount of dots actually matters. Thank you!
Just a quick note that you probably don't want to add the .js file extension in your import.
1

Here are all types of import syntax:

import defaultExport from "module-name";
import * as name from "module-name";
import { export } from "module-name";
import { export as alias } from "module-name";
import { export1 , export2 } from "module-name";
import { export1 , export2 as alias2 , [...] } from "module-name";
import defaultExport, { export [ , [...] ] } from "module-name";
import defaultExport, * as name from "module-name";
import "module-name";

The problem with your import is that you need to use ./helpers.js if in the same directory. It's same as UNIX file system structure.

1 Comment

0

Try this:

 import Httprequest from './helpers.js';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.