Been struggling for a couple days to run the project in prod and it just keeps showing different errors. The last one(hopefully) is:
> rimraf dist && tsc -p tsconfig.build.json
tsc-watch/test/fixtures/failing.ts(2,1): error TS1005: '}' expected.
But tsconfig.build.json is fine:
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}
It happens after I try to build/run the docker container with:
docker-compose up --force-recreate --build
Here's what I have in the Dockerfile:
FROM node:11-stretch
ADD ./* $HOME/
RUN npm install
RUN npm run build
CMD npm run start:prod
And the package.json
{
"name": "mvp-api",
"private": true,
"version": "0.0.1",
"description": "MVP API",
"license": "MIT",
"scripts": {
"build": "rimraf dist && tsc -p tsconfig.build.json",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "ts-node -r tsconfig-paths/register src/main.ts",
"start:dev": "tsc-watch -p tsconfig.build.json --onSuccess \"node dist/main.js\"",
"start:debug": "tsc-watch -p tsconfig.build.json --onSuccess \"node --inspect-brk dist/main.js\"",
"start:prod": "node dist/main.js",
"lint": "tslint -p tsconfig.json -c tslint.json",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"#nestjs/common": "^6.5.3",
"#nestjs/core": "^6.5.3",
"#nestjs/jwt": "^6.1.1",
"#nestjs/microservices": "^6.0.4",
"#nestjs/passport": "^6.1.0",
"#nestjs/platform-express": "^6.0.4",
"#nestjs/swagger": "^3.1.0",
"#nestjs/typeorm": "^6.1.3",
"#nestjs/websockets": "^6.0.4",
"#nestrx/twilio": "0.0.2",
"#types/mailgun-js": "^0.22.3",
"#types/qrcode": "^1.3.4",
"aws-sdk": "^2.513.0",
"cache-manager": "^2.10.0",
"cache-manager-mongodb": "^0.2.1",
"cache-manager-redis-store": "^1.5.0",
"class-transformer": "^0.2.3",
"class-validator": "^0.10.0",
"dotenv": "^8.0.0",
"hbs": "^4.0.4",
"is-base64": "^0.1.0",
"libphonenumber-js": "^1.7.22",
"mailgun-js": "^0.22.0",
"mysql": "^2.17.1",
"nestjs-config": "^1.4.3",
"onesignal-node": "^2.1.1",
"passport": "^0.4.0",
"passport-jwt": "^4.0.0",
"qrcode": "^1.4.1",
"randomstring": "^1.1.5",
"reflect-metadata": "0.1.13",
"rimraf": "2.6.3",
"rxjs": "6.4.0",
"swagger-ui-express": "^4.0.7",
"twilio": "^3.33.4",
"typeorm": "^0.2.18",
"vcard-generator": "0.0.7"
},
"devDependencies": {
"#nestjs/testing": "6.1.1",
"#types/cache-manager": "^2.10.0",
"#types/express": "4.16.1",
"#types/jest": "24.0.11",
"#types/node": "11.13.4",
"#types/randomstring": "^1.1.6",
"#types/supertest": "2.0.7",
"jest": "24.7.1",
"prettier": "1.17.0",
"supertest": "4.0.2",
"ts-jest": "24.0.2",
"ts-loader": "^6.0.4",
"ts-node": "8.1.0",
"tsc-watch": "2.2.1",
"tsconfig-paths": "3.8.0",
"tslint": "5.16.0",
"typescript": "3.4.3",
"webpack": "^4.39.2",
"webpack-cli": "^3.3.6",
"webpack-node-externals": "^1.7.2"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
It used to build fine with these configurations but after purging all containers/images and trying to recreate everything it just doesn't work.
Does anyone know how to get it to run properly in prod?
Apparently the problem was that the node_modules would keep copying over.
Adding a .dockerignore file with the following content solves the problem:
node_modules
Now I can run the docker container successfully.
Related
I am creating a dependancy to generate PDF on nestjs, to do it I am using puppeteer.
When I try to use puppeteer inside the dependancy I get an error:
Error: Run `npm install` to download the correct Chromium revision (1056772).
But when I do it from the project and not the dependancy there is no error. It only happen when I use puppeteer from the dependancy, but it's launched from the same project.
First I generated the PDF inside a nestjs project and it worked fine.
Then I moved the code to my library inside a service but then there is the error.
I tried to run the following command to install chromium
node node_modules/puppeteer/install.js
But nothing change and here is the result of the command:
Chromium is already in C:\Users\Greg.cache\puppeteer\chrome\win64-1069273; skipping download.
I also tried to delete the node modules and reinstall it but no change.
The package.json of the dependancy:
{
"name": "#gboutte/nestjs-pdf",
"version": "0.0.3",
"description": "This package provide a service to render PDF from html string or from handlebars tempaltes for nestjs.",
"keywords": [
"nestjs",
"handlebars",
"hbs",
"templates",
"pdf"
],
"homepage": "https://github.com/gboutte/nestjs-pdf#readme",
"bugs": {
"url": "https://github.com/gboutte/nestjs-pdf/issues",
"email": "gboutte#protonmail.com"
},
"repository": {
"type": "git",
"url": "https://github.com/gboutte/nestjs-pdf"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rimraf dist && tsc",
"prepublish": "npm run build"
},
"author": "Grégory Boutte <gboutte#protonmail.com>",
"license": "MIT",
"dependencies": {},
"devDependencies": {
"#nestjs/common": "^9.1.6",
"#gboutte/nestjs-hbs": "^0.0.3",
"#types/node": "^18.11.5",
"puppeteer": "^19.1.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.5.7",
"typescript": "^4.8.4"
},
"peerDependencies": {
"#nestjs/common": "^9.1.6",
"#gboutte/nestjs-hbs": "^0.0.3",
"#types/node": "^18.11.5",
"puppeteer": "^19.1.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.5.7",
"typescript": "^4.8.4"
}
}
And the package.json of the main project
{
"name": "nestjs-hbs-demo",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"main": "dist/index.js",
"readmeFilename": "README.md",
"files": [
"dist/**/*",
"*.md"
],
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"#gboutte/nestjs-hbs": "file:../nestjs-hbs",
"#gboutte/nestjs-pdf": "file:../nestjs-pdf",
"#nestjs/common": "^9.0.0",
"#nestjs/core": "^9.0.0",
"#nestjs/platform-express": "^9.0.0",
"#nestjs/platform-fastify": "^9.1.6",
"puppeteer": "^19.4.1",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0"
},
"devDependencies": {
"#nestjs/cli": "^9.0.0",
"#nestjs/schematics": "^9.0.0",
"#nestjs/testing": "^9.0.0",
"#types/express": "^4.17.13",
"#types/jest": "28.1.8",
"#types/node": "^16.0.0",
"#types/supertest": "^2.0.11",
"#typescript-eslint/eslint-plugin": "^5.0.0",
"#typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "28.1.3",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "28.0.8",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "4.1.0",
"typescript": "^4.7.4"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
I resolved the issue by updating my local nodejs and I changed the #types/node dependency of both project to be the same.
"#types/node": "^18.0.0",
I found out that it was this problem by trying to install the library with github instead of locally.
Before I used npm install ../directory-to-my-lib, then I used npm install <url to the github repo>.
The difference is that using github npm gave me an error saying that both version of #types/node were different, I don't know why I did not have this error using the other command.
Now that I fixed the version it works with all installation methods (npm package / github / locally).
I have the following code in main.ts file
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
console.log("working");
await app.listen(3000);
}
bootstrap();
when I run my application it runs successfully, but I am unable to access my controller's endpoints. I do not even see the working from console logs on my screen. It was running fine, but then stopped working after a while. I believe it could be due to some library I have added.
Just in case if it matters, below is my package.json
{
"name": "sfsf",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "npm run start:dev",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"typeorm": "ts-node-dev ./node_modules/typeorm/cli.js"
},
"dependencies": {
"#nestjs/common": "^7.0.0",
"#nestjs/core": "^7.0.0",
"#nestjs/jwt": "^8.0.0",
"#nestjs/passport": "^8.0.1",
"#nestjs/platform-express": "^7.0.0",
"#nestjs/typeorm": "^8.0.2",
"bcryptjs": "^2.4.3",
"class-validator": "^0.13.2",
"dotenv": "^10.0.0",
"module": "^1.2.5",
"mysql2": "^2.3.3",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.4.0",
"typeorm": "^0.2.41"
},
"devDependencies": {
"#nestjs/cli": "^7.0.0",
"#nestjs/schematics": "^7.0.0",
"#nestjs/testing": "^7.0.0",
"#types/express": "^4.17.3",
"#types/jest": "26.0.10",
"#types/node": "^13.9.1",
"#types/passport-jwt": "^3.0.6",
"#types/passport-local": "^1.0.34",
"#types/supertest": "^2.0.8",
"#typescript-eslint/eslint-plugin": "3.9.1",
"#typescript-eslint/parser": "3.9.1",
"eslint": "7.7.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-import": "^2.20.1",
"jest": "26.4.2",
"prettier": "^1.19.1",
"supertest": "^4.0.2",
"ts-jest": "26.2.0",
"ts-loader": "^6.2.1",
"ts-node": "9.0.0",
"tsconfig-paths": "^3.9.0",
"typescript": "^3.7.4"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
Upgrade every #nestjs/* package to v8. Or downgrade them to v7 and rxjs to v6
I try to deploy my nestJS application in app engine with standard env instance. But I received this error:
0mCould not write file '/workspace/dist/my/ts/file': EROFS: read-only file system, open '/workspace/dist/my/ts/file'.
I understand that nodejs will only be able to perform writing in the /tmp directory.
My question is: how can I put the workspace folder (created by cloud build) into tmp folder
here is my app.yaml:
runtime: nodejs12
env: standard
here my package.json:
{
"name": "elengui-api",
"version": "2.0.0",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "npx #nestjs/cli build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "npx #nestjs/cli start",
"start:dev": "npx #nestjs/cli start --watch",
"start:debug": "npx #nestjs/cli start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"gcp-build": "npm run build",
"deploy": "gcloud app deploy"
},
"dependencies": {
"#hapi/joi": "^17.1.1",
"#nestjs/common": "^7.5.1",
"#nestjs/config": "^0.6.3",
"#nestjs/core": "^7.5.1",
"#nestjs/jwt": "^7.2.0",
"#nestjs/mapped-types": "^0.3.0",
"#nestjs/passport": "^7.1.5",
"#nestjs/platform-express": "^7.5.1",
"#nestjs/schedule": "^0.4.3",
"#nestjs/swagger": "^4.7.13",
"#nestjs/throttler": "^1.1.3",
"#nestjs/typeorm": "^7.1.5",
"#types/bcrypt": "^3.0.0",
"#types/cookie-parser": "^1.4.2",
"#types/hapi__joi": "^17.1.6",
"#types/nodemailer": "^6.4.0",
"#types/passport": "^1.0.6",
"#types/passport-jwt": "^3.0.4",
"#types/passport-local": "^1.0.33",
"bcrypt": "^5.0.0",
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",
"cookie-parser": "^1.4.5",
"crypto-random-string": "^3.3.1",
"csurf": "^1.11.0",
"express": "^4.17.1",
"hbs": "^4.1.1",
"helmet": "^4.4.1",
"mysql2": "^2.2.5",
"nodemailer": "^6.5.0",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"path": "^0.12.7",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.3",
"swagger-ui-express": "^4.1.6",
"typeorm": "^0.2.31"
},
"devDependencies": {
"#nestjs/cli": "^7.5.4",
"#nestjs/schematics": "^7.1.3",
"#nestjs/testing": "^7.5.1",
"#types/cron": "^1.7.2",
"#types/express": "^4.17.11",
"#types/jest": "^26.0.15",
"#types/node": "^14.14.6",
"#types/supertest": "^2.0.10",
"#typescript-eslint/eslint-plugin": "^4.6.1",
"#typescript-eslint/parser": "^4.6.1",
"eslint": "^7.12.1",
"eslint-config-prettier": "7.2.0",
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.6.3",
"prettier": "^2.1.2",
"supertest": "^6.0.0",
"ts-jest": "^26.4.3",
"ts-loader": "^8.0.8",
"ts-node": "^9.0.0",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.0.5"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
This error is explained with trying to write to directory diffent than /tmp.
Official documentation contain following:
While Cloud Storage is the recommended solution for reading and
writing files in App Engine, if your app only needs to write temporary
files, you can use standard Node.js methods to write files to a
directory named /tmp.
So this is the error, it seems your code is trying to use directory mentioned in the error massage. I do not have playgroud at hand for it, but I suppose this is related with start script using npx. You should use node <your-app.js> instead. I found different question where this was confirmed by question poster.
Below I have a sample test for a Typescript project. I'm using mocha chai to do the testing. The test case is blank for now.
import {KafkaConsumer} from '../infrastructure/delivery/kafka/kafka-consumer';
import {expect} from 'chai';
import {describe, it} from 'mocha';
describe('KafkaConsumer Initialization', () => {
it('should initialize KafkaConsumer', () => {
})
})
When I run the test I get the following error:
import {describe} from 'mocha';
^
SyntaxError: Unexpected token {
Any idea why this might be? If I change line 3 to import 'mocha'; I a similar SyntaxError. Without that line, describe() and it() are not defined
Below is my package.json file.
{
"name": "wss",
"version": "0.0.0",
"private": true,
"main": "dist/index.js",
"scripts": {
"tsc": "tsc",
"start": "node --inspect=5858 -r ts-node/register ./src/index.ts",
"start:watch": "nodemon",
"build": "tsc",
"prod": "tsc && npm run postbuild && node ./dist/index.js",
"postbuild": "npm run copy-files",
"copy-files": "copyfiles -u 1 ./src/**/*.js dist",
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register 'tests/**/*.ts'",
"test:watch": "nodemon --exec npm test"
},
"nyc": {
"require": [
"ts-node/register"
],
"include": [
"src/**/*.ts"
],
"extension": [
".ts"
],
"reporter": [
"lcov",
"text"
],
"sourceMap": true,
"instrument": true
},
"dependencies": {
"#types/dotenv": "^6.1.1",
"#types/newrelic": "^5.11.0",
"#types/redis": "^2.8.21",
"aws-iot-device-sdk": "^2.2.4",
"axios": "^0.19.0",
"bluebird": "^3.5.5",
"chai-http": "^4.3.0",
"cookie-parser": "~1.4.3",
"copyfiles": "^2.2.0",
"dotenv": "^7.0.0",
"express": "~4.16.0",
"kafka-node": "^4.1.3",
"lodash": "^4.17.15",
"morgan": "~1.9.0",
"newrelic": "^5.11.0",
"redis": "^3.0.2",
"reflect-metadata": "^0.1.13",
"uuid": "^8.0.0"
},
"devDependencies": {
"#types/bluebird": "^3.5.27",
"#types/express": "^4.16.1",
"#types/jsonwebtoken": "^8.3.2",
"#types/lodash": "^4.14.150",
"#types/mocha": "^8.0.0",
"#types/node": "^11.13.14",
"#types/winston": "^2.4.4",
"chai": "^4.2.0",
"eslint": "^4.19.1",
"mocha": "^8.0.1",
"nodemon": "^1.18.11",
"nyc": "^14.1.1",
"ts-node": "^8.10.2",
"ts-node-dev": "^1.0.0-pre.41",
"tslint": "^5.16.0",
"typescript": "^3.9.2",
"typescript-eslint-parser": "^22.0.0"
}
}
RESOLVED! test script in package.json should be changed to:
"test": "mocha --require ts-node/register --watch-extensions ts './src/test/test.ts'",
I have followed the tutorial for nestJs and have a basic working app. After that, I tried the tutorial to connect my app with webpack and hotreloading and am now presented with the following error:
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:703:23)
I have tried looking at similar errors and cannot find anything conclusive other than hints here and there to something that is not compiling in my test files - any direction would be greatly appreciated.
Using Node 12, npm 6.9.0
Package JSON below:
"scripts": {
"build": "tsc -p tsconfig.build.json",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "node dist/server",
"webpack": "webpack --config webpack.config.js",
"start:dev": "concurrently --handle-input \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" ",
"start:debug": "nodemon --config nodemon-debug.json",
"prestart:prod": "rimraf dist && npm run build",
"start:prod": "node dist/main.js",
"lint": "tslint -p tsconfig.json -c tslint.json",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"#nestjs/common": "^6.0.4",
"#nestjs/core": "^6.0.4",
"#nestjs/microservices": "^6.0.4",
"#nestjs/platform-express": "^6.0.4",
"#nestjs/swagger": "^3.0.2",
"#nestjs/typeorm": "^6.1.1",
"#nestjs/websockets": "^6.0.4",
"mysql": "^2.17.1",
"reflect-metadata": "0.1.13",
"rimraf": "2.6.3",
"rxjs": "6.4.0",
"swagger-ui-express": "^4.0.4",
"typeorm": "^0.2.17"
},
"devDependencies": {
"#nestjs/testing": "6.1.1",
"#types/express": "^4.16.1",
"#types/jest": "24.0.11",
"#types/node": "11.13.4",
"#types/supertest": "2.0.7",
"concurrently": "^4.1.0",
"jest": "24.7.1",
"nodemon": "1.18.11",
"prettier": "1.17.0",
"supertest": "4.0.2",
"ts-jest": "24.0.2",
"ts-loader": "^6.0.1",
"ts-node": "8.1.0",
"tsconfig-paths": "3.8.0",
"tslint": "5.16.0",
"typescript": "3.4.3",
"wait-on": "^3.2.0",
"webpack": "^4.32.2",
"webpack-cli": "^3.3.2",
"webpack-node-externals": "^1.7.2"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
this is probably the tsconfig, are you using:
"module": "commonjs" In the compilerOptions ? When you working with nodejs this is the option that you have to use
I finally posted this on NestJS repo and received a response for those interested:
https://github.com/nestjs/nest/issues/2389