-1

Constants.js

const CONTACT_SCHEMA = {
    CONTACT: CONTACT_OBJECT,
    FIRSTNAME: FIRSTNAME_FIELD,
    LASTNAME: LASTNAME_FIELD,
    EMAIL: EMAIL_FIELD,
    PHONE: PHONE_FIELD,
    FAX: FAX_FIELD
};

export default CONTACT_SCHEMA;

CreateContact.js

import { CONTACT_SCHEMA } from 'Constants';

How can I import CONTACT_SCHEMA from Constants.js into CreateContact.js

Here I am using this thing in lightning web component. I write all constants in the constants.js file and now i want to use these constants in the CreateContact file. But it's giving some error

Invalid reference Constants of type module in file createContact.js

Mitch
  • 88
  • 7
Gopal
  • 103
  • 3
  • 1
    Does this answer your question? [using brackets with javascript import syntax](https://stackoverflow.com/questions/31096597/using-brackets-with-javascript-import-syntax) – Daedalus May 17 '20 at 10:47
  • No, I tried that already but that's not working – Gopal May 17 '20 at 11:02

1 Answers1

0

You could try

const CONTACT_SCHEMA = {
    CONTACT: CONTACT_OBJECT,
    FIRSTNAME: FIRSTNAME_FIELD,
    LASTNAME: LASTNAME_FIELD,
    EMAIL: EMAIL_FIELD,
    PHONE: PHONE_FIELD,
    FAX: FAX_FIELD
};

export default CONTACT_SCHEMA 

And import

import  CONTACT_SCHEMA  from 'Constant'

Though importing like this:

import { CONTACT_SCHEMA } from 'Constant';

If you're not exporting with the default keyword

Should work. Currently it looks like Constant.js is in the same folder as the file you're importing it from, if that's not true then you'll need to amend the import to reflect. it.

To be safe always assume case sensitivity matters.

Mitch
  • 88
  • 7
  • "Invalid reference constants of type module in file CreateContact.js" This error message I am getting. I tried both import statements. – Gopal May 17 '20 at 11:11
  • Can you update your question with what you've tried. – Mitch May 17 '20 at 11:15
  • If you have a default export then you don't need the { } on the import. Read the link Daedalus posted. Change import { CONTACT_SCHEMA } from 'Constants'; to import CONTACT_SCHEMA from 'Constant'; You had an extra s, apologies if you copied that straight from my example where I added the 's' in error. – Mitch May 17 '20 at 11:27
  • File name is constants.js by mistake I forgort to write that here. I tried direct CONTACT_SCHEMA but again same error. – Gopal May 17 '20 at 11:51
  • Are Constants.js and CreateContact.js in the same folder? – Mitch May 17 '20 at 11:55
  • https://i.stack.imgur.com/AKxuO.png Here you can see my files – Gopal May 17 '20 at 11:59
  • Yes both files are in the same folder – Gopal May 17 '20 at 12:03
  • The import paths need to have a path relative to the root of the project. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules Look under the heading "Importing features into your script" So it might be '/lwc/createContact/constants' in you case, but I cannot see your whole project structure. – Mitch May 17 '20 at 12:12
  • import CONTACT_SCHEMA from '/lwc/createContact/constants'; With this again same errror. can we make a hangout call? I will be thankful to you. – Gopal May 17 '20 at 12:16
  • Update the question with what your most up to date solution. – Mitch May 17 '20 at 12:20