require is not defined in node command line - node.js

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

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 ^^

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",
},

Vscode + NestJs modules infrequently not found

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

Typescript can not find node core modules

I am trying to write a class with TypeScript and therefore I'd like to import some native node modules which fails because of
[ts] Can not find module 'net'.
The same issue happens for other native node modules. However it does work for bluebird for example. (See Screenshot).
My tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"allowJs": true,
"types": ["node"]
},
"exclude": [
"node_modules"
]
}
My question:
Why does it not recognize native modules like 'net' ?
Edit: This error is thrown when executing the build task: error TS2688: Cannot find type definition file for 'node'

Resources