无法解析Typescript模块

时间:2018-05-31 06:01:52

标签: reactjs typescript webpack

我有两个Typescript反应模块:moduleAmoduleB

我正在尝试使用Button.tsx中的moduleA组件Button.tsx,使用moduleB导出moduleA引用此组件的moduleA

以下是我要遵循的步骤:

  1. moduleA
  2. 中安装和配置webpack和ts-loader
  3. 使用moduleB
  4. 的webpack创建构建
  5. moduleA
  6. 中配置webpack和ts-loader
  7. 使用moduleBnpm install ../moduleA安装Button
  8. 然后,我引用moduleB中的moduleA组件,import { Button } from "moduleA"; 使用:{/ p>

    ERROR in C:\Users\VaibhavPC\Projects\moduleb\src\Start.tsx
    ./src/Start.tsx
    [tsl] ERROR in C:\Users\VaibhavPC\Projects\moduleb\src\Start.tsx(2,24)
          TS2307: Cannot find module './moduleA'
    

    我收到此错误:

    package.json

    这是我的moduleA { "name": "moduleA", "version": "1.0.0", "description": "", "main": "main.js", "scripts": { "start": "react-scripts start", "build": "rm ./lib/* && tsc", "magic": "webpack --config webpack.config.js", "prepublish": "npm run build", "test": "./test.sh" }, "author": "", "license": "ISC", "devDependencies": { "@types/chai": "^3.4.35", "@types/enzyme": "^2.7.6", "@types/mocha": "^2.2.40", "@types/react": "^15.0.18", "@types/react-dom": "^0.14.23", "@types/sinon": "^1.16.36", "chai": "^3.5.0", "enzyme": "^2.8.0", "jsdom": "^9.12.0", "mocha": "^3.2.0", "react": "^15.4.2", "react-addons-test-utils": "^15.4.2", "react-dom": "^15.4.2", "sinon": "^2.1.0", "webpack-cli": "^2.1.4", "ts-loader": "^4.3.0", "webpack": "^4.10.1" }, "files": [ "lib", "LICENSE", "main.js" ], "types": "./lib/Button.d.ts", "dependencies": { "typescript": "^2.8.3" } }

    moduleB

    { "name": "tmp", "version": "1.0.0", "description": "", "main": "main.js", "scripts": { "start": "react-scripts start", "build": "rm ./lib/* && tsc", "magic": "webpack --config webpack.config.js", "prepublish": "npm run build", "test": "./test.sh" }, "author": "", "license": "ISC", "devDependencies": { "@types/chai": "^3.4.35", "@types/enzyme": "^2.7.6", "@types/mocha": "^2.2.40", "@types/react": "^15.0.18", "@types/react-dom": "^0.14.23", "@types/sinon": "^1.16.36", "chai": "^3.5.0", "enzyme": "^2.8.0", "jsdom": "^9.12.0", "mocha": "^3.2.0", "react": "^15.4.2", "react-addons-test-utils": "^15.4.2", "react-dom": "^15.4.2", "sinon": "^2.1.0", "webpack-cli": "^2.1.4", "ts-loader": "^4.3.0", "webpack": "^4.10.1", "babel-preset-react": "^6.16.0", "babel-preset-stage-3": "^6.17.0", "css-loader": "^0.27.3", "json-loader": "^0.5.4", "sass-loader": "^6.0.2", "style-loader": "^0.13.1", "url-loader": "^0.5.7", "extract-text-webpack-plugin": "^2.0.0-beta.4" }, "files": [ "lib", "LICENSE", "main.js" ], "types": "./lib/hello.d.ts", "dependencies": { "modulea": "file:../modulea", "react-scripts": "1.1.4", "typescript": "^2.8.3" } } 的package.json。

    tsconfig.json

    { "compilerOptions": { "outDir": "./dist", "target": "es5", "module": "commonjs", "noImplicitAny": true, "removeComments": true, "declaration": true, "sourceMap": true, "jsx": "react" }, "include": [ "./src/**/*.tsx", "./src/**/*.ts" ], "exclude": [ "node_modules" ] } ├── dist ├── node_modules ├── src ├── package.json ├── webpack.config.js ├── tsconfig.json

    library(qgraph)
    col=c("#5150a7","#c3200f","#ed9718","#017acb","#FFC0CB","#9c9bd5")
    e <- as.data.frame(get.edgelist(g))
    lay <- qgraph.layout.fruchtermanreingold(e,vcount=vcount(g))
    plot(g,layout=lay,vertex.color=col[V(g)$community],vertex.label =NA, vertex.size= 2) 
    

    还有其他办法吗?

1 个答案:

答案 0 :(得分:0)

这是因为依赖程序包已损坏。

导出按钮的

moduleA在错误的位置。 package的{​​{1}}属性设置为main,因此,在使用包时,它将在项目的根目录中寻找main.js。但是根据您问题中的文件夹树,它不存在。

main.js

作为验证步骤,转到moduleB的├── dist ├── node_modules ├── src ├── package.json ├── webpack.config.js ├── tsconfig.json 文件夹,然后查看node_modules,基本文件夹内是否有moduleA

可能不是因为main.js的{​​{1}}设置为tsconfig,所以解决方案是将outDir的{​​{1}}更改为{{ 1}}