javascript - L'argomento path di Webpack non è una stringa
Mi sono scervellato cercando di capire perché Webpack dica che per il modulo di ingresso "app", il percorso non è una stringa. Non è esattamente lo stesso degli altri errori che ho letto su Stackoverflow, ed è per questo che mi spinge a fare la domanda da solo.
Sto usando l'ultima versione di Webpack (5.16) e Webpack-cli (4.4) e sto cercando di costruire questo per farlo funzionare su Node.
Ho il seguente webpack.config.js, in cui ho aggiunto solo la sezione stats per cercare di risolvere il problema:
'use strict'
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
context: __dirname,
stats: {
logging: true,
loggingDebug: [/webpack/],
loggingTrace: true,
errors: true,
errorStack: true,
errorDetails: true,
},
entry: {
app: './js/app.js'
},
optimization: {
minimize: true
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default'],
}),
new MiniCssExtractPlugin(),
],
output: {
publicPath: '/',
path: __dirname + '/dist',
filename: '[name].bundle.js',
},
module: {
rules: [
{
test: /\.m?js$/, exclude: /(node_modules)/,
use: ['babel-loader', { options: { presets: ['@babel/preset-env'] } }]
},
{
test: /\.(scss)$/,
use: [{
loader: 'style-loader',
}, {
loader: 'css-loader',
}, {
loader: 'postcss-loader',
options: {
plugins: function () {
return [
require('precss'),
require('autoprefixer')
]
}
}
}, {
loader: 'sass-loader'
}]
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader']
},
{
test: /\.(woff|woff2)$/,
use: ['url-loader', { options: { limit: 5000 } } ]
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
use: ['url-loader', { options: { limit: 10000, mimetype: 'application/octet-stream' } } ]
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
use: ['url-loader', { options: { limit: 10000, mimetype: 'image/svg+xml' } } ]
},
{
test: require.resolve('jquery'),
use: [{
loader: 'expose-loader',
options: {
exposes: ['$','jQuery']
}
}]
}
],
},
}
Il seguente codice nel mio file app.js:
import 'bootstrap'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'counterup/jquery.counterup.js'
import '../template/js/custom.js'
import '../template/css/style.css'
Ogni volta che eseguo webpack su questo codice, ricevo sempre l'errore:
ERROR in app
Module not found: Error: path argument is not a string
ModuleNotFoundError: Module not found: Error: path argument is not a string
at factory.create (/app/node_modules/webpack/lib/Compilation.js:1656:28)
at hooks.factorize.callAsync (/app/node_modules/webpack/lib/NormalModuleFactory.js:708:13)
at eval (eval at create (/app/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:10:1)
at hooks.resolve.callAsync (/app/node_modules/webpack/lib/NormalModuleFactory.js:272:22)
at eval (eval at create (/app/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)
at err (/app/node_modules/webpack/lib/NormalModuleFactory.js:494:15)
at err (/app/node_modules/webpack/lib/NormalModuleFactory.js:118:11)
at resolveRequestArray (/app/node_modules/webpack/lib/NormalModuleFactory.js:557:8)
at /app/node_modules/neo-async/async.js:2830:7
at done (/app/node_modules/neo-async/async.js:2925:13)
A quale "percorso" si riferisce anche qui? Ho provato di tutto, dall'aggiungere contesto, al cambiare l'entrata, al spostare il file app.js, e rimuovendo tutto tranne bootstrap da app.js. Sembra che non importa cosa faccia, non riesco davvero a compilarlo. Ho dato un'occhiata al codice di Webpack, ma lo stack trace non fornisce suggerimenti utili.
Qualsiasi aiuto sarebbe molto apprezzato.
Risposta n. 1
Per semplificare il codice, abbiamo rimosso alcune proprietà, e avevi ragione, non è necessario importare path.
Node: 14.15.1 Webpack: 5.16 Webpack-cli: 4.4
module.exports = {
entry: {
app: './js/app.js',
},
output: {
path: __dirname + '/dist',
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules)/,
use: {
// you forgot to set the `loader` property
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
},
],
},
};
