I tried to start my project with npm run start:prod command but got
Error: Cannot find module {path to my project}\dist\main.js'.
I have tried to rename the path to all my files in the project from src/myController to ../myController .
My package.json (scripts)
"scripts": {
"build": "tsc -p tsconfig.build.json",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "ts-node -r tsconfig-paths/register src/main.ts",
"start:dev": "concurrently \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.json\" ",
"start:debug": "nodemon --config nodemon-debug.json",
"prestart:prod": "rimraf dist && npm run build",
"start:prod": "node dist/main.js"
My tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"noImplicitAny": false,
"removeComments": true,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"rootDir": ".",
"outDir": "../dist",
"baseUrl": "./src"
},
"include": [ "./src/**/*.ts", ],
"exclude": ["node_modules", "dist", "src/**/*.spec.ts", "src/**/__test__/*"]
}
The actual output:
nest-typescript-starter#1.0.0 prestart:prod
{path to my project}
rimraf dist && npm run build
nest-typescript-starter#1.0.0 build
{path to my project}
tsc -p tsconfig.build.json
nest-typescript-starter#1.0.0 start:prod
{path to my project}
node dist/main.js
internal/modules/cjs/loader.js:584
throw err;
^
Error: Cannot find module
{path to my project}\dist\main.js'
Okeey, I was a little dumb. The answer is very easy. In package.json you need change line from "start:prod": "node dist/main.js" to "start:prod": "node dist/src/main.js". In tsconfig.json you need change "outDir": "../dist" to "outDir": "./dist"
you can just add a new command:
under the scripts in package.json :
"start:live": "rimraf dist && nest build && node dist/src/main",
then you can run:
npm run start:live
Related
I'm trying to deploy my typescript project with pm2, but the argument seems not working correctly.
Here is ecosystem.config.yml:
apps:
- name: 'node-app'
script: './src/main.ts'
interpreter: './node_modules/.bin/ts-node'
# they dont't work correctly below.
interpreter_args: '-r tsconfig-paths/register'
# args: '-T -r tsconfig-paths/register ./src/index.ts',
# args: '-r ./node_modules/tsconfig-paths/register ./src/main.ts'
# interpreter_args: ['--require=tsconfig-paths/register']
# interpreter_args: '-P . -r ./node_modules/tsconfig-paths/register'
kill_timeout: 10000
instances: max
exec_mode: cluster
env:
NODE_ENV: development
When I run pm2 start ecosystem.config.yml and check the logs then:
0|node-app | Error: Cannot find module '~framework/common/frame-util'
0|node-app | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
0|node-app | at Function.Module._load (internal/modules/cjs/loader.js:507:25)
0|node-app | at Module.require (internal/modules/cjs/loader.js:637:17)
0|node-app | at require (internal/modules/cjs/helpers.js:20:18)
0|node-app | at Object.<anonymous> (/Users/zhaolin/__CODES__/__PERSONAL__/charley-nest-starter/script/ci/src/solution/gateway/auth/auth.service.ts:5:1)
0|node-app | at Module._compile (internal/modules/cjs/loader.js:689:30)
0|node-app | at Module.m._compile (/Users/zhaolin/.nvm/versions/node/v10.9.0/lib/node_modules/pm2/node_modules/ts-node/src/index.ts:473:23)
0|node-app | at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
0|node-app | at Object.require.extensions.(anonymous function) [as .ts] (/Users/zhaolin/.nvm/versions/node/v10.9.0/lib/node_modules/pm2/node_modules/ts-node/src/index.ts:476:12)
0|node-app | at Module.load (internal/modules/cjs/loader.js:599:32)
And my tsconfig.json is:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"rootDir": ".",
"paths": {
"~framework/*":["src/framework/*"],
"~solution/*":["src/solution/*"],
"~business/*":["src/solution/business/*"],
"~dto/*":["src/solution/dto/*"],
"~entities/*":["src/solution/entities/*"],
"~/*":["src/*"]
}
},
"exclude": ["node_modules", "dist"]
}
It seems that the paths alias are not working properly.
It works well without pm2(this is a nestjs project):
...
"start": "rimraf dist && cross-env NODE_ENV=development ts-node -r tsconfig-paths/register src/main.ts",
...
Can anyone help me with this problem?
Thanks.
Just like Aleksandr said you can first build the project using tsc and then run it using node, instead of ts-node.
Example:
In your script runner (within package.json) you can define:
"build": "tsc",
"runBuild": "pm2 start ecosystem.yml --only node-app"
In your ecosystem.yml you can define:
apps:
- name: 'node-app'
script: './dist/src/main.js'
node_args: '-r ts-node/register -r tsconfig-paths/register'
kill_timeout: 10000
instances: max
exec_mode: cluster
env:
NODE_ENV: development
To run:
$ npm run build
$ npm run runBuild
Hope that helps
In my case, I did resolve PM2 use with TS like the bellow
PM2 config file in the root folder: pm2.config.ts
// TS Setup
module.exports = {
apps: [
{
name: 'AppName',
script: './app/index.ts',
},
],
};
// JS setup
// module.exports = {
// apps: [
// {
// name: 'AppName',
// script: './dist/app/index.js',
// },
// ],
// };
Here is my package.json scripts section
"scripts": {
"compile": "tsc",
"test:ci": "jest",
"clean": "rimraf ./dist",
"test": "jest --watchAll --no-cache",
"ts:dev": "ts-node-dev --poll app/index.ts",
"start": "pm2 start --interpreter ts-node-dev pm2.config.ts",
"pm2:stop": "pm2 stop --interpreter ts-node-dev pm2.config.ts",
"js:dev": "npm run clean && npm run compile && node ./dist/app/index.js"
},
I'm implementing e2e tests into an Angular project. It wasn't started with #angular-cli, so I'm configuring most of it manually.
What I'm trying to do now is to define a script in package.json to transpile only the specs in tests/ folder to tests/compiled.
I tried to follow this question: tsconfig.json - Only build ts files from folder. It recommends to use --rootDir to define the folder.
But if I use this: tsc --rootDir tests --outDir ./tests/compiled, it compiles files from other folders anyways (like ./src). Also, it returns a lot of TS6059 errors, complaining that rootDir should contain all source files.
An example:
error TS6059: File 'C:/Users/Betalabs/Documents/Projetos/Engine-Frontend/src/vendor.browser.ts' is not under 'rootDir' 'tests'. 'rootDir' is expected to contain all source files.
The file hierarchy is this. Please note that the test specs (*.spec.ts) I want to transpile are in the tests/ folder.
(root)
-- src/
-- tests/
-- compiled/
-- (transpiled test files, ".js")
-- helpers/
-- (helper modules, ".ts")
-- page-objects/
-- (page objects, ".ts")
-- forms.spec.ts
-- filters.spec.ts
-- .gitignore
-- .tern-project
-- buildspec.yml
-- conf.js
-- package-lock.json
-- package.json
-- README.md
-- tsconfig.json
-- tsconfig.webpack.json
-- tslint.json
-- typedoc.json
-- webpack.config.js
This is my tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"noEmitHelpers": true,
"importHelpers": true,
"strictNullChecks": false,
"baseUrl": "./src",
"paths": {
"#angular/*": ["node_modules/#angular/*"]
},
"lib": [
"dom",
"es6",
"es2017.object"
],
"typeRoots": [
"node_modules/#types"
],
"types": [
"jasmine",
"hammerjs",
"node",
"source-map",
"uglify-js",
"webpack"
]
},
"exclude": [
"node_modules",
"dist"
],
"awesomeTypescriptLoaderOptions": {
"forkChecker": true,
"useWebpackText": true
},
"compileOnSave": false,
"buildOnSave": false,
"atom": { "rewriteTsconfig": false }
}
I'm "solving" this by compiling all files anyways and removing the unnecessary ones manually. In other words: a complete mess :P
Could you help me with this?
In order to compile those files, you can use
"include": ["tests"]
in your tsconfig
That being said, I think it'd be much more easier to convert the project to use ng cli :)
Good luck
I can not deploy my functions when I try to import a .json credential file.
When running firebase init functions I selected the TypeScript
option.
Then I put my functions in TypeScript and tried loading the .json
file with the credentials of my project in order to use
firebase-admin.
/functions (Directory)
tsconfig.json
{
"compilerOptions": {
"lib": ["es6"],
"module": "commonjs",
"noImplicitReturns": true,
"outDir": "lib",
"sourceMap": true,
"target": "es6",
"types" : [ "node" ],
"esModuleInterop": true,
"resolveJsonModule": true,
},
"compileOnSave": true,
"include": [
"src",
"./typings.d.ts"
]
}
typings.d.ts:
declare module "*.json" {
const value: any;
export default value;
}
package.json:
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.0.3"
},
"devDependencies": {
"tslint": "~5.8.0",
"typescript": "~2.8.3"
},
"private": true
}
functions/src/
-- serviceAccountKey.json
-- index.ts
index.ts:
import * as admin from 'firebase-admin';
import * as serviceAccount from './serviceAccountKey.json';
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "DATABASE-URL"
});
If I remove the import works normally, I can not deploy when I import the file.
Error:
i deploying functions
Running command: npm --prefix "functions" run lint
> functions# lint Z:\functions
> tslint --project tsconfig.json
Running command: npm --prefix "functions" run build
> functions# build Z:\functions
> tsc
tsconfig.json(11,5): error TS5023: Unknown compiler option 'resolveJsonModule'.
+ functions: Finished running predeploy script.
i functions: ensuring necessary APIs are enabled...
+ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
Error: Error parsing triggers: Cannot find module './serviceAccountKey.json'
Try running "npm install" in your functions directory before deploying.
You should use require() (not import) to pull in the contents of a JSON file. If you really must use import (I don't recommend it), read this.
If I remove the import works normally, I can not deploy when I import the file.
A few tsconfig options are missing:
"esModuleInterop": true,
"resolveJsonModule": true,
I have installed typescript using sudo npm install -g typescript, which seems to work because tsc -version returns Version 2.2.2. I also tried installing it locally as this stackoverflow post suggested. I believe that I have everything else installed correctly because the IDE opens other files correctly.
My OS is Ubuntu 16.0.
Error message:
java.lang.IllegalStateException: Node.js could not be found. If it is installed to a location not on the PATH, please specify the location in the TypeScript preferences.
The tsconfig.json is below. I believe that the value of typeRoots should point to the #types location. And indeed the same directory where the .json file resides, there is a node_modules/#types directory, which I would have thought should enable typescript.
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/#types"
],
"lib": [
"es2016",
"dom"
]
}
}
The TypeScript preferences mentioned in the error message are in the Webclipse preferences.
Simply enter the path returned by which node in a shell.
I am trying to create Angular 2 sample ngc and JIT compiler. With JIT compiler i can run the sample but with ngc compiler am facing issue with component.html file path.
npm run ngc
ejangular2-systemjs-starter#1.0.0 ngc E:\AOT\seed-application-changes\sample-AOT
"node" copy-dist-files.js && rollup -c rollup-config.js && ngc -p tsconfig-aot.json
Error: Compilation failed. Resource file not found: E:/AOT/seed-application-changes/sample-AOT/src/src/app.
component.html
at ModuleResolutionHostAdapter.readResource (E:\AOT\seed-application-changes\sample-AOT\node_modules\#a
ngular\compiler-cli\src\compiler_host.js:291:19)
at CompilerHost.loadResource (E:\AOT\seed-application-changes\sample-AOT\node_modules\#angular\compiler
-cli\src\compiler_host.js:230:85)
at Object.get (E:\AOT\seed-application-changes\sample-AOT\node_modules\#angular\compiler\bundles\compil
er.umd.js:26365:111)
at DirectiveNormalizer._fetch (E:\AOT\seed-application-changes\sample-AOT\node_modules\#angular\compile
r\bundles\compiler.umd.js:13744:47)
at DirectiveNormalizer.normalizeTemplateAsync (E:\AOT\seed-application-changes\sample-AOT\node_modules\
#angular\compiler\bundles\compiler.umd.js:13790:25)
Below are my configuration.
sample structure
rollup.config.js
import nodeResolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs';
import uglify from 'rollup-plugin-uglify'
export default {
entry: 'src/main.js',
dest: 'aot/build.js', // output a single application bundle
sourceMap: false,
format: 'iife',
onwarn: function(warning) {
// Skip certain warnings
// should intercept ... but doesn't in some rollup versions
if ( warning.code === 'THIS_IS_UNDEFINED' ) { return; }
// intercepts in some rollup versions
if ( warning.indexOf("The 'this' keyword is equivalent to 'undefined'") > -1 ) { return; }
// console.warn everything else
console.warn( warning.message );
},
plugins: [
nodeResolve({jsnext: true, module: true}),
commonjs({
include: 'node_modules/rxjs/**',
}),
uglify()
]
}
tsconfig-aot.json
{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es2015", "dom"],
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
},
"files": [
"src/app.module.ts",
"src/main-aot.ts"
],
"angularCompilerOptions": {
"genDir": "aot",
"skipMetadataEmit" : true
}
}
package.json
"scripts": {
"start": "concurrently \"npm run tsc:w\" \"npm run lite\" ",
"lite": "lite-server",
"tsc": "tsc",
"tsc:w": "tsc -w",
"ngc": "\"node\" copy-dist-files.js && rollup -c rollup-config.js && ngc -p tsconfig-aot.json",
"serve": "lite-server -c bs-config.aot.json"
},