ts-node cannot find module
I have two files:
src/app/index.tssrc/config.ts
Within index.ts:
import { getConfig } from "config";
getConfig();
Within config.ts:
export const getConfig = () => console.log("hello world!");
I have a tsconfig.json file:
{
"compilerOptions": {
"outDir": "./ts-build/",
"module": "commonjs",
"moduleResolution": "node",
"baseUrl": "src",
"target": "es5"
}
}
Running ts-node ./src/app/index.ts throws me: Error: Cannot find module 'config'. Setting moduleResolution to classic doesn't change anything.
Versions:
"ts-node": "^3.3.0",
"typescript": "^2.5.2"
Any ideas if this is a ts-node issue, typescript issue or did I do something wrong?
This https://github.com/TypeStrong/ts-node/issues/1012
ts-node always works from pwd. you need to set basedir in tsconfig.json and work with tsconfig-paths.
ts-node always works from pwd. you need to set basedir in tsconfig.json and work with tsconfig-paths.
That is exactly what I did and I am still having that problem. Not sure if the problem lies on the fact that I'm using require to import a file that then uses import. Here is my tsconfig:
{
"compilerOptions": {
"outDir": "./dist",
"jsx": "react",
"allowJs": true,
"esModuleInterop": true,
"target": "es6",
"baseUrl": "./src",
"module": "esnext",
"moduleResolution": "Node",
"sourceMap": true,
// NOTE: this is only temporary until we actually can add all typescript types!
"skipLibCheck": true,
"paths": {
"react": ["../node_modules/react"],
"redux": ["redux/*"],
"testUtils": ["testUtils"],
"spring/*": ["../../spring/*"],
"spring": ["../../spring/src"]
},
"lib": ["es2020", "DOM"]
},
"include": ["./src/**/*", "./types"],
"exclude": ["node_modules"],
"ts-node": {
"compilerOptions": {
"paths": {
"ukStrings": ["./src/ukStrings.ts"]
},
"module": "CommonJS",
"target": "es2017"
}
}
}
As you can see, I have a path specified for ts-node. However, when I require a file require(fullPathToFile) that then imports ukStrings I get a can not compile error, telling me that it can't find the ukStrings file.
You could use the module-alias package as described in https://dev.to/larswaechter/path-aliases-with-typescript-in-nodejs-4353
Hi,
I'm having the same issue from the first post, long story short I'm converting a nodejs app to typescript, I run the app with:
ts-node-esm .\\src\\server.ts
This is my tsconfig.json
"compilerOptions": {
/* Docs: [https://www.typescriptlang.org/tsconfig](https://www.typescriptlang.org/tsconfig) */
"target": "ES2020",
"module": "ESNext",
"outDir": "bin",
"rootDir": "src",
"strict": true,
"noImplicitAny": false,
"strictNullChecks": true,
"checkJs": true,
"allowJs": true,
"moduleResolution": "node",
"types": ["node"],
"lib": ["es6"],
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"allowUnreachableCode": true,
"useUnknownInCatchVariables": false // TypeScript 4.4+ only!
},
"include": ["src/**/*.ts", "src/**/*.js"],
"exclude": ["node_modules", "<node_internals>/**"],
"esm": true
}
Any help would be great!
I met the problem: Error: Cannot find module '@app/lib/prisma_client', and I add the following code in the tsconfig.json:
{
"ts-node": {
// Do not forget to `npm i -D tsconfig-paths`
"require": ["tsconfig-paths/register"]
}
}
then re-run ts-node src/index.ts, everything is good now!
reference: paths and baseUrl
and the full tsconfig.json is here:
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"baseUrl": "./src",
"paths": {
"@app/*": ["*"]
},
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"],
"ts-node": {
"require": ["tsconfig-paths/register"]
}
}
thx๏ผthis question tried me tolong.
I had pretty much the same issue as the OP, but using esm. I didn't get anywhere with the solutions in the comments here but this blog post did help.
Specifically, adding "experimentalSpecifierResolution": "node" in my ts-config under ts-node resolved my issue.
Full ts-config:
{
"compilerOptions": {
"target": "ES2019",
"lib": [
"es2019",
"DOM"
],
"module": "ESNext",
"declaration": true,
"strict": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"isolatedModules": true,
"typeRoots": [
"./node_modules/@types"
],
"resolveJsonModule": true,
"esModuleInterop": true,
"moduleResolution": "node"
},
"ts-node": {
"esm": true,
"experimentalSpecifierResolution": "node",
"transpileOnly": true
}
}
For anyone struggling with ts-node or ts-node-dev modify your package.json with
ts-node-dev -r tsconfig-paths/register --poll src/app.ts
Thank you dwil618
It works in windows node v18
"ts-node": { "esm": true, "experimentalSpecifierResolution": "node", "transpileOnly": true }
i know this is an old issue, but i recently stumbled onto it myself. I believe i found a workaround:
Relevant configuration:
directory structure:
/
/dist/src/foo/...
/dist/src/foo.js
/dist/test/foo/integration/...
/dist/test/foo/unit/...
/src/foo/...
/src/foo.ts
/test/foo/integration/...
/test/foo/unit/...
THE MAIN OFFENDER: package.json:
{
"imports": {
"#src/*": "./dist/src/*",
"#test/*": "./dist/test/*"
},
}
tsconfig.json:
{
"include": [
"src/**/*",
"test/**/*"
],
"exclude": [
"node_modules/**/*",
"dist/**/*"
],
"compilerOptions": {
"rootDir": ".",
"outDir": "./dist",
"baseUrl": ".",
"paths": {
"#src/*": [ "src/*" ],
"#test/*": [ "test/*" ]
},
}
}
Problem synopsis:
tsconfig-paths/registercan't handle this particular case.ts-nodeusesimports: { ... }frompackage.jsonrather thenpathsfromtsconfig.json.- if
"imports": { "#src/*": "./dist/src/*" }thenNODE_ENV=devcode stops working. akats-node,nodemon,etcstops working, because/dist/dir by definition doesn't exist inNODE_ENV=devmode. - if
"imports": { "#src/*": "./src/*" }thenNODE_ENB=prodcode stops working. akanode .bc*.jsfiles reside in/dist/src/dir.nodesimply doesn't see/src/dist/**/*.jscode with path aliases directed to/src/dir. Not to mention/src/dir is populated with*.tsfiles.
Workaround:
- Create a 2nd
package.jsonfile in/dist/package.json:
{
"imports": {
"#src/*": "./src/*",
"#test/*": "./test/*"
},
}
- Rewrite original
package.jsonin/package.json:
{
"imports": {
"#src/*": "./src/*",
"#test/*": "./test/*"
},
}
Explanation:
NODE_ENB=devcode resides in/src/dir as*.tsfiles.NODE_ENB=prodcode resides in/dist/src/dir as*.jsfiles.import: { ... }in/package.jsonconfigures howts-nodewill resolve path aliases in/srcdir, aka theNODE_ENB=devcode.import: { ... }in/dist/package.jsonconfigures hownodewill resolve path aliases in/src/distdir, aka theNODE_ENB=prodcode.
I do not know if this workaround is foolproof. I can imagine problems when trying to publish a package with multiple package.json files. But haven't tested it yet.
Please leave a like if you found this workaround helpful ๐๐๐๐
What I actually discovered was that I have to have the environment variable
NODE_PATHset to match the baseUrl also. I ended up running the following:NODE_PATH=./src ts-node ./src/app/index.ts.
that's exactly it! solved it for me, tks
Thank you dwil618
It works in windows node v18
"ts-node": { "esm": true, "experimentalSpecifierResolution": "node", "transpileOnly": true }
@byleese what is this , where do you put this?
tsconfig.json
{
"compilerOptions": {
"target": "es2017" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"lib": [
"es2017",
"esnext.asynciterable"
] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
"module": "commonjs" /* Specify what module code is generated. */,
"rootDir": "." /* Specify the root folder within your source files. */,
"moduleResolution": "Node" /* Specify how TypeScript looks up a file from a given module specifier. */,
"baseUrl": "./" /* Specify the base directory to resolve non-relative module names. */,
"paths": {
"@src/*": ["./src/*"],
"@prisma/*": ["./prisma/*"]
} /* Specify a set of entries that re-map imports to additional lookup locations. */,
"typeRoots": [
"src/types/libraries/",
"node_modules/@types"
] /* Specify multiple folders that act like './node_modules/@types'. */,
"resolveJsonModule": true /* Enable importing .json files. */,
"declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
"sourceMap": true /* Create source map files for emitted JavaScript files. */,
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
"importHelpers": true /* Allow importing helper functions from tslib once per project, instead of including them per-file. */,
"allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
"strict": true /* Enable all strict type-checking options. */,
"useUnknownInCatchVariables": true /* Default catch clause variables as 'unknown' instead of 'any'. */,
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": ["src/**/*", ".env"],
"exclude": ["node_modules", "src/http", "src/logs", "src/tests"],
"ts-node": {
"require": ["tsconfig-paths/register"]
}
}
package.json
{
"name": "server",
"scripts": {
"dev": "ts-node src/index.ts",
"build": "tsc && tsc-alias",
"start": "node dist/src/index.js"
},
"devDependencies": {
"@types/node": "^20.12.12",
"ts-node": "^10.9.2",
"tsc-alias": "^1.8.10",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.4.5"
},
"version": "1.0.0",
"main": "index.ts",
"license": "MIT"
}