webpack 找不到 src,但它不应该寻找它



我试图了解如何在 React 中使用打字稿。我正在浏览本教程:https://blog.logrocket.com/how-why-a-guide-to-using-typescript-with-react-fffb76c61614

当我通过下面在我的package.json中定义的 npm 脚本"pile"运行 webpack 命令时,我得到:ERROR in Entry module not found: Error: Can't resolve './src' in '/my/home/directory/projects/tsx-tutorial'

这是我的项目文件夹的目录结构:

tsx-tutorial
├── app.tsx {<---- I want this to be my entry point right?}
├── dist
├── index.html
├── index.ts
├── lookTree.txt
├── node_modules.zip
├── package-lock.json
├── package.json
├── src {<----- there it is??}
│   └── hello.ts
├── tsconfig.json
└── webpack.config.js
2 directories, 10 files

这是我的webpack.config.js

var path = require("path");
var config = {
entry: "app.tsx", // I'm not doing anything with ./src at all here??
output: {
path: path.resolve(__dirname, "build"),
filename: "bundle.js"
},
resolve: {
extensions: [".ts", ".tsx", ".js"]
},
module: {
loaders: [
{
test: /.tsx?$/,
loader: "ts-loader",
exclude: /node_modules/
}
]
}
};

我不要求任何带有"src"的东西,句号——为什么它在那里看?我觉得我错过了一些非常基本的东西。

最后是包.json:

{
"name": "tsx-tutorial",
"version": "0.0.1",
"description": "to help me learn tsx-fu",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"pile": "webpack"
},
"author": "",
"license": "ISC",
"devDependencies": {
"ts-loader": "^4.4.1",
"webpack": "^4.12.1",
"webpack-cli": "^3.0.8"
},
"dependencies": {
"@types/react": "^16.4.1",
"@types/react-dom": "^16.0.6",
"react": "^16.4.1",
"react-dom": "^16.4.1"
}
}

非常感谢您的帮助!感谢您帮助新手学习。

虽然看起来你已经给了它一个配置,但文件需要在底部导出它,以便 webpack 看到它。

module.exports = config

解释一下这个错误:从版本 4 开始,如果未指定配置,webpack 默认使用./src作为入口点。 由于模块解析的工作方式,./src./src/index.js相同,但是文件名和扩展名是可配置的。

相关内容

最新更新