error TS5083: Cannot read file '/tsconfig.json' - node.js

I am trying to run the command "tsc --build" when running "docker compose up --build" so it can run that command when creating the Docker container.
The thing is I am getting always an error:
I have been reading about it and I am not importing in my code anything outside the "rootdir"... so I have no idea why those packages are there...
I am not sure if removing the rootdir option from tsconfig file is a good idea or it would cause an error afterwards in my app...
Any ideas?

You need to copy your tsconfig.json to your Docker image.
Add the following after line 5:
COPY tsconfig.json ./
Notes:
You probably need to also copy the nodemon.json
At the line 10 you copy all the current directory to your image, ensure your .dockerignore file contain a rule to exclude the node_modules/, dist/ folders.

Related

Missing /src and /dist in node_module (with workspaces)

I forked a package and made some changes and need to install it locally. Here's my branch.
I ran:
yarn add 'saber2#https://github.com/ilmoi/saber-common#head=ilmoi_support_v0'
which successfully installs the package, but it's missing both /src and /dist and so it unusable.
Having searched for similar issues I unsuccessfully tried:
building and commiting /dist dir (made sure not in .gitignore)
adding a "files" line to package.json (both top level and inside the workspace)
adding a "prepare" line
adding a blank .npmignore
None of those works. Here's how the package looks in node modules:
I suspect the problem is to do with workspaces. For some reason none of them compile / show. Please advise on what I'm missing.

run eslint in multi repository project

let's say, I have following project structure:
back/package.json
back/lib/Content/*.js
front/package.json
slices/budget/back/package.json
slices/budget/back/lib/Content/*.js
slices/budget/front/package.json
slices/accounting/back/package.json
slices/accounting/back/lib/Content/*.js
slices/accounting/front/package.json
how do I?
cd back && eslint ./lib/**/*.js ../slices/**/lib/Content/*.js
specifically, I want to
install eslint one time as devDependencies
somewhere in /back of root module
config eslint one time somewhere in /back/package.json:eslint key of root module
add eslint config in /back/package.json of root module just one time
eslint entire tree of modules
not in each slice seperatly
run from ci cd
so I need a way to run from /back
and later - maybe someway to respect eslint config hierarchy
not change project directory structure at all
what I receive
cd back && npm run lint
> back#1.0.0 lint
> eslint ../
Oops! Something went wrong! :(
ESLint: 8.23.1
ESLint couldn't find a configuration file
reason: https://eslint.org/docs/latest/user-guide/configuring/configuration-files#using-configuration-files
You can use the --ignore-path option to specify a file with patterns that should be ignored. The file should contain one pattern per line. For example, to ignore all files in the node_modules directory, you could create a .eslintignore file with the following contents:
node_modules
You can also use the --ignore-pattern option to specify a pattern that should be ignored. For example, to ignore all files in the node_modules directory, you could run:
eslint . --ignore-pattern node_modules
The error is probably because you haven't specified the eslint config file explicitly. To run eslint on all the modules, starting from the parent folder, run: eslint ../ -c .eslintrc.js (or whatever .eslintrc file you use in back). It seems like eslint is confused if it does not have the config file in the same directory it is running from hence you need to manually specify the path to it.
The correct way of solving this issue would be creating sharable config file with configuration you have in back right now:
module.exports = {
rules: {
semi: [2, "always"]
}
};
Then you publish it to public or private npm with a name #your-project/eslint-config and use it in .eslintrc.json that is the same in all your projects:
{
"extends": [
"#your-project/eslint-config"
]
}
This way gives you ability to configure CI in a simple and independent way if you have lots of repositories: just run eslint lib/*.js.
If you have all the repositories in one computer and want to lint all of them using one command, you can use one of my tools:
redfork, install eslint and redfork globally and run:
redfork 'eslint lib/*.js'
But maybe you need to have some changes in project structure.
runny, if you don't want to make changes in project structure, just add configuration file .runny.json:
{
"command": "eslint lib/*.js",
"directories": [
"~/one",
"~/two",
"~/three"
]
}
It will run the same command for any directory you need.
I had a similar issue and the following has solved my problem.
I guess you haven't specified the eslint config file explicitly.
To run eslint on all the modules
run: eslint ../ -c .eslintrc.js
It seems like eslint is confused if it does not have the config file in the same directory it is running from, so you need to manually specify the path to it.
no real answer, except to create .eslintignore, .eslintrc, package.json at project root

html-minifier-terser ignores its output argument

Within the root directory of my project, I run the following command using npm and html-minifier-terser:
npx html-minifier-terser --input-dir src/ --output-dir dist/ src/index.html -o index.min.html --file-ext html
The output file to dist/ is index.html and not index.min.html.
I tried to install html-minifier-terser globally, which does not help. I then tried to remove the specification of input and output directories within the command, which resulted in the expected behavior.
Any suggestions why I can't get dist/index.min.html to work?
Doing a little more research, I found a solution on this thread ng build production should minify index.html in dist.
The workaround is to omit using the input and output directory arguments and instead specify the output path as in --output dist/index.min.html.

Docker compose v3 named volume & node_modules from npm install

Using compose v3.
In the build I copy package.json and run npm install into
/var/www/project/node_modules
I dont add any code in the build phase.
In compose I add volumes
- ./www:/var/www/project/www
As everyone knows the host bind to /www will effectively "overwrite" the node_modules I installed during the build phase.
Which is why we add a named module afterwards
- ./www:/var/www/project/www
- modules:/var/www/project/www/node_modules
this works fine and dandy the first time we build/run the project
since the named volume "modules" doesnt exist, the www/node_modules from the build phase will be mounted instead.
HOWEVER, in this is the actual issue.
The next time I make a change to package.json and do:
docker-compose up --build
I can see how the new npm modules are installed but once the named "modules" volume is attached (it now exists stuff there from the previous run) it "overwrites" the newly installed modules in the image.
the above method of adding a named volume is suggested in tons of places as a remedy for the node modules issue. But as far as I can see from lots of testing this only works once.
if I were to rename the named volume every time I make a change to package.json it would of course work.
A better thing would be to include rm command in your entrypoint script to clean out node modules before running npm install.
As an alternative, you can use $ docker system prune before running another build. This will make sure that no earlier things are being used.

Starting an app fails

I have a node.js app that compiles the runtime version into the dist folder. therefore the package.json file specifies the start script node dist/index.js.
I now want to containerize it but the container doesn't need to have the distribution directory and those files really should live in the root of the app, thus my Dockerfile contains the lines
COPY package.json /usr/src/app/
COPY dist/* /usr/src/app/
which places the runtime files in the image. the problem I have is that when the docker file issues its last command:
CMD ["npm", "start"]
it fails because the index.js is now in the wrong location (because it's looking for it in the non-existent distribution directory). I could solve the problem by issuing:
CMD ["node", "index.js"]
instead but that seems like the wrong thing to do. what is the correct approach?
* update I *
I had modified the index.js so it could run from the root of the project (i.e. it expects to find the resources it needs in the dist/ folder) by issuing a node dist/index.js, but of course, this is now also a problem since there is no distribution directory. how is this generally approached?
I would code all your javascript require calls relative to the current file with no reference to the "dist" directory. So let's say you have index.js and routes.js in the project root. index.js loads routes via var routes = require('./routes'). When you compile these, compile BOTH of them into the dist directory and all should be well.
If you want to run via npm start from the project root, you can configure that to do node dist/index.js.
For docker, there's no reason to use npm at all to launch your app. It's an unnecessary process executing for no benefit. Just launch via node index.js in your dockerfile with WORKDIR /usr/src/app.

Resources