I already add node to the types field in my tsconfig.json. Also, I did npm i #types/node. But webpack doesn't recognize node in webpack.config.ts.
Errors:
Cannot find name 'require'. Do you need to install type definitions for node? Try npm i #types/node and then add node to the types field in your tsconfig.ts(2580)
Cannot find name '__dirname'.ts(2304)
This is my tsconfig.json
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": false,
"esModuleInterop": true,
"module": "commonjs",
"target": "es2015",
"jsx": "react",
"baseUrl": "src",
"typeRoots": ["/node_modules/#types", "#types"],
"types": ["node"]
},
"include": ["./src/**/*.tsx", "./src/**/*.ts"],
"exclude": ["./node_modules", "./**/*.spec.ts"]
}
Related
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?
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 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
I have a typescript module #tlabs/models where I'm simply exporting in index.ts:
export * from '../models......'
where in each file I have something like:
export const Project = typedModel('projects', ProjectSchema);
and my only dependency is ts-mongoose imported in each file simply as:
import { createSchema, Type, typedModel, ExtractProps } from 'ts-mongoose';
ts-mongoose being a dependency that itself required mongoose + mongoose types.
In my typescript node project I have ts-mongoose, mongoose and #tlabs/models as dependencies and #types/mongoose as dev dependency.
Running tsc is fine, the files get compiled and there's no error being thrown out but then trying to run the actual files throws out:
Error: Cannot find module '#tlabs/models'
I have reinstalled all modules several times and checked package.json as well as the actual files on the disk + through vscode and they're right there.
What am I missing out?
My tsconfig is:
{
"include": ["src/**/*"],
"exclude": ["node_modules", "./node_modules", "./node_modules/*"],
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"allowJs": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"noImplicitAny": false,
"alwaysStrict": true,
"strictNullChecks": true,
"types": [],
"lib": [],
"experimentalDecorators": true
}
}
My final TS config for my exported models:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "lib",
"strict": true,
"esModuleInterop": true
},
"include": ["src"]
}
and relevant package.json configuration:
{
"main": "lib/index.js",
"types": "lib",
"scripts": {
"tsc": "tsc",
"build": "tsc -p ."
},
"files": [
"lib"
],
}
I was using Typescript 1.8 with typings, but i decided to upgrade to typescript 2.8 with #types. I downloaded #types/node, but console show errors:
error TS2304: Cannot find name 'require'.
The structure of my project is the following:
node_modules
src >
client
server >
tsconfig.ts
tsconfig.ts:
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"typeRoots": [
"../node_modules/#types"
],
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "server",
"baseUrl": "."
},
"include": [
"src/**/*"
]
}
Adding "types": ["node"] not help
Based on your directory structure, it looks like the path for the typeRoots option is incorrect - it should be a relative path from your tsconfig file. Your node_modules directory is 2 levels higher than the location of your tsconfig, so your type roots should be:
"../../node_modules/#types"
Full tsconfig:
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"typeRoots": [
"../../node_modules/#types"
],
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "server",
"baseUrl": "."
},
"include": [
"src/**/*"
]
}