With TypeScript 2.8, we have now the possiblity to generate .d.ts only from source files. So my tsconfig.json becomes :
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"module": "commonjs",
"target": "es5",
"lib": ["ES6","ES2017","ESNext"],
"downlevelIteration": true,
"moduleResolution": "node",
"declaration": true,
"emitDeclarationOnly": true,
"declarationDir": "./lib"
},
"exclude": [
"node_modules"
],
"include": [
"src/**/*"
],
"strict": true
}
The problem is the build is broken with that (https://travis-ci.org/jy95/mediaScan/jobs/359573738).
My repo : https://github.com/jy95/mediaScan/tree/prettier
Any ideas how to fix it ?
My jest.config.js :
// jest.config.js
module.exports = {
verbose: true,
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testMatch": [
"<rootDir>/__tests__/**/*.(ts|tsx|js)"
],
"testPathIgnorePatterns": ["<rootDir>/node_modules/", "<rootDir>/__tests__/__helpers__/"],
"collectCoverage": true
};
Thanks
I've experienced similar issue when was switching jest to process .js files with ts-jest instead of babel-jest.
Error happens (according to stacktrace) at getConstructorDefinedThisAssignmentTypes in typescript.js. And is most likely caused by incorrect or missing types. Add some breakpoints or log types there/declarations to see what exactly is failing.
You can try to upgrade typescript to the latest version, it might give more valuable error message, and try to tsc --noEmit to see if there are some loose ends with your types.
Also sometimes npx jest --clearCache helps
Related
So, I saw many similar issues, but most of them refer to built code, and this one is actually a CLI script.
My command is:
node_modules/.bin/babel-node -x .js,.jsx,.ts,.tsx scripts/database/index.ts generate
And if it calls some code from node_modules (React Native related to be precise) it will throw the error.
I tried type: module but it caused even worse errors.
babel.config.js:
module.exports = {
presets: [
'module:metro-react-native-babel-preset',
'#babel/preset-flow',
],
plugins: [
['#babel/plugin-proposal-decorators', { legacy: true }],
['#babel/plugin-transform-flow-strip-types'],
[
require.resolve('babel-plugin-module-resolver'),
{
root: ['.'],
extensions: [
'.ios.js',
'.android.js',
'.js',
'.ts',
'.tsx',
'.json',
// '.png',
],
alias: {
app: ['./app'],
'test/*': ['test/'],
'#components': './app/components',
},
},
],
],
sourceMaps: true,
};
tsconfig.json:
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"isolatedModules": true,
"jsx": "react",
"lib": ["ES2016"],
"module": "commonjs",
"moduleResolution": "node",
"noEmit": true,
"resolveJsonModule": true,
"strict": true,
"target": "esnext",
"baseUrl": "./",
"paths": {
"app/*": ["app/*"],
"tests": ["tests/*"],
},
// ensure ignores node_modules
"skipLibCheck": true,
"preserveSymlinks": true,
"typeRoots": ["./node_modules/#types"],
"types": [
"node",
"#wdio/types",
"webdriverio/async",
// "#wdio/jasmine-framework",
// "expect-webdriverio/jasmine",
"jest"
]
},
// "include": [
// "src/*",
// "tests/*",
// ],
"exclude": [
"node_modules",
"babel.config.js",
"metro.config.js",
"jest.config.js",
// isnores special cases
"**/modules/**",
"node_modules/react-native/**",
"node_modules/#react-navigation/**",
]
}
remove "type": "module"
install #babel/register
in your very first line of code, add this line
require('#babel/register')({ extensions: ['.js', '.jsx', '.ts', '.tsx'] })
npm script
"start": "babel-node -x js,.jsx,.ts,.tsx -- src/app.ts"
update:
turn out that you can skip the whole #babel/register thing, what you are missing is a -- in your command
Woof this was rough.
I recently started gradually adopting TS into my Node app and I thought that was the cause.
But I'm starting to think the default option for the --config in babel-node is no longer looking for babel.config.js and rather only looking for babel.config.json.
I painstakingly converted my babel config to JSON and that seems to have fixed.
Also, the --extensions argument must include .ts. So to support both .js and .ts files in the same project you need to define this:
package.json
"scripts": {
"task": "babel-node --extensions .js,.ts -- ",
}
babel.config.json
{
"presets": [
"#babel/preset-typescript",
[
"#babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
],
"plugins": [
[
"module-resolver",
{
"root": ["./"],
"alias": {
"test": "./__test__"
}
}
]
]
}
Then finally I was able to execute CLI commands like so:
yarn task something.js
or
yarn task something.ts
I need some clarification here: If your application's package.json has dependencyA and another commonDependency, and commonDependency's package.json already has dependencyA, does your application need dependencyA in its package.json? My guess is no, but I need to confirm.
I am trying to reduce the time (currently ~60 mins on our Jenkins server) it takes for this step: Generating es5 bundles for differential loading...
I believe by removing some of these "peer/nested" dependencies from our application, the build times will improve.
Application Package JSON
"dependencies": {
"#dependencyA": "1.2.3", <-- is this required here?
"#commonDependency": "2.3.4"
}
CommonDependency Package JSON
"dependencies": {
"#dependencyA": "1.2.3",
"#dependencyB": "4.5.6",
"#dependencyC": "7.8.9",
}
Application TSConfig JSON
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"paths": {
"#angular/*": [
"./node_modules/#angular/*"
]
},
"module": "es2020",
"moduleResolution": "node",
"importHelpers": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"target": "es2015",
"preserveSymlinks": true,
"downlevelIteration": true,
"typeRoots": [
"node_modules/#types"
],
"lib": [
"es2018",
"dom",
"esnext.asynciterable"
]
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
UPDATE:
My issue now is this:
In the Application, there are .ts files that have
import ... from #dependencyA.
So when I remove the dependencyA from Application package.json, the ng build fails.
It will not resolve
import ... from #commonDependency, which has dependencyA in its package.json. Any suggestions?
I building a boildplate for nestjs and microservices (still work in progress)
now i got an issue that i try to run the app or run the test and it give me this
the code located github link
the issue that on tsconfig.json i added paths
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist/apps",
"baseUrl": "./",
"incremental": true,
"paths": {
"#frontapi/*": ["./apps/front-api/src/*"],
"#config/*": ["./apps/config-server/src/*"],
"#entities/*": ["./apps/entities-server/src/*"],
"#devops/*": ["./apps/devops-mcu/src/*"],
"#tasks/*": ["./apps/task-que-handler/src/*"],
"#logicBus/*": ["./apps/logic-bus/src/*"],
"#common/*": ["./apps/common/*"]
}
},
"exclude": [
"node_modules",
"dist"
]
}
and i run the package json two of the commands for testing:
"start:frontapi": "npm run start:frontapi:env && tsc-watch -p tsconfig.build.json --onSuccess \"node -r tsconfig-paths/register -r ts-node/register --inspect-brk=5858 dist/front-api/src/main.js\"",
and
"test:e2e": "npm run clean&&tsc -p tsconfig.build.json && jest --config ./apps/front-api/test/jest-e2e.json",
and both give me that it dont located #common/config/configuration
and i have no idea what i have missing here and help with this one
Edit there is file called jest-e2e.json this his content
{
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"moduleNameMapper": {
"^#frontapi/(.*)$": "./apps/front-api/src/$1",
"^#config/(.*)$": "./apps/config-server/src/$1",
"^#entities/(.*)$": "./apps/entities-server/src/$1",
"^#devops/(.*)$": "./apps/devops-mcu/src/$1",
"^#tasks/(.*)$": "./apps/task-que-handler/src/$1",
"^#logicBus/(.*)$": "./apps/logic-bus/src/$1",
"^#common/(.*)$": "./apps/common/$1"
}
}
please let me know if i missing something here or you missing something will do my best to provide
I was facing the same issue.
After taking a look at your project structure, I have some suggestions/observations. I will focus on apps/front-api.
There you have the jest.e2e.json file inside the test directory
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
...,
"moduleNameMapper": {
"^#frontapi/(.*)$": "./apps/front-api/src/$1",
"^#config/(.*)$": "./apps/config-server/src/$1",
"^#entities/(.*)$": "./apps/entities-server/src/$1",
"^#devops/(.*)$": "./apps/devops-mcu/src/$1",
"^#tasks/(.*)$": "./apps/task-que-handler/src/$1",
"^#logicBus/(.*)$": "./apps/logic-bus/src/$1",
"^#common/(.*)$": "./apps/common/$1"
}
}
I'd like to point out that the rootDir: '.' makes reference to the directory the file is in, so in this case rootDir value will be /path/to/your/projectShowCase/backend/apps/front-api/test.
The important thing is that you can use relative paths in the moduleNameMapper. For instance:
"moduleNameMapper": {
"^#frontapi/(.*)$": "<rootDir>/../src/$1",
"^#config/(.*)$": "<rootDir>/../../config-server/src/$1",
...,
"^#common/(.*)$": "<rootDir>/../../common/$1"
}
You just need to map each dependency to its specific path
Im trying to use WebWorkers in Typescript and I have encoutered a problem.
I have installed #types/node and updated everything. 'worker_threads' is not longer experimental and is in stable version of node.js.
This is my typescript config:
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
"webpack-env",
"node",
"dom-mediacapture-record"
],
"paths": {
"#/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}
When Im trying to do import in my typescript file like this:
import { Worker } from 'worker_threads';
Im getting error:
This dependency was not found:
* worker_threads in ./src/SpeechRecognizer/SpeechRecognizer.ts
To install it, you can run: npm install --save worker_threads
Type checking and linting in progress...
No type errors found
No lint errors found
Version: typescript 3.6.3, tslint 5.20.0
Also Im using Vue.js for frontend.
I have already tried everything I could find so Im glad for every help.
If this wont work I will propably switch to JavaScript.
Thanks.
You can use the npm package 'worker-loader'. Goto the below link https://www.npmjs.com/package/worker-loader and refer the section 'Integrating with TypeScript'
I found typescript version for node unfortunately it's not able find "child_process" module from the node packages.
Exactly the error message in my console is Module not found: Error: Can't resolve 'child_process'
When I went through some blog they recommended to add below code in your tsconfig.json file.
"typeRoots": [
"node_modules/#types"
]
Same thing as I pasted over here unlikely it's did't help.
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"baseUrl": "src",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/#types"
],
"lib": [
"es2016",
"dom"
]
}
}
Why they don't inject this node module ? Am I doing something wrong in tsconfig.json file? I would be a grateful if you could give a little help.
Thanks in advance.
Maybe is the path:
"typeRoots": [
"../node_modules/#types"
]
Usually you have your tsconfig in a src.
Update:
My original answer makes no sense, because you have this "baseUrl": "src".
As instead, I believe it could be a Webpack issue.
Try adding this to your webpack.config file:
target: 'node'
Update 2:
Workaround: Add the following to your config file:
node: {
child_process: 'empty'
}