在 Visual Studio 下编译时找不到模块 [英] Cannot find module when compiling under Visual Studio
本文介绍了在 Visual Studio 下编译时找不到模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个非常简单的 TypeScript 应用程序,它使用 webpack 编译得很好,但是当我尝试在 Visual Studio 中构建时给了我一个错误Build:Cannot find module 'react'".我被卡住了,任何帮助表示赞赏.
I have very simple TypeScript app, which compiles fine using webpack, but gives me an error "Build:Cannot find module 'react'" when trying to build in Visual Studio. I'm stuck, any help is appreciated.
package.json:
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:
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:
tsconfig.json:
{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es6",
"jsx": "preserve",
"module": "commonjs"
}
}
Hello.tsx:
import * as React from "react";
interface IHelloProps { compiler: number }
export class Hello extends React.Component<IHelloProps, {}> {
render() {
return <div > Hell no 2!</div >
}
}
推荐答案
我通过从这里安装最新版本 (2.0.6.0) 解决了所有问题:https://www.microsoft.com/en-us/download/details.aspx?id=48593
I got it all resolved by installing latest version (2.0.6.0) from here: https://www.microsoft.com/en-us/download/details.aspx?id=48593
这篇关于在 Visual Studio 下编译时找不到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文