开发者社区> 问答> 正文

使用新创建的React + Typescript Webpack应用设置“ npm start”?

我需要在webpack的初始设置方面获得一些帮助,以通过热重载为我的应用程序提供服务。我已经按照本指南设置了带有Typescript的React + Webpack:https ://www.typescriptlang.org/docs/handbook/react-&-webpack.html

但是我想知道如何进行设置,以便我可以调用“ npm start”并通过热重载使其编译并托管应用程序。

我的webpack.config.js如下所示:

module.exports = {
    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"
    }
};

我的package.json看起来像这样:

{
  "name": "client",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "test"
  },
  "author": "DMW",
  "license": "ISC",
  "devDependencies": {
    "@types/react": "^16.9.15",
    "@types/react-dom": "^16.9.4",
    "source-map-loader": "^0.2.4",
    "ts-loader": "^6.2.1",
    "typescript": "^3.7.3",
    "webpack": "^4.41.2",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.9.0"
  },
  "dependencies": {
    "react": "^16.12.0",
    "react-dom": "^16.12.0"
  },
  "keywords": []
}

如果有人可以将我链接到某个东西以使其正常工作,我将非常感激。我对发现的搜索结果感到有些害怕。

展开
收起
几许相思几点泪 2019-12-08 22:11:51 1326 0
2 条回答
写回答
取消 提交回答
  • package.json里添加:

    "scripts": {
      "start": ""
    }
    
    2020-03-31 11:52:02
    赞同 展开评论 打赏
  • 你好, 看起来你的 package.json 里缺少 start 脚本,所以无法运行 npm start

    在 package.json 的 scripts 字段下添加 start 脚本,就可以使用 npm start 来执行这段脚本了

    2020-02-26 10:29:40
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
利用编译将 Vue 组件转成 React 组件 立即下载
React Native 全量化实践 立即下载
React在大型后台管理项目中的工程实践 立即下载

相关镜像