javascript - TypeError:无法读取未定义的属性“createContext”
问题描述
我正在尝试使用npm run build构建我的应用程序。
我得到错误为
TypeError: Cannot read property 'createContext' of undefined
.
检查解决方案后,我进行了以下更改
我尝试将react 和 react dom 升级到 ^16.4。仍然得到同样的错误。
我在我的组件中定义为 react.default.createContext() 我在我的组件中做了一个控制台,发现 react.default 是 undefined 。
正是在这部分发生了错误。
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const initStore = {
i18n: {},
query: {}
};
exports.AppContext = react_1.default.createContext(initStore);
exports.withAppContext = (Children) => {
return class App extends react_1.default.Component {
static async getInitialProps(ctx = {}) {
if (Children.hasOwnProperty('getInitialProps')) {
const context = Object.assign(ctx, {});
const childProps = (await Children.getInitialProps(context)) || {};
return Object.assign(childProps, {});
}
else {
return {};
}
}
render() {
return <exports.AppContext.Consumer>{app => <Child {...this.props} app={app}/>}</exports.AppContext.Consumer>;
}
};
};
我在下面分享了我的 package.json 文件
{
"name": "next-web",
"version": "1.0.0",
"description": "",
"main": "index.js",
"browser": {
"fs": false,
"child_process": false,
"fork": false
},
"fork": false,
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/react-transition-group": "^4.2.3",
"alertifyjs": "^1.13.1",
"apollo-cache-inmemory": "^1.6.5",
"apollo-client": "^2.6.8",
"apollo-link": "^1.2.13",
"apollo-link-context": "^1.0.19",
"apollo-link-error": "^1.1.12",
"apollo-link-http": "^1.5.16",
"apollo-link-retry": "^2.2.15",
"apollo-link-timeout": "^1.4.0",
"apollo-link-ws": "^1.0.19",
"apollo-utilities": "^1.3.3",
"aws-xray-sdk-core": "^2.5.0",
"babel-plugin-root-import": "^6.4.1",
"child_process": "^1.0.2",
"crypto-js": "^3.1.9-1",
"ctrip-apollo": "^4.4.3",
"date-fns": "^2.9.0",
"fs": "0.0.1-security",
"graphql": "^14.6.0",
"graphql-hooks-memcache": "^1.3.1",
"graphql-tag": "^2.10.1",
"isomorphic-fetch": "^2.2.1",
"jquery-param": "^1.0.2",
"js-cookie": "^2.2.1",
"koa": "^2.11.0",
"koa-bodyparser": "^4.2.1",
"koa-router": "^8.0.6",
"memcached": "^2.2.2",
"next": "^9.0.1",
"next-config": "^0.1.0",
"next-routes": "^1.4.2",
"next-server": "^9.0.5",
"polished": "^3.4.4",
"qrcode": "^1.4.4",
"react": "^16.12.0",
"react-adopt": "^0.6.0",
"react-apollo": "^3.1.3",
"react-app-rewired": "^2.1.5",
"react-copy-to-clipboard": "^5.0.2",
"react-day-picker": "^7.4.0",
"react-dnd": "^10.0.2",
"react-dnd-html5-backend": "^10.0.2",
"react-dom": "^16.12.0",
"react-lazyload": "^2.6.5",
"react-progressive-image": "^0.6.0",
"react-slick": "^0.25.2",
"react-transition-group": "^4.3.0",
"styled-components": "^5.0.0",
"styled-tools": "^1.7.1",
"subscriptions-transport-ws": "^0.9.16",
"ua-parser-js": "^0.7.21",
"webpack-node-externals": "^1.7.2",
"ycb": "^2.1.1"
},
"devDependencies": {
"babel-preset-es2015": "^6.24.1",
"react-dev-utils": "^10.1.0"
}
}
是某种版本问题吗?我也使用 Nextjs 作为框架而不是反应
解决方案
推荐阅读
- css - 我可以在本书的 6.2 版中使用最新的布尔玛吗
- postgresql - postgis设置更新问题?
- javascript - 当面板已经打开时滑动切换面板(向下滑动)
- c++ - 程序语言C++中的三角角问题
- linux - 在没有 IIS 的情况下为相同地址和端口上的许多站点提供服务
- c# - 此异步/等待代码可能导致死锁?
- android - Kotlin RecyclerView 内容大小高度
- android - 三星 Galaxy S9:TYPE_GEOMAGNETIC_ROTATION_VECTOR 不存在
- powerbi - Power BI:将切片器值添加到表中
- ios - 未能集成 Swift 和 Objective-C 框架