Cannot Find Module Error When Running ts-node with Relative Path

Cannot Find Module Error When Running ts-node with Relative Path
typescript
Ethan Jackson

I have tried several different things to try to fix this issue but no matter what I do when running npx ts-node updateCostCenters.ts I continuely get this error

Error: Cannot find module '@/../prisma/seedFiles/oneTimeScripts/update-resources/utils/utils' Require stack: - /Users/braidenparkinson/Development/backend/prisma/seedFiles/oneTimeScripts/update-cost-center/updateCostCenter.ts at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1143:15) at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/Users/braidenparkinson/Development/backend/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30) at Function.Module._load (node:internal/modules/cjs/loader:984:27) at Module.require (node:internal/modules/cjs/loader:1231:19) at require (node:internal/modules/helpers:179:18) at Object.<anonymous> (/Users/braidenparkinson/Development/backend/prisma/seedFiles/oneTimeScripts/update-cost-center/updateCostCenter.ts:1:1) at Module._compile (node:internal/modules/cjs/loader:1369:14) at Module.m._compile (/Users/braidenparkinson/Development/backend/node_modules/ts-node/src/index.ts:1618:23) at Module._extensions..js (node:internal/modules/cjs/loader:1427:10) at Object.require.extensions.<computed> [as .ts] (/Users/braidenparkinson/Development/backend/node_modules/ts-node/src/index.ts:1621:12) { code: 'MODULE_NOT_FOUND', requireStack: [ '/Users/braidenparkinson/Development/backend/prisma/seedFiles/oneTimeScripts/update-cost-center/updateCostCenter.ts' ] }

Despite me able to find the utils file in that directory by clicking cmd + click So it looks like its there

for reference I have tried the following posts to get this to work and each to no avail Node JS Module Not Working . Module Not Found

node module not found .MODULE NOT FOUND

I've also tried deleting and reinstalling node_modules but still stuck any pointers in the right direction would be appreciated.

Heres my tsconfig

{ "compilerOptions": { "module": "commonjs", "declaration": true, "removeComments": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "allowSyntheticDefaultImports": true, "target": "es2017", "sourceMap": true, "outDir": "./dist", "baseUrl": "./src", "incremental": true, "skipLibCheck": true, "strictNullChecks": true, "noImplicitAny": false, "strictBindCallApply": false, "forceConsistentCasingInFileNames": false, "noFallthroughCasesInSwitch": false, "noErrorTruncation": true, "paths": { "@/*": ["*"] } } }

Heres my code

import { CostCenter, CostCenterCsv, csvToJson, formatObject, } from '@/../prisma/seedFiles/oneTimeScripts/update-resources/utils/utils'; import { wells } from './../../wells'; import fs from 'fs'; import { tank_batteries } from '../../tank_batteries_updated'; import Fuse from 'fuse.js'; const costCentersAsCsv = fs.readFileSync('./new_cost_centers.csv', { encoding: 'utf8', flag: 'r', }); const costCenters = csvToJson<CostCenterCsv>(costCentersAsCsv).sort((a, b) => { if (Number(a.Code) < Number(b.Code)) { return -1; } if (Number(a.Code) > Number(b.Code)) { return 1; } return a.Description?.localeCompare(b.Description); }); const cleaned_tank_battery = tank_batteries.map((tankBattery) => ({ ...tankBattery, tank_battery_desc: tankBattery.tank_battery_desc .toLowerCase() .replace(/[^\w\s]/gi, '') // Remove special characters .replace(/\s+/gi, '') // Remove spaces .replace(/lact/gi, '') // Remove 'lact' .replace(/^tb\s*/gi, '') // Remove 'tb' at the start .replace(/\s*tb\s*/gi, '') // Remove 'tb' in the middle .replace(/tb$/gi, ''), // Remove 'tb' at the end })); function fuzzySearch(description: string) { const fuse = new Fuse(cleaned_tank_battery, { keys: [{ name: 'tank_battery_desc' }], includeScore: true, threshold: 0.3, // lower value for stricter matching shouldSort: true, }); const sortedResult = fuse.search(description); if ((sortedResult?.[0]?.score ?? 1) < 0.3) { return cleaned_tank_battery[sortedResult[0].refIndex]; } return null; } const wellIdSet = new Set(wells.map((well) => well.well_id)); const formatedCostCenters = costCenters.map<CostCenter>((costCenter) => { const isWell = wellIdSet.has(Number(costCenter.Code)); const isTankBattery = costCenter.Type === 'TB'; const result = isTankBattery ? fuzzySearch( costCenter.Description.toLowerCase() .replace(/[^\w\s]/gi, '') // Remove special characters .replace(/\s+/gi, '') // Remove spaces .replace(/btry$/gi, ''), // Remove 'BTRY' at the end ) : null; return { cost_center_id: costCenter.Code, property_name: costCenter.Description, property_type_code: costCenter.Type, county_name: costCenter.County, property_id: isWell ? costCenter.Code : isTankBattery ? (result?.tank_battery_id ?? null) : null, property_type: isWell ? 'Well' : isTankBattery ? 'TankBattery' : null, }; }); const costCentersString = formatedCostCenters .map((obj) => formatObject(obj, ' ')) .join(',\n'); fs.writeFileSync( './costCenters.ts', `export const costCenters = [\n${costCentersString}\n];\n`, ); // convert json to csv const csv = formatedCostCenters .map((obj) => Object.values(obj).join(',')) .join('\n'); fs.writeFileSync('./newcostCenters.csv', csv);

Answer

Now your path is looking rather absolute than relative. I would try to import it following way:

import { CostCenter, CostCenterCsv, csvToJson, formatObject, } from '../update-resources/utils/utils';

as I can see in the callback, you are trying to run the file from the "update-cost-center" folder, which is located in the same folder, as "update-resources". So you just need to go one level higher, before accessing desired folder and module.

Related Articles