我有一个项目,使用反应和Mobx与Mobx-react。
我的项目在本地运行得很好。然而,当使用webpack -p构建时,我在控制台中得到一个空白屏幕,并显示以下错误:
webpack:///./~/mobx-react/index.js?:3 Uncaught Error: Cannot find module "mobx"
at webpackMissingModule (webpack:///./~/mobx-react/index.js?:3)
at webpackUniversalModuleDefinition (webpack:///./~/mobx-react/index.js?:3)
at eval (webpack:///./~/mobx-react/index.js?:10)
at Object.<anonymous> (bundle.js:18)
at n (bundle.js:1)
at eval (webpack:///./src/components/Category.jsx?:35)
at Object.<anonymous> (bundle.js:27)
at n (bundle.js:1)
at eval (webpack:///./src/components/CategoryNavsDates.jsx?:15)
at Object.<anonymous> (bundle.js:14)这是我的webpack配置:
var path = require('path');
var webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const autoprefixer = require('autoprefixer')
var CopyWebpackPlugin = require('copy-webpack-plugin');
var BUILD_DIR = path.resolve(__dirname, 'build/');
var SOURCE_DIR = path.resolve(__dirname, 'src/');
module.exports = {
devtool: 'eval',
entry: SOURCE_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
cacheDirectory: true,
plugins: ['transform-decorators-legacy'],
presets: ['es2015', 'stage-0', 'react']
}
},
{
test: /\.css$/,
use: [
'style-loader',
{ loader: 'css-loader', options: { modules: true, importLoaders: 1, localIdentName: "[name]__[local]___[hash:base64:3] "} },
{ loader: 'postcss-loader', options: {} },
]
},
],
},
resolve: {
extensions: ['.js', '.jsx'],
},
plugins: [
new webpack.LoaderOptionsPlugin({
options: {
postcss: function () {
return [
require('postcss-import'),
require('postcss-cssnext'),
];
},
}
}),
new CopyWebpackPlugin([{ from: 'index.html', to: '' },])
],
devServer: {
historyApiFallback: true
},
};在我的整个项目中只有一个使用Mobx的文件,那就是错误所指的文件Category.jsx。
Category.jsx示例:
import { observer } from 'mobx-react'
import { observable } from 'mobx'
...
@observer class Category extends React.Component {
@observable showingSmallMenu = false
...
}正如我所说的,这一切在本地都运行得很好。
这里会有什么问题呢?
发布于 2017-04-11 14:26:01
如果在mobx-react之前导入mobx会有什么不同吗
发布于 2018-12-29 11:38:07
在我的例子中,这是由以下原因引起的:
alias: {
mobx: __dirname + '/node_modules/mobx/lib/mobx.es6.js'
}去掉mobx别名,问题就解决了
https://stackoverflow.com/questions/43329688
复制相似问题