I want to use nestjs-i18n in an monorepo project and share i18n assets between apps.
I tried with this configuration but the i18n assets are not copied to the dist directory:
"common": {
"type": "library",
"root": "libs/common",
"entryFile": "index",
"sourceRoot": "libs/common/src",
"compilerOptions": {
"tsConfigPath": "libs/common/tsconfig.lib.json",
"assets": [
{
"include": "i18n/**/*"
}
],
"webpack": true
}
Any ideas or suggestion? Thanks in advance!
Our library #ltonetwork/lto, is written in typescript. We use tsc to compile to javascript in the lib folder.
The package contains several sub-packages, which are located in subfolders that contain an index.ts file.
When trying to import a submodule, like this
import {Transfer} from "#ltonetwork/lto/transactions";
I'm expecting this to work, but I get the following error
test.ts:1:24 - error TS2307: Cannot find module '#ltonetwork/lto/transactions' or its corresponding type declarations.
The package.json of #ltonetwork/lto contains
{
"scripts": {
"compile": "tsc -p ./tsconfig.json"
},
"main": "lib",
"exports": {
".": "./lib/index.js",
"./*": "./lib/*/index.js",
"./package.json": "./package.json"
},
"files": [
"lib",
"interfaces.d.ts"
]
}
and the tsconfig.json is
{
"compilerOptions": {
"alwaysStrict": true,
"baseUrl": "",
"lib": ["es2017.object", "es2015", "es6", "dom"],
"module": "commonjs",
"sourceMap": true,
"declaration": true,
"target": "es6",
"paths": {},
"rootDir": "src",
"outDir": "lib"
},
"include": ["src"]
}
I've tried to explicitly name the submodules, instead of using wildcards in exports, but that made no difference.
What am I doing wrong that's causing this import issue?
Edit:
This is not related to monorepos or yarn workspaces. This is about using the exports field in npm with typescript 4.7.1-rc. This feature wasn't working with earlier versions of typescript.
For more information see https://github.com/microsoft/TypeScript/issues/33079
I've also tried
{
"scripts": {
"compile": "tsc -p ./tsconfig.json"
},
"main": "lib",
"exports": {
".": {
"require": {
"default": "./lib/index.js",
"types": "./lib/index.d.ts"
},
"import": {
"default": "./lib/index.js",
"types": "./lib/index.d.ts"
}
},
"./transactions": {
"require": {
"default": "./lib/transactions/index.js",
"types": "./lib/transactions/index.d.ts"
},
"import": {
"default": "./lib/transactions/index.js",
"types": "./lib/transactions/index.d.ts"
}
},
"./package.json": "./package.json"
},
"files": [
"lib",
"interfaces.d.ts"
]
}
Ensure moduleResolution in the consuming library is set to either Node16 or NodeNext in your tsconfig.json file.
Since Typescript 3.1 we've been able to do this and it doesn't require the consumer to have a tsconfig.json.
"typesVersions": {
"*": {
"path1": [ "./path/to/path1.d.ts" ],
"path2": [ "./path/to/path2.d.ts" ],
}
},
This may only work with named submodules.
I am aware that this question has been asked multiple times however after trying most of them I am unable to make it work for my use case. I am building simple API with Typescript and Node. I have all my code residing in the src folder from the root.
To avoid those annoying ../../ i have configured absolute paths in my tsconfig.json with the following declarations.
"baseUrl": "./",
"paths": {
"*":[
"./src/*"
]
},
So lets say i have a config.ts file, I should be able to access it from 'src/controllers/tickets.controllers.ts' by just calling import config from 'config'
This does not work.
Additionally in my .eslintrc.json file i have added the following declarations as mentioned in many any answers related to this question.
{
"env": {
"es2021": true,
"node": true
},
"extends": [
"airbnb-base"
],
"parser": "#typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": [
"#typescript-eslint"
],
"rules": {
"import/extensions": [
"error",
"ignorePackages",
{
"js": "never",
"ts": "never"
}
]
},
"settings": {
"import/resolver": {
"node": {
"extensions": [
".js",
".ts"
]
}
}
}
}
I am trying to run on desktop this repository a vscode extension.
I cloned it locally and run npm install
Press f5 on vscode editor and got an error
Process exited with code 1
(node:1404) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
internal/process/warning:44
Canceled
To work around with the warning I found another stackoverflow question -
(node:9374) Warning: To load an ES module, set "type": "module"
So I set "type":"module" on package.json and press f5 again.
and another error show up.
Process exited with code 1
Uncaught TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /mnt/c/vscode-php-debug/src/phpDebug.ts
And found another question on stackoverflow - Can't run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts
So I remove "type":"module" what happens is I'm in the loop now, confuse.
Does anyone tried debugging this or encounter as such?
Using the following command worked for me with node v14.16.1:
node --loader ts-node/esm --experimental-specifier-resolution=node index.ts
There is a warning telling me that --experimental-loader is an experimental feature but I don't care because I only use it to debug the typescript code.
setting "type": "module" in your package.json should work. Check again if you made an error in inputting it. Solved my problem when I inputted it.
Faced this and lots of connected issues during migrating from js (with commonJs modules) to Typescript with support of Eslint and Prettier. The main problem is that Node.js need commonJs imports in .js files and allow to use es6modules in .mjs. Typescript by default generates .js files, so there are 2 ways to handle it:
Way 1 (if you want to compile your ts files to js with es6modules support):
Migrated to es6 modules, called all .js files - .mjs, change __dirname usages to const __dirname = dirname(fileURLToPath(import.meta.url));
Then i set "type": "module" in package.json
Then i added such config for tsconfig.json:
{
"compilerOptions":
{
"target": "es2018",
"module": "es2020",
"outDir": "dist",
"sourceMap": true,
"allowJs": true,
"esModuleInterop": true,
"moduleResolution": "node",
"strict": true,
},
"include": ["./src"],
"exclude": ["node_modules"],
}
Config for .eslintrc.json looks like that:
{
"root": true,
"env": {
"es2020": true,
"jasmine": true,
"jest": true,
"node": true
},
"settings": {
"noInlineConfig": true,
"node": {
"tryExtensions": [".js", ".ts", ".d.ts"],
"moduleDirectory": ["node_modules", "src/"]
},
"import/resolver": {
"node": {
"extensions": [".js", ".ts", ".d.ts"],
"moduleDirectory": ["node_modules", "src/"],
"typescript": {}
},
"typescript": {
"alwaysTryTypes": true,
"project": "."
}
}
},
"parser": "#typescript-eslint/parser",
"extends": [
"eslint:recommended",
"plugin:#typescript-eslint/recommended",
"plugin:node/recommended",
"airbnb-base",
"prettier"
],
"parserOptions": { "ecmaVersion": 2018, "sourceType": "module" },
"plugins": [
"#typescript-eslint"
],
"rules": {
"import/extensions": "off",
"linebreak-style": "off",
"node/no-unsupported-features/es-syntax": "off",
"no-underscore-dangle": "off",
"import/prefer-default-export": "off",
}
}
In one terminal i run : npx tsc -w
In another: npm run start Start script: "start": "nodemon --es-module-specifier-resolution=node dist/server.js"
Way 2 (if you don't care about compiled js code and it can be commonJS):
Migrated to es6 modules, called all .js files - .ts, add types, leave __dirname usages as is (will be handled by #types/node) and install #types/node, ts-node, nodemon.
In package.json "type": "commonjs", "main": "src/{your_root_file}.ts",
Then i added such config for tsconfig.json:
{
"compilerOptions":
{
"target": "es6",
"module": "commonjs",
"outDir": "dist",
"esModuleInterop": true,
"lib": ["es6"],
"moduleResolution": "node",
"strict": true,
"noEmitOnError": true,
"noImplicitAny": true,
"experimentalDecorators": true, // for typeorm
"emitDecoratorMetadata": true // for typeorm
},
"include": ["./src"],
"exclude": ["node_modules"],
}
Config for .eslintrc.json looks like that:
{
"root": true,
"env": {
"es2020": true,
"jasmine": true,
"jest": true,
"node": true
},
"settings": {
"noInlineConfig": true,
"node": {
"tryExtensions": [".js", ".ts",".mjs", ".d.ts"],
"moduleDirectory": ["node_modules", "src/"]
},
"import/resolver": {
"node": {
"extensions": [".js", ".ts", ".d.ts", ".mjs"],
"moduleDirectory": ["node_modules", "src/"],
"typescript": {}
},
"typescript": {
"alwaysTryTypes": true,
"project": "."
}
}
},
"parser": "#typescript-eslint/parser",
"extends": [
"eslint:recommended",
"plugin:#typescript-eslint/recommended",
"plugin:node/recommended",
"airbnb-base",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
"prettier"
],
"parserOptions": { "ecmaVersion": 2018, "sourceType": "module", "project": "./tsconfig.json" },
"plugins": [
"#typescript-eslint",
"eslint-plugin-tsdoc",
"import"
],
"rules": {
"tsdoc/syntax": "warn",
"import/extensions": "off",
"linebreak-style": "off",
"node/no-unsupported-features/es-syntax": "off",
"no-underscore-dangle": "off",
"import/prefer-default-export": "off",
"#typescript-eslint/no-explicit-any": "error",
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}
In terminal: npm run start Start script: "start": "nodemon src/server.ts"
Nodemon will automatically detect that it is running on .ts file and will use ts-node to run your application.
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/index.js",
"runtimeArgs": ["-r","esm"],
}
]
}
Add
"module": "commonjs"
in tsconfig.json. This will resolve this issue
For more details refer: https://nodejs.org/api/modules.html#modules-commonjs-modules
Add "type": "module" in package.json file.
To fix this change the extension file name.
For example, if your module javascript file was called script.js - change the name to script.mjs.
Do this for both the file that will use the import and the file that will export.
Also in the index.html page ensure that you refer to the file using the .mjs extension for example:
<script type="module" src="script.mjs"></script>
I'm looking for an example project that's using Angular 6 Universal and is successfully deploying to GCP App Engine. I've searched GitHub but can't find an example--the Universal Starter doesn't include deployment. Also, my old Angular 5 Universal config is not working for this new V6 project.
The current error is coming from nginx: "502 Bad Gateway"
It seems to be the favicon.ico file that's causing the problem--looks like a Node.js issue. I just dealt with this on a pure Node.js project (not Angular) and was able to resolve it, but I can't find the right config in the Angular context.
server.ts
app.use(favicon('src/favicon.ico'));
angular.json (start of it)
{
"$schema": "./node_modules/#angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"appname": {
"root": "",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "#angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/browser",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
{
"glob": "favicon.ico",
"input": "src",
"output": "/"
},
{
"glob": "**/*",
"input": "src/assets",
"output": "/assets"
}
],
"styles": [
{
"input": "node_modules/#angular/material/prebuilt-themes/indigo-pink.css"
},
"src/styles.css"
], ...
dispatch.yaml (because I have multiple services using the same domain)
dispatch:
# Default service serves the typical web resources and all static resources.
- url: "*/favicon.ico"
service: default
The issue was not properly starting Node in package.json. This one line in scripts fixed the problem...
"scripts": {
...
"start": "node dist/server.js",