Vscode + NestJs modules infrequently not found - node.js

This is really driving me nuts: I created a fresh new NestJs project with the #nestjs/cli command. At the beginning everything was fine. Then after adding a controller via nest generate controller mycontroller and installing types for jasmine and node, somehow the modules #nestjs/testing cannot be found anymore.
I recreated a new project over and over and always after a time I get this error, or an error for other packages of nestjs. (for example #nestjs/common).
I already deactivated nearly all extensions (only those that are really not likely to interfere with imports are still active, such as TSLint).
Does anybody have an idea what could be wrong? If it helps, here is my (autogenerated by nestjs) tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"allowJs": true,
"outDir": "./dist",
"baseUrl": "./src"
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
Node Version: 8.11.2
Typescript: 2.9.1
VsCode: 1.24.1

Okay, this was a simple, dump configuration mistake:
The special thing was that I had a project structure like that:
base-project
|
-- sub-project
| |
| tsconfig.json
|
...
and totally missed that I needed to set the rootDir property in tsconfig.json to ./sub-project. After doing so, all modules could be found properly.
For those still having problems, you may also want to take a look at the property baseUrl.
Hope that helps others, having the same problem! Cheers

Related

Cannot find module or its corresponding type declarations (monorepo)

Im trying to install, run and learn https://github.com/CMSgov/design-system, but after installation (yarn install), i can't execute (yarn develop) since im getting error for actually any import from #cmsgov/design-system in packages/docs.
There is something probably that needs to be added or modified in tsconfig.json and this is how it looks like:
{
"compilerOptions": {
"target": "es2015",
"jsx": "react",
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"baseUrl": ".",
"skipLibCheck": true
},
"include": ["packages/**/src/**/*"]
}
Ive tried to manually install package inside docs using:
npm install --save #cmsgov/design-system
but without success. Node versions i've tried to compile with are:
18.12.1
and 16.19.0
Because there are no types in DefinetlyTyped, i've also created decs.d.ts in root:
declare module "#cmsgov/design-system"
Is there maybe something related to lerna settings or something else.
Thanks in advice

Cannot import custom typescript package in node.js

So after a few hours of banging my head against the wall, I decided to ask stackoverflow.
I have a custom typescript project/package which I would like to use with zx. I used npm pack and installed the tar.gz globally, when I try to require or import my custom module I always get the error ERR_MODULE_NOT_FOUND and I can't seem to find the right typescript configuration to make this work. What has to be done to make a typescript module importable in a normal node.js script? This is my current tsconfig.json:
{
"compilerOptions": {
"lib": [ "es2015" ],
"target": "es5",
"esModuleInterop": true,
"moduleResolution": "node",
"downlevelIteration": true,
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"strict": true,
"types": ["node", "jest"]
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
if this is to little information, here is the link to the repo https://github.com/mak1A4/sn-request
EDIT: I already have the NODE_PATH environment variable set in my .zshrc and also tried installing the tar.gz locally inside a normal node.js project, with the same result :-/
I changed my config based on this repo example-typescript-package didn't work either ... but after fixing the typo I made in the import statement it worked ^^

typescript 4.4.4: tsconfig paths not resolving as expected

my tsconfig.json
{
"compilerOptions": {
"target": "ES2018",
"module": "CommonJS",
"lib": ["ESNext", "ESNext.AsyncIterable"],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
"removeComments": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"baseUrl": "./",
"paths": {
"#/*": ["src/*"],
"#config": ["src/config"],
}
},
"exclude": ["node_modules"],
"include": ["./src/**/*.ts"]
}
I am pretty sure there is nothing wrong in tsconfig paths because I even get path autocompletion from my IDE. however when my code is compiled (tsc --project tsconfig.json)
import { PRODUCTION } from '#config';
// or
import { PRODUCTION } from '#/config';
I get following javascript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const _config_1 = require("#config");
console.log(_config_1.PRODUCTION);
//# sourceMappingURL=index.js.map
but javascript doesn't understand what "#" is therefore I get "module not found" error. How can I solve this problem?
See this issue on TypeScript's GitHub project: Module path maps are not resolved in emitted code (#10866)
tl;dr
TypeScript won't rewrite your module paths
paths was designed to help TypeScript understand path aliases used by bundlers
You'll either need to:
use a bundler, such as webpack, and configure its own path maps
use a build time tool, such as tspath, to rewrite the paths
modify the runtime module resolver, with something like module-alias
I've been stuck with this issue for 2 whole days trying to find something that works. #Wing's answer is correct, if you're using plain tsc with no other tools, then Node won't know what you mean by #/ (or whatever your alias is) in your imports.
I've tried a few different approaches and I've found one that required no configuration.
What worked:
At the end of my journey I came across a package that is still actively being maintained: tsc-alias. The only thing I needed to do was add && tsc-alias -p tsconfig.json to my tsc command in my build script and voilĂ , it works. So your full command would be something like tsc -p tsconfig.json && tsc-alias -p tsconfig.json.
What I tried that didn't work:
I tried "module-alias" and I couldn't get it to work.
I tried "tsconfig-paths" as suggested by #AlexMorley-Finch with no luck as well. The package seems to be finding the modules but there seems to be a bug that causes the path of the modules not to be mapped correctly (there's an issue open for it).
I then came across tscpaths. This package requires you to explicitly specify your project, src dir and out dir. I got it to work after figuring out my outdir needed to be ./build/src instead of ./build. The caveat here: this package hasn't been touched since May 2019 as of writing this.

require is not defined in node command line

I have a typescript project that was compiling and running perfectly fine but now I get the error
ReferenceError: require is not defined whenever I try to run node.
I'm not completely sure this is a Typescript issue because when I run a simple plain js file using require I get the same error. I'm a little confused since require should be defined when using node index.js isn't it?
tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"esModuleInterop": true,
"moduleResolution": "node",
"noImplicitAny": false,
"removeComments": true,
"allowSyntheticDefaultImports": true,
"outDir": "dist/",
"sourceMap": false
},
"include": ["src/"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
The problem was that I had enabled experimental support for ES6 Modules in the package.json.
Removing the option: "type": "module" from package.json solved all my problems.
See here: ECMAScript modules on Official NODEjs docs

How can I resolve "Property does not exist error" with shared typescript projects in vscode?

Our enterprise app uses typescript for both node and angular. We have a shared typescript project that is used by both for common classes and such. It's shared using symlinks. When I make a change in the shared project, the node project does not immediately pick up the change. I get the error "Property X does not exists on type Y". I have to pretty much restart vscode to get it to work, or I go and open the .d.ts file so vscode realizes the new method/property exists.
I think I'm missing a configuration somewhere. I feel like vscode should be able to automatically pick up changes. Why is it not?
Here are the details...
VSCode: 1.33.0
Typescrit: 3.2.4
Node: 10.15.0
Shared project's tsconfig.json...
{
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./dist",
"sourceMap": true,
"strict": false,
"target": "es5",
"typeRoots": [
"./node_modules/#types/"
]
},
"exclude": [
"node_modules",
"test/*",
"src/**/*.spec.ts",
"dist/*"
]
}
Excerpt of node project's dependencies in package.json...
"dependencies": {
"shared": "file:../shared",
},

Resources