3

我正在尝试了解如何将 typescript 与 React 一起使用。我正在浏览本教程:https ://blog.logrocket.com/how-why-a-guide-to-using-typescript-with-react-fffb76c61614

当我通过下面定义的 npm 脚本“pile”运行 webpack 命令时package.json,我得到: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"
  }
}

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

4

1 回答 1

2

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

module.exports = config

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

于 2018-06-25T20:11:31.963 回答