Just started working with typescript. Unfortunately when I try to build for production it fails.
Firstly I run
tsc
This passes without any error, but when I try to run the build file I get import errors
node build/index.js
The error I get is below:
[0] (function (exports, require, module, __filename, __dirname) { import {
[0] ^^^^^^
[0]
[0] SyntaxError: Unexpected token import
[0] at createScript (vm.js:80:10)
[0] at Object.runInThisContext (vm.js:139:10)
Below is my tsconfig
{
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
],
"compilerOptions": {
"lib": [
"es5",
"es6",
],
"pretty": true,
"target": "es5",
"module": "commonjs",
"outDir": "./build",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true
}
}
I am using node v8.9.3
If you use TypeORM there can be a problem with your ormconfig. Your configuration file probably contains path like src/entities/*.ts in the entity section. So it causes requiring *.ts files from your src folder, not from dist folder.
When working with NodeJs, your tsconfig.json should look like this:
{
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
],
"compilerOptions": {
"lib": ["es6"], // No need for "es5" if you have "es6"
"types": ["node"], // When you code for nodejs
"target": "es6", // NodeJs v8.9.3 supports most of the es6 features
"pretty": true,
"module": "commonjs",
"outDir": "./build",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true
}
}
Related
I am trying to get Node Express/Typescript app that uses Mongoose (which drags in Mongodb) to work with Jest. I know what the error is, I just can't find the correct configuration to get jest and mongodb to play nicely.
No matter what I try I either get:
chart-review-server2.0/node_modules/mongodb/src/bson.ts:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import type {
^^^^^^
SyntaxError: Cannot use import statement outside a module
or if I try to exclude it from the config using transformIgnorePatterns in my jest.config.js configuration like this: transformIgnorePatterns: ['node_modules/(?!(mongodb))'], I get:
RangeError: Maximum call stack size exceeded
at Object.get [as ObjectId] (node_modules/mongodb/src/bson.ts:38:3)
at Object.get [as ObjectId] (node_modules/mongodb/src/bson.ts:38:3)...
My jest.config.js file looks like this:
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
transformIgnorePatterns: [`node_modules/(?!(mongodb))`],
transform: {
'^.+\\.ts?$': 'ts-jest'
},
moduleDirectories: [ "node_modules", "src", "test"],
verbose: true,
testMatch: ["<rootDir>/test/*(*.)+(test).+(ts)"],
setupFiles: [
'dotenv/config'
],
};
my tsconfig.json looks like this:
{
"compilerOptions": {
"lib": [
"es6"
],
"module": "commonjs",
"target": "es5",
"rootDirs": [
"./src"
],
"outDir": "./dist",
"baseUrl": "./src",
"moduleResolution": "node",
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"allowJs": true,
"downlevelIteration": true,
"isolatedModules": false,
"noEmit": true
},
"include": [
"src/**/*.ts",
"test/**/*.ts"
],
"exclude": [
"node_modules"
],
"types": [
"node"
],
"typeRoots": [
"node_modules/#types"
]
}
Thanks in advance for any replies, and for reading this.
Commenting out moduleDirectories on jest.config.js solved the issue for me. All credits to #jimmythecode, see his answer.
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 have monorepo with lerna, and I want to run typescript file (in scripts folder, not a package).
the problem is when I run the file (bar.ts)- it access to some files in the project and throw an error:
Error: Cannot find module '#packages/roles'
Require stack:
- C:\..\prj\packages\pck1\src\foo.ts
- C:\..\prj\packages\pck1\src\index.ts
- C:\..\prj\scripts\bar.ts
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1029:15)
at Function.Module._load (internal/modules/cjs/loader.js:898:27)
at Module.require (internal/modules/cjs/loader.js:1089:19)
at require (internal/modules/cjs/helpers.js:73:18)
I run the command from the root project:
ts-node scripts/bar
I also have tsconfig.json file (in the root project):
{
"ts-node": {
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"baseUrl": ".",
"paths": {
"#packages/*": ["packages/*/src"]
}
}
},
"compilerOptions": {
"noEmit": true,
"strict": false,
"jsx": "react",
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"esModuleInterop": true,
"removeComments": true,
"rootDir": ".",
"baseUrl": ".",
"lib": ["esnext", "dom"],
"types": ["webpack-env", "node"],
"paths": {
"#packages/*": ["packages/*/src"]
}
},
"include": ["packages", "apps", "jest-dom/extend-expect"],
"exclude": ["node_modules", "dist"]
}
In every packages/*/src I have index.ts that expose everything I need.
Not sure what I missing here. why ts-node can't resolve the file?
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've created a package with a module outside of folder with an appication that should contain the module to test it. Then I've added package with npm link #scope/module-name. After that I've added paths to root tsconfig.json.
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "es2015",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es5",
"typeRoots": [
"node_modules/#types"
],
"lib": [
"es2018",
"dom"
],
"paths": {
"#scope/module-name": [
"node_modules/#scope/module-name"
],
"#scope/module-name/*": [
"node_modules/#scope/module-name/*"
]
}
}
}
I've also added package to include in tsconfig.app.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"types": []
},
"include": [
"**/*.ts",
"../node_modules/#scope/**/*.ts",
],
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
And yes, I've added "preserveSymlinks": true in angular.json as it's said in similar questions on SO.
In app.module.ts I'm doing
import { DispRegistryModule } from "#scope/module-name/public_api.d.ts";
But still I'm gettig an error
ERROR in ./node_modules/#scope/module-name/public_api.d.ts Module
build failed (from ./node_modules/#ngtools/webpack/src/index.js):
Error:
D:\internal\test-app\node_modules#scope\module-name\public_api.d.ts
is missing from the TypeScript compilation. Please make sure it is in
your tsconfig via the 'files' or 'include' property.
How can I fix the error?