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

时间:2018-06-25 16:58:23

标签: typescript npm webpack tsx

我试图了解如何在React中使用打字稿。我正在浏览本教程:this

当我通过下面在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”的内容-为什么它看起来在那里?我感觉好像缺少了一些基本的东西。

最后是package.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"
  }
}

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

1 个答案:

答案 0 :(得分:1)

尽管它看起来像您给它提供的配置一样,但是文件需要在底部导出以便Webpack能够看到它。

module.exports = config

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