I have a node app written in TypeScript that compiles with tsc and then runs with node ./dist/index.js with no problem. However, I would like to use nodemon to watch the source and so have been playing with ts-node.
I have run into and solved a few issues, but the issue I have now is a problem using the threads.js library:
require() of ES modules is not supported.
As I say, tsc produces the working target no problem, so why might it be falling over with this?
I am currently just trying ts-node --esm ./src/index.ts. I have also tried this in my tsconfig.json:
"ts-node": { "esm": true, "experimentalSpecifierResolution": "node" },
Here is my tsconfig.json:
{
"ts-node": { "esm": true, "experimentalSpecifierResolution": "node" },
"compilerOptions": {
"target": "es2020",
"lib": [
"es2015",
"es2016",
"es2020",
"esnext",
"dom"
],
"module": "es2020",
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"noImplicitAny": false
},
"include": [
"src/**/*.ts",
"typings.d.ts"
]
}
Clues?
Related
I am using v12.14.1 version, in my project when try to type npm run test, I am getting this warning There is a mismatch between your NodeJs version v12.14.1 and your TypeScript target ESNext. This might lead to some unexpected errors when running tests with ts-jest
and all my test is failed, please can you say what node version should I use, or how can I fix this warning, to pass my tests
The answer is in this cheatsheet.
You are running node version 12, so you must set "target": "es2019"
and "lib": ["ES2019"] in your tsconfig.json.
Example of what your tsconfig can look like:
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"noImplicitAny": false,
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "./dist",
"sourceMap": true,
"strict": true,
"target": "es2019",
"lib": [
"es2019",
"es2020.bigint",
"es2020.string",
"es2020.symbol.wellknown"
],
"typeRoots": ["node_modules/#types"],
"resolveJsonModule": true
},
"compileOnSave": true,
"include": ["./src/**/*"],
"exclude": ["node_modules"]
}
I Always get this error when creating a new test file in repo.
Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i --save-dev #types/jest` or `npm i --save-dev #types/mocha`.
Error only goes away when I close and reopen vscode which is strange. As you can imagine this is not ideal solution. I am pretty sure I am doing everything correctly.
Anyone know why I always get this error? Also happens when I create a new styles.module.sccs file too.
package.json
"devDependencies": {
"#testing-library/dom": "^8.11.3",
"#testing-library/user-event": "^13.5.0",
"#types/jest": "^27.4.0"
}
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"types": ["jest", "node"],
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
],
"files": ["assets.d.ts"]
}
I have this project structure:
How to configure tsconfig.json for using paths like #app/, #server/.
I try this:
{
"compilerOptions": {
"module": "CommonJS",
"target": "es5",
"lib": [
"esnext",
"dom"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": false,
"baseUrl": "..",
"paths": {
"#app/*": [
"app/*"
],
"#server/*": [
"server/*"
]
}
},
"include": [
"."
]
}
Same config works with webpack and ts-loader, but when i run npx ts-node server/index.ts i got error:
npx ts-node server/index.ts
Error: Cannot find module '#server/a'
server/index.ts :
import a from '#server/a'
console.log('This is index.ts')
a()
Your config works for webpack because you run webpack from the project root. It does not work for server.ts because the path is relative to its directory. Try:
"paths": {
"#app/*": [
"../app/*"
],
"#server/*": [
"../server/*"
]
}
If you need to do it for both, you need two different tsconfig.json - one in the root and one in app or server.
Take a look at my project: https://github.com/mmomtchev/rlayers
It uses this feature a lot.
I get this error in my web application
ERROR in node_modules/zone.js/dist/zone.js.d.ts:600:21 - error TS2503: Cannot find namespace 'NodeJS'.
600 declare var global: NodeJS.Global;
As you can see here below i already added the types": ["node"] property but it's still not working
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"types": ["node"],
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "es6",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"typeRoots": [
"node_modules/#types"
],
"lib": [
"es2018",
"dom"
]
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
I don't really know how solve it. The applicationn starts the first time getting Cannot GET / and then, after saving whatever file, it works but still with the NodeJS error. Thanks
For those who have this kind of problem i solved updating zone.js at version "zone.js": "^0.11.1"
While upgrade angular 8 to 13 i am facing same. Because zone.js not installed to my project. so install zone js the problem resolved.
npm i zone.js
I am trying to install moment in node js.But getting the below error :--
error TS2307: Cannot find module 'moment'.
import * as moment from 'moment';
Below is my tsconfig
{
"compilerOptions": {
"module": "commonjs",
"strict": true,
"baseUrl": "./",
"outDir": "build",
"removeComments": true,
"noImplicitAny": false,
"experimentalDecorators": true,
"target": "es6",
"emitDecoratorMetadata": true,
"moduleResolution": "node",
"importHelpers": true,
"types": [
"node",
"jest"
],
"typeRoots": [
"node_modules/#types"
]
},
"include": [
"./src/**/*.ts"
],
"exclude": [
"./src/public/"
]
}
Below is the command I have executed so far --
npm install moment
npm install --save -dev #types/moment
Please let me know if I need to add some more information