I stuck at MODULE_NOT_FOUND , I have checked other questions related to it and tried their soultions but could not get succeed. Any help will be appreciated. Thanks.
I am trying to create node.js with mysql db using TypeORM and trying to import User entity created in entity folder where I have file name User.ts
This is my folder structure.
Below is package.json
{
"name": "mysql-typeorm",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/express": "^4.17.17",
"@types/node": "^18.14.6",
"express": "^4.18.2",
"mysql2": "^3.2.0",
"nodemon": "^2.0.21",
"reflect-metadata": "^0.1.13",
"ts-node": "^10.9.1",
"typeorm": "^0.3.12",
"typescript": "^4.9.5"
}
}
Below is my app.js
const express = require('express')
const { User } = require('./entity/User')
const { DataSource , createConnection} = require('typeorm')
const app = express()
const port = 3000 || process.env.port
app.listen(port , () => {
console.log(`server is listening on ${port}`)
})
const appDataSource = new DataSource({
type:"mysql",
host:"localhost",
port:3308,
username:"root",
password:"",
database:"booksDB",
synchronize:true,
logging:true,
entities:[User]
});
appDataSource.initialize().then((res)=> {
console.log(`db is connected`)
}).catch((err) => {
console.log(`${err}`)
})
app.get('/test',(req,res)=>{
res.send({
test:'Testing is done'
})
})
Below is my User.ts for entity User located in entity folder.
import {Entity , PrimaryGeneratedColumn , Column , BaseEntity } from 'typeorm'
@Entity()
export class User extends BaseEntity {
@PrimaryGeneratedColumn()
id!: number;
@Column()
name!: string;
@Column()
email!: string;
@Column()
phone!: string;
}
I am getting error like this
Error: Cannot find module './entity/User'
Require stack:
- D:\2023\elluminous\technical_growth_program\node.js\node-mysql-typeorm\app.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (D:\2023\elluminous\technical_growth_program\node.js\node-mysql-typeorm\app.js:2:18)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'D:\\2023\\elluminous\\technical_growth_program\\node.js\\node-mysql-typeorm\\app.js'
]
}
[nodemon] app crashed - waiting for file changes before starting...
Also I have tried with
import { User } from "./entity/User"
in app.js but I am getting error like this
(node:15732) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
D:\2023\elluminous\technical_growth_program\node.js\node-mysql-typeorm\app.js:3
import { User } from "./entity/User"
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
You cannot mix JS and TS. In your scripts you are using nodemon app.js
which means that you are starting a regular NodeJS process and it will be restarted should app.js
or any imported dependencies change.
TS cannot be executed by NodeJS and should be first transformed into JS using tsc
compiler. For dev mode you would want to have it served and for that you would use ts-node
which I can see in your deps but is unused in the scripts.
To tell nodemon
that you want to use TS you should change app.js
to app.ts
so it's using TS instead of JS. This should fix the issue.