I have two Typescript react modules: moduleA
and moduleB
.
I am trying to use a component Button.tsx
from moduleA
by exporting the Button.tsx
in moduleB
referring to this component using moduleA
.
Here's the steps I am following :
moduleA
.moduleA
.moduleB
.moduleA
in moduleB
using npm install ../moduleA
.Then, I am referring the Button
component in moduleB
, from moduleA
using:
import { Button } from "moduleA";
I am getting this error :
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'
Here's my package.json
of 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"
}
}
and package.json of 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"
}
}
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"
]
}
folder structure:
├── dist
├── node_modules
├── src
├── package.json
├── webpack.config.js
├── tsconfig.json
Is there any other way to do it?
How TypeScript resolves modules. TypeScript will mimic the Node. js run-time resolution strategy in order to locate definition files for modules at compile-time. To accomplish this, TypeScript overlays the TypeScript source file extensions ( .
To solve the error "Module not found: Error: Can't resolve 'ts-loader'", make sure to install the ts-loader package by opening your terminal in your project's root directory and running the command npm install -D ts-loader typescript and restart your development server.
When set to true, allowSyntheticDefaultImports allows you to write an import like: ts. import React from "react"; instead of: ts.
In TypeScript, files containing a top-level export or import are considered modules.
This is because the package that is being used as a dependency is broken.
moduleA
looks for the button in the wrong place. The package.json
has the main
property set to "main.js"
. When the package is used, it tries to find the main.js
file in the root directory. But according to your folder structure it does not exist.
As a verification step, go to moduleB
's node_modules
folder and open moduleA
, is there a main.js
inside the folder?
It shouldn't exist because tsconfig
has outDir
set to "./dist"
. The solution would be to change moduleA
's main
to "./dist/main.js"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With