Everything runs smoothly with my imports during coding, but after building the project using tsc
, the imported files are not resolving to valid paths.
This is how my tsconfig.json
looks:
{
"compilerOptions": {
"target": "ES2018" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"lib": ["es6"] /* Specify library files to be included in the compilation. */,
"allowJs": true /* Allow javascript files to be compiled. */,
"outDir": "build" /* Redirect output structure to the directory. */,
"rootDir": "src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"moduleResolution": "node",
"resolveJsonModule": true /* Include modules imported with '.json' extension */,
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,
"allowSyntheticDefaultImports": true,
"baseUrl": "src",
"paths": {
"@root/*": ["../*"],
"@src/*": ["./*"]
}
}
}
The scripts
section in my package.json
is as follows:
"scripts": {
"dev": "nodemon",
"build": "rm -rf ../build && tsc",
"start": "yarn run build && node build/index.js",
"lint": "eslint . --ext .ts"
},
The use of nodemon
through a json file is working well, thanks to tsconfig-paths
:
{
"watch": ["src"],
"ext": ".ts,.js",
"ignore": [],
"exec": "ts-node -r tsconfig-paths/register ./src/index.ts"
}
Upon attempting to execute yarn start
, an error arises in my api/build/index.js
:
Error: Cannot find module '../src/app'
The code is searching for a non-existent file at ../src/app
. The correct path should be ./app
.
The layout of the build
folder is structured as follows:
build
routes
index.js
users.js
startup
routes.js
app.js
index.js
I've invested a considerable amount of time troubleshooting this issue without success. I'm uncertain about what's going wrong here.
How can I resolve this problem?