after compiling my ts files when I ran my script.js file in the VS Code terminal, I received this error:
node:internal/modules/cjs/loader:1228
throw err;
^
Error: Cannot find module '/Users/<username>/projects/course tree/course_tree/scripts/script.js'
at Function._resolveFilename (node:internal/modules/cjs/loader:1225:15)
at Function._load (node:internal/modules/cjs/loader:1055:27)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
at node:internal/main/run_main_module:36:49 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
Node.js v22.14.0
I honestly don't know what is causing this error. I tried searching on Stack but I couldn't find a solution to my problem.
I'm new to using TypeScript and I'm not too familiar with modules and modifying tsconfig.json.
My project folders is organized like this:
My tsconfig.json is this:
{
"compilerOptions": {
"target": "es2016",
"module": "Node16",
"moduleResolution": "nodenext",
"outDir": "./ts-out",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"type": "module",
"include": ["./ts-in"],
"exclude": [],
}
My script.ts file is this:
import * as d3 from "d3";
import Node from "./node.js";
import Course from "./course.js";
import Graph from "./graph.js"
let c1 : Course = new Course("CPSC 110", 4);
let c2 : Course = new Course("CPSC 210", 4);
// c2.addPrequisite(c1);
console.log("Hellow");
console.log(c1.courseId);
c1.print_position();
c2.print_position();
// console.log(c2.prerequisites);
let graph : Graph = new Graph();
graph.addNode(c1);
console.log(c1.position);
console.log(graph.adj_matrix);
graph.addNode(c2);
console.log(c2.position);
console.log(graph.adj_matrix);
let c3 : Node = new Course("CPSC 121", 4);
graph.addNode(c3);
console.log(c3.position);
console.log(graph.adj_matrix);
let c4 : Node = new Course("CPSC 107", 3);
graph.addNode(c4);
console.log(c4.position);
console.log(graph.adj_matrix);
let c5 : Node = new Course("CPSC 103", 3);
graph.addNode(c5);
console.log(c5.position);
graph.addPrequisite(c5, c4);
console.log(graph.adj_matrix);
console.log(graph.course_nodes);
My script.js file is this:
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const course_js_1 = __importDefault(require("./course.js"));
const graph_js_1 = __importDefault(require("./graph.js"));
let c1 = new course_js_1.default("CPSC 110", 4);
let c2 = new course_js_1.default("CPSC 210", 4);
// c2.addPrequisite(c1);
console.log("Hellow");
console.log(c1.courseId);
c1.print_position();
c2.print_position();
// console.log(c2.prerequisites);
let graph = new graph_js_1.default();
graph.addNode(c1);
console.log(c1.position);
console.log(graph.adj_matrix);
graph.addNode(c2);
console.log(c2.position);
console.log(graph.adj_matrix);
let c3 = new course_js_1.default("CPSC 121", 4);
graph.addNode(c3);
console.log(c3.position);
console.log(graph.adj_matrix);
let c4 = new course_js_1.default("CPSC 107", 3);
graph.addNode(c4);
console.log(c4.position);
console.log(graph.adj_matrix);
let c5 = new course_js_1.default("CPSC 103", 3);
graph.addNode(c5);
console.log(c5.position);
graph.addPrequisite(c5, c4);
console.log(graph.adj_matrix);
console.log(graph.course_nodes);
At the beginning I tried creating a d.ts file for each of my .ts files since I was receiving syntax errors in my import statements. Weirdly, the errors only went away when I replaced the relative path statements ../ts-out/<name-of-js-file>.js
with ./<name-of-js-file>.js
. However, I was able to compile the .ts files without any issues.
The error appears after I input node script.js
. I've tried rm -rf node_modules
and then npm install
but it did not work. All modules are local and created by me so I've been using relative paths in all my import statements.
Theses errors didn't occur at all when I initially placed all my .js and .ts files in a single folder with my tsconfig.json (My problems only started once I started to organize my files).
I'd be grateful if someone could point me in the right direction on how to fix this.
It said Error: Cannot find module '/Users/<username>/projects/course tree/course_tree/scripts/script.js'
But there is no script.js
in script
folder. It should be script/ts-out/script.js
. So I guess you were runing node script.js
in script
folder.
If that so, you need to change the command to node scripts/ts-out/script.js
BTW, if you are running typescript in your local you can use node-ts
instead of node
. It can directly run .ts file without compile them first