【问题标题】:Cannot find module when compiling under Visual Studio在 Visual Studio 下编译时找不到模块
【发布时间】:2017-04-01 21:07:27
【问题描述】:

我有一个非常简单的 TypeScript 应用程序,它使用 webpack 编译得很好,但是在尝试在 Visual Studio 中构建时给我一个错误“Build:Cannot find module 'react'”。我被卡住了,感谢任何帮助。

package.json:

{
  "name": "react-router-test",
  "version": "1.0.0",
  "description": "",
  "main": "./dist/bundle.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^15.4.0",
    "react-dom": "^15.4.0",
    "react-router": "^3.0.0"
  },
  "devDependencies": {
    "@types/react": "^0.14.49",
    "@types/react-dom": "^0.14.18",
    "@types/react-router": "^2.0.39",
    "babel-cli": "^6.18.0",
    "babel-core": "^6.18.2",
    "babel-loader": "^6.2.7",
    "babel-preset-es2015": "^6.18.0",
    "babel-preset-react": "^6.16.0",
    "ts-loader": "^1.2.1",
    "typescript": "^2.0.10",
    "webpack": "^1.13.3"
  }
}

webpack.config.js:

    module.exports = {
    entry: "./src/Index.tsx",
    output: {
        filename: "./dist/bundle.js"
    },

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: ["", ".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
    },

    module: {
        //loaders: [
        //    { test: /\.tsx$/, exclude: /node_modules/, loader: 'babel-loader?presets[]=es2015&presets[]=react!ts-loader' }

        loaders: [
            {
                test: /\.ts(x?)$/,
                loader: "babel-loader!ts-loader"
            },
        ]

    },

    devServer: {
        contentBase: ".",
        host: "localhost",
        port: 8081
    },

    externals: {
        "react": "React",
        "react-dom": "ReactDOM"
    },

}

tsconfig.json:

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es6",
    "jsx": "preserve",
    "module": "commonjs"
  }
}

你好.tsx:

import * as React from "react";

interface IHelloProps { compiler: number }

export class Hello extends React.Component<IHelloProps, {}> {
    render() {
        return <div > Hell no 2!</div >
    }
}

【问题讨论】:

  • 一切看起来都很好,这让我很奇怪。你运行 npm install 了吗?
  • 如果我运行“npm link typescript”,然后运行“typings install --global --save dt~react”,它会有所帮助,但之后我似乎总是与 node_modules 中的 @type 文件夹发生冲突从 webback 编译时,其他包出现问题,准确地说是 react-router。
  • 试试这个。删除你的节点模块文件夹然后转到你的应用程序的根文件夹并运行npm install....然后给我反馈
  • 抱歉,最近没能做到这一点。我开始了全新的应用程序,只是为了有一个非常干净的开始。到了同一点。我现在意识到我不想在新版本的打字稿中使用打字,但我在我所在的地方,从 web pack 编译,在 Visual Studio 中找不到模块

标签: javascript visual-studio reactjs typescript visual-studio-2015


【解决方案1】:

我通过从这里安装最新版本 (2.0.6.0) 解决了所有问题:https://www.microsoft.com/en-us/download/details.aspx?id=48593

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 2023-03-24
    • 2020-08-22
    相关资源
    最近更新 更多