Webpack 无法解析 Windows 上的 core-js 路径
Webpack cannot resolve core-js paths on Windows
运行 webpack 我收到错误:
ERROR in ./node_modules/core-js/index.js
Module not found: Error: Can't resolve './es' in 'pathtoproject\node_modules\core-js'
@ ./node_modules/core-js/index.js 1:0-15
@ multi core-js whatwg-fetch ./src/contactForm.tsx
ERROR in ./node_modules/core-js/index.js
Module not found: Error: Can't resolve './internals/path' in 'pathtoproject\node_modules\core-js'
@ ./node_modules/core-js/index.js 4:11-38
@ multi core-js whatwg-fetch ./src/contactForm.tsx
ERROR in ./node_modules/core-js/index.js
Module not found: Error: Can't resolve './proposals' in 'pathtoproject\node_modules\core-js'
@ ./node_modules/core-js/index.js 2:0-22
@ multi core-js whatwg-fetch ./src/contactForm.tsx
ERROR in ./node_modules/core-js/index.js
Module not found: Error: Can't resolve './web' in 'pathtoproject\node_modules\core-js'
@ ./node_modules/core-js/index.js 3:0-16
@ multi core-js whatwg-fetch ./src/contactForm.tsx
虽然文件夹确实存在。我相信这与 Windows 路径有关,因为当我将 core-js/index.js 文件更改为使用 path.resolve() 而不是相对导入时,它确实有效。这不是我想要的解决方案,因为它需要更改 core-js 模块。
参考这里是我的webpack.config.js
const path = require('path');
module.exports = {
entry: ["core-js", "whatwg-fetch", "./src/contactForm.tsx"],
output: {
path: path.join(__dirname, "build/assets/js/"),
filename: "contactForm.js"
},
mode: "production",
// Enable sourcemaps for debugging webpack's output.
devtool: "source-map",
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx"]
},
module: {
rules: [
{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
{
loader: "ts-loader"
}
]
},
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{
enforce: "pre",
test: /\.js$/,
loader: "source-map-loader"
}
]
},
// When importing a module whose path matches one of the following, just
// assume a corresponding global variable exists and use that instead.
// This is important because it allows us to avoid bundling all of our
// dependencies, which allows browsers to cache those libraries between builds.
externals: {
"react": "React",
"react-dom": "ReactDOM"
}
};
任何修复的想法将不胜感激
你可以尝试更换
output: {
path: path.join(__dirname, "build/assets/js/"),
filename: "contactForm.js"
},
和
output: {
path: path.resolve(__dirname, "build/assets/js/"),
filename: "contactForm.js"
},
路径连接只是连接路径,解析会将路径视为绝对路径。
您还可以添加上下文 属性:
module.exports = {
context: path.resolve(__dirname),
entry: ["core-js", "whatwg-fetch", "./src/contactForm.tsx"],
这是因为我的 webpack 配置中没有 javascript 文件作为可解析的扩展。
...
resolve: {
//modules: ['node_modules'],
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx"]
},
...
需要
...
resolve: {
//modules: ['node_modules'],
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx", ".js"]
},
...
运行 webpack 我收到错误:
ERROR in ./node_modules/core-js/index.js
Module not found: Error: Can't resolve './es' in 'pathtoproject\node_modules\core-js'
@ ./node_modules/core-js/index.js 1:0-15
@ multi core-js whatwg-fetch ./src/contactForm.tsx
ERROR in ./node_modules/core-js/index.js
Module not found: Error: Can't resolve './internals/path' in 'pathtoproject\node_modules\core-js'
@ ./node_modules/core-js/index.js 4:11-38
@ multi core-js whatwg-fetch ./src/contactForm.tsx
ERROR in ./node_modules/core-js/index.js
Module not found: Error: Can't resolve './proposals' in 'pathtoproject\node_modules\core-js'
@ ./node_modules/core-js/index.js 2:0-22
@ multi core-js whatwg-fetch ./src/contactForm.tsx
ERROR in ./node_modules/core-js/index.js
Module not found: Error: Can't resolve './web' in 'pathtoproject\node_modules\core-js'
@ ./node_modules/core-js/index.js 3:0-16
@ multi core-js whatwg-fetch ./src/contactForm.tsx
虽然文件夹确实存在。我相信这与 Windows 路径有关,因为当我将 core-js/index.js 文件更改为使用 path.resolve() 而不是相对导入时,它确实有效。这不是我想要的解决方案,因为它需要更改 core-js 模块。
参考这里是我的webpack.config.js
const path = require('path');
module.exports = {
entry: ["core-js", "whatwg-fetch", "./src/contactForm.tsx"],
output: {
path: path.join(__dirname, "build/assets/js/"),
filename: "contactForm.js"
},
mode: "production",
// Enable sourcemaps for debugging webpack's output.
devtool: "source-map",
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx"]
},
module: {
rules: [
{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
{
loader: "ts-loader"
}
]
},
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{
enforce: "pre",
test: /\.js$/,
loader: "source-map-loader"
}
]
},
// When importing a module whose path matches one of the following, just
// assume a corresponding global variable exists and use that instead.
// This is important because it allows us to avoid bundling all of our
// dependencies, which allows browsers to cache those libraries between builds.
externals: {
"react": "React",
"react-dom": "ReactDOM"
}
};
任何修复的想法将不胜感激
你可以尝试更换
output: {
path: path.join(__dirname, "build/assets/js/"),
filename: "contactForm.js"
},
和
output: {
path: path.resolve(__dirname, "build/assets/js/"),
filename: "contactForm.js"
},
路径连接只是连接路径,解析会将路径视为绝对路径。
您还可以添加上下文 属性:
module.exports = {
context: path.resolve(__dirname),
entry: ["core-js", "whatwg-fetch", "./src/contactForm.tsx"],
这是因为我的 webpack 配置中没有 javascript 文件作为可解析的扩展。
...
resolve: {
//modules: ['node_modules'],
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx"]
},
...
需要
...
resolve: {
//modules: ['node_modules'],
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx", ".js"]
},
...