0

I have been trying to solve this error with several ways, since other people had similar outputs, but still couldn’t do it completely.

I am creating an e-commerce site integrating commerce.js, after importing from '@chec/commerce.js' I had a warning (VS):

Could not find a declaration file for module '@chec/commerce.js'. '/Users/e-commerce/node_modules/@chec/commerce.js/lib/index.js' implicitly has an 'any' type.
  Try `npm install @types/chec__commerce.js` if it exists or add a new declaration (.d.ts) file containing `declare module '@chec/commerce.js';`ts(7016)

Whenever I fired the localhost, as I was expecting, I saw a related error message, not exactly the same though (will reproduce it below).
Therefore my thought was: fixing the error message in VS, will most likely fix this one as well. I manage to solve the warning by adding the new declaration file (because it seems that the package mentioned there doesn’t exist yet) So, I thought it was that, then I fired the server and the output is exactly the same as before.

TypeError: Cannot read property 'toLowerCase' of undefined
new e
node_modules/@chec/commerce.js/lib/index.js:1

Now, apparently is the first line of the file index.js in node_modules. Ok, what does it say then? (I will write here more than only the first line, just in case)


    var _objectWithoutProperties=require("@babel/runtime/helpers/objectWithoutProperties"),_toConsumableArray=require("@babel/runtime/helpers/toConsumableArray"),_typeof=require("@babel/runtime/helpers/typeof"),_defineProperty=require("@babel/runtime/helpers/defineProperty"),_classCallCheck=require("@babel/runtime/helpers/classCallCheck"),_createClass=require("@babel/runtime/helpers/createClass"),_regeneratorRuntime=require("@babel/runtime/regenerator"),_asyncToGenerator=require("@babel/runtime/helpers/asyncToGenerator"),axios=require("axios");function _interopDefaultLegacy(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}

But this is not the only one, below, another one complaining about the commerce module still

Module.<anonymous>
src/lib/commerce.js:4
  1 | // @ts-ignore
  2 | import Commerce from "@chec/commerce.js";
  3 | 
> 4 | export const commerce = new Commerce(
  5 |   process.env.REACT_APP_CHEC_PUBLIC_KEY,
  6 |   true
  7 | );

Well, now I am a bit confused about the error messages that I still see, and therefore would like to hear what do you think it could be the problem?

I check some posts here as well, such as Typescript react - Could not find a declaration file for module ''react-materialize'. 'path/to/module-name.js' implicitly has an any type and related
By the way, I am not using typescript.

Thank you in advance.

4 Answers4

1

I had the exact same error, it went away as soon as I moved the .env file outside the src/ folder. Not using TypeScript either.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

Here are some quick steps that should proly help:

  • Go the /src folder
  • And create this file chec-commerce.d.ts
    The d tells TypeScript that this is a declaration script.
  • Inside the just created file, write this:
declare module '@chec/commerce';

Based on how you are importing the module, this should clear all the errors.
Kindly confirm.

MwamiTovi
  • 2,425
  • 17
  • 25
0

Replace this:

export const commerce = new Commerce(process.env.REACT_APP_CHEC_PUBLIC_KEY, true);

with

export const commerce = new Commerce('PUBLIC_API_KEY', true);

I couldn't fix it any other way.

user9186277
  • 45
  • 1
  • 7
-1

.env file should outside the src folder and its work for me its will also work for you too...

Ankush
  • 16
  • 5
  • 1
    @Joseph How is this a "thank you" answer? It suggests that the file shouldn't be in the src folder, which looks like an answer to me (though I don't know if it is correct), and though it does seem to be low quality, it still is an answer. – Mark Rotteveel Dec 13 '20 at 09:27
  • Thanks for info . I will always remember this @joseph – Ankush Dec 13 '20 at 09:27
  • @MarkRotteveel sorry for my mistake – Joseph Dec 13 '20 at 09:40
  • It was actually that. How clumsy of me, but since I saw other people with the same problem and no one ever mentioned the env file, I completely forgot about it. – mauro avellaneda Dec 15 '20 at 14:36