我最近嘗試使用 tsdx 創建一個 react typescript 組件進行編譯,但在除錯時遇到了一些問題。該軟體包已發布,并且似乎正在發布和安裝而沒有錯誤。然而,當談到實際使用時,我總是遇到:
TypeError: Cannot read properties of null (reading 'useEffect').
我有同樣的問題,useContext所以簡單地替換了功能,但現在錯誤已遷移到useEffect.
這個包在本地運行完全正常,如果我洗掉所有代碼并將其手動注入應用程式,它可以毫無問題地運行。最初我只是在解構useEffect鉤子,react但即使顯式匯入reactasimport React from 'react'; react仍然很明顯null......
查看.cjs檔案我可以看到 React 被匯入:
var React = _interopDefault(require('react'));
但問題仍然存在。
感謝任何和所有幫助,如果有幫助,將從我的 package.json 中粘貼一些資訊:)
{
"name": "xxxx",
"version": "x.x.x",
"description": "xxxx",
"author": "xxx xxx",
"keywords": [],
"main": "dist/index.js",
"module": "dist/xxx.esm.js",
"typings": "dist/index.d.ts",
"engines": {
"node": ">=10"
},
"files": [
"dist",
"src",
"README.md"
],
"repository": {
"type": "git",
"url": "git https://github.com/xxxx/xxxx.git"
},
"dependencies": {
"@babel/polyfill": "^7.12.1",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.5.0",
"@types/node": "^16.11.34",
"@types/react": "^18.0.9",
"@types/react-dom": "^18.0.3",
"axios": "^0.27.2",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-scripts": "5.0.1",
"typescript": "^4.6.4",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "npx tsdx build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@babel/cli": "^7.17.10",
"@babel/core": "^7.17.12",
"@babel/preset-env": "^7.17.12"
},
"bugs": {
"url": "https://github.com/xxxx/xxxx/issues"
},
"homepage": "https://github.com/xxxx/xxxx#readme",
"license": "ISC"
}
編輯:另外,如果值得注意 - 這個專案沒有初始化為 tsdx 專案,我在切換之前遇到打包問題后,將 tsdx 構建腳本和一些元素添加到 tsconfig 和package.json
uj5u.com熱心網友回復:
只需為遇到此問題的任何其他人留個便條,在我的包中,我的常規dependencies物件中有“反應”和許多其他依賴項,package.json當我將這些移至devDependencies.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/480584.html
上一篇:NPM安裝無CD檔案夾
