我正在从头开始创建Webpack配置,当我尝试使用npm脚本构建或启动我的项目(开发和生产模式)时,出现错误“未安装Webpack”。
建议安装Webpack ... 安装webpack开始捆绑:$ npm install --save-dev webpack
在此错误消息之前,我已完成此操作。但是,我确实尝试删除了node_modules,重新安装了webpack,重新初始化了npm repo-仍然没有任何效果。
如果当前行为是错误,请提供重现步骤。 相关代码(我为每个客户端和服务器都有一个webpack配置)
webpack.server.js ...
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const WebpackShellPlugin = require('webpack-shell-plugin');
const pathResolver = require('../utils/pathResolver');
const versioning = require('../utils/versioning');
require('dotenv').config();
console.clear(); // eslint-disable-line no-console
// Get git hash & app version
const version = versioning.generate();
const isProd = process.env.NODE_ENV === 'production';
// Webpack plugins that are always in use, regardless of the mode
const plugins = [
// Clean last build
new CleanWebpackPlugin({
verbose: true,
cleanOnceBeforeBuildPatterns: pathResolver.serverOutputDir,
}),
// Run time environment variables
new webpack.DefinePlugin({
'process.env.VERSION': JSON.stringify(version.version),
'process.env.GIT_HASH': JSON.stringify(version.gitHash),
}),
];
// Plugins in development mode
if (!isProd) {
plugins.push(
// Hot loading
new WebpackShellPlugin({
onBuildStart: ['echo "Starting webpack"'],
onBuildEnd: ['npm run server:watch'],
}),
);
}
// Plugins in production mode
if (isProd) {
plugins.push(
new webpack.DefinePlugin({ 'process.env.CLIENT_BUILD_DIR': JSON.stringify(pathResolver.clientOutputDir) }),
);
}
module.exports = {
target: 'node',
mode: process.env.NODE_ENV,
watch: !isProd,
externals: [nodeExternals()],
devtool: 'inline-source-map',
entry: pathResolver.serverEntryPoint,
plugins,
module: {
rules: [
{
test: /\.(ts|js)$/,
use: [{
loader: 'ts-loader',
options: { configFile: 'tsconfig.json' },
}],
exclude: /node_module/,
},
],
},
resolve: {
// JavaScript & TypeScript are resolved through ts-loader
extensions: ['.ts', '.js'],
},
output: {
filename: 'server.ts',
path: pathResolver.serverOutputDir,
},
};
package.json ...
"server": "webpack --config ../config/webpack/webpack.server.js",
"server:watch": "nodemon --exec babel-node ../build/api/server.js",
"build:server": "NODE_ENV=production webpack --mode production --config ../config/webpack/webpack.server.js",
复制步骤。两者都导致相同的错误...
$ npm run server
$ npm run build:server
预期的行为是什么? Webpack应该使用指定的加载程序捆绑我的文件。
其他相关信息: webpack版本:^ 4.44.2 Node.js版本:12.16.1 NPM版本:6.14.5 操作系统:macOS Catalina 10.15.5