慕容708150
2023-11-12 14:29:57
我一直在嘗試用多種方法解決這個(gè)錯(cuò)誤,因?yàn)槠渌艘灿蓄愃频妮敵?,但仍然無法完全做到。我正在創(chuàng)建一個(gè)集成 commerce.js 的電子商務(wù)網(wǎng)站,從“@chec/commerce.js”導(dǎo)入后,我收到了警告(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)每當(dāng)我觸發(fā)本地主機(jī)時(shí),正如我所期望的那樣,我都會(huì)看到一條相關(guān)的錯(cuò)誤消息,但并不完全相同(將在下面重現(xiàn))。因此我的想法是:修復(fù) VS 中的錯(cuò)誤消息,很可能也會(huì)修復(fù)這個(gè)問題。我設(shè)法通過添加新的聲明文件來解決警告(因?yàn)樗坪跆岬降陌€不存在)所以,我認(rèn)為是這樣,然后我啟動(dòng)了服務(wù)器,輸出與以前完全相同。TypeError: Cannot read property 'toLowerCase' of undefinednew enode_modules/@chec/commerce.js/lib/index.js:1現(xiàn)在,顯然是node_modules 中文件index.js 的第一行。好吧,那它說了什么?(為了以防萬一,我會(huì)在這里寫不止第一行) 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}}但這不是唯一的一個(gè),下面還有另一個(gè)抱怨商務(wù)模塊的問題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 | );好吧,現(xiàn)在我對(duì)仍然看到的錯(cuò)誤消息有點(diǎn)困惑,因此想聽聽您認(rèn)為這可能是什么問題?我也在這里查看了一些帖子,例如Typescript React - Could not find a declaration file for module ''react-materialize'。'path/to/module-name.js' 隱式具有任何類型和相關(guān)順便說一句,我沒有使用打字稿。
4 回答

慕田峪9158850
TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超7個(gè)贊
我遇到了完全相同的錯(cuò)誤,當(dāng)我將 .env 文件移到 src/ 文件夾之外時(shí),它就消失了。也不使用 TypeScript。

哈士奇WWW
TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊
替換這個(gè):
export const commerce = new Commerce(process.env.REACT_APP_CHEC_PUBLIC_KEY, true);
和
export const commerce = new Commerce('PUBLIC_API_KEY', true);
我無法以任何其他方式修復(fù)它。

胡子哥哥
TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊
以下是一些應(yīng)該會(huì)有所幫助的快速步驟:
轉(zhuǎn)到
/src
文件夾并創(chuàng)建這個(gè)文件
chec-commerce.d.ts
告訴d
TypeScript 這是一個(gè)聲明腳本。在剛剛創(chuàng)建的文件中,寫入以下內(nèi)容:
declare module '@chec/commerce';
根據(jù)您導(dǎo)入模塊的方式,這應(yīng)該會(huì)清除所有錯(cuò)誤。
請(qǐng)確認(rèn)。

紫衣仙女
TA貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊
.env 文件應(yīng)該位于 src 文件夾之外,它對(duì)我有用,它也對(duì)你有用......
添加回答
舉報(bào)
0/150
提交
取消