It sounds like a common error. But in my case i can not find the reason. I am creating an NPM package as an backend-API for another project. I am using typescript and node.
I want to change my import statements from import XX from "../../../XXX"
into import XX from "@XX"
, am using PHP storm as code editor and it says that my code is good.
But when i run the script with npx npx ts-node src/index
it will returns this:
Error: Cannot find module '@authentication/SignUp'
Require stack:
- /Users/Youssef_1/Projects/driver-seat-amplify-api/src/index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/Users/Youssef_1/Projects/driver-seat-amplify-api/node_modules/@cspotcode/source-map-support/source-map-support.js:679:30)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/Youssef_1/Projects/driver-seat-amplify-api/src/index.js:6:18)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/Youssef_1/Projects/driver-seat-amplify-api/src/index.js' ]
}
index.ts:
import {SignUp, ConfirmSignUp, ResendSignUp} from "@authentication/SignUp";
import SignIn from "./Authentication/SignIn";
import Validator from "./Services/Validator";
import {RegexPatterns, RegexTypes} from "./Enums/Regex";
import SignOut from "./Authentication/SignOut";
import DeleteAccount from "./Authentication/DeleteAccount";
import UserManagement from "./Authentication/UserManagement";
import "./app.config";
export default {
SignIn,
SignUp,
SignOut,
ConfirmSignUp,
ResendSignUp,
Validator,
UserManagement,
RegexPatterns,
RegexTypes,
DeleteUser: DeleteAccount,
}
tsconfig.json:
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"alwaysStrict": true,
"baseUrl": "./src/",
"sourceMap": true,
"paths": {
"@authentication/*": ["Authentication/*"]
}
}
}
babel.config.js
module.exports = {
plugins: [
"@babel/plugin-transform-typescript",
"@babel/preset-env",
["module-resolver", {
"root": ["./src"],
"alias": {
"authentication": "./src/Authentication",
}
}]
],
presets: [
"@babel/plugin-transform-typescript",
"@babel/preset-env"
],
};
Any ideas?
CodePudding user response:
This saved my day: TS config path error. Error: Cannot find module '@/models/UserSchema' (accepted answer).
Installing ts-node fixed this issue for me.