I'm unable to move my index.ts file inside a src folder to organize my project.
I'm using prisma and following the 'from the scratch tutorial' (https://www.prisma.io/docs/getting-started/setup-prisma/start-from-scratch) which require just a index.ts to query the database. When I run the script just from the root folder using npx ts-node index.ts it runs fine.
But when I put it inside a folder (in my case, src/controller, two folders), it logs a
error: Cannot find module './index.ts
If I run npx ts-node src/controller/index.ts with the path to index.ts, it does run fine, but there is a way to configure the path, so I can just type index.ts?
Related
Whenever I create Vue and React projects using npm, it generates a "tmpnodejsnpm-cache" folder in my project directory.
And after deleting this folder also project runs properly but if I hit the "npm i" command again it will again create same folder, I want to get rid of it.
I have a project structure that looks like:
root/
controllers/
database/
app.js
server.js
.eslintrc.json
package.json
etc...
I want to run eslint from command line to lint all .js files in root and subdirectories, but trying to use global patterns at this level is giving different errors:
If I try: eslint ./**/*.js, I start getting errors from node_modules, references from config files inside the modules..., which I assume means eslint is for some reason not ignoring the modules folder as it should by default.
The only way to get it working is by individually listing the files in the root and subdirectories, like:
yarn run eslint controllers/** server.js app.js database/**
Doing this works, but is not a very clean solution. Is this behavior expected? Must I necessarily put everything inside a src folder as usually done?
Not sure how to eslint properly from root folder but in order to check all files at once, one option I could see is to put all the project files and folders (other than eslint.json, package*.json etc) in a new folder "src".
Post which project structure would be :-
root/
src/
controllers/
database/
app.js
server.js
.eslintrc.json
package.json
etc...
Then doing npx eslint src
I have a nodejs app with the following structure
app
-dir1
-dir2
-dir3
app.js
package.js
and for dir1 is an app of its own, also it will function as a package of the main app when ran from app/app.js
if i copy the entire app dir to another folder, navigate to app/dir1
it will contain
dir1
-pack1
-pack2
-pack3
-pack4
app_sub.js
package.json
and i have to run this app_sub.js
when i run this file module not found error is occurring
for the file in
/home/mypath/app/dir2/somefile.js
the error is occurring from outside the folder of the app.
can you guys help me in this matter?
Note:
in the directory where i copied the app if i run the npm install from app directory and then goto the app/dir1 and run node app_sub.js it is working fine
When developing two local projects in angularjs(where one imports the other) I would simply run "npm link" in module B's folder and then run "npm link module-B" in my main module's folder and whenever I changed a file in module B I would see it directly in my browser serving module A.
But it doesn't seem to be as easy with angular(4).
I use ng-packagr to generate a dist folder.
I run npm link inside the dist folder.
I run npm link module-B in my main module's folder.
I then run ng serve --preserve-symlinks.
So far so good, it can understand the Components of module B.
But if I try to change something in module B, rerun ng-packagr, my main module's "ng serve" fails to compile, I have to stop and start the ng serve.
I think ng-packagr first removes the dist folder and this triggers a rebuild in ng serve which fails and doesn't notice the newly created files that came after the deletion of the dist folder.
Do we have to use ng-packagr or is there some other way of doing multi-project-local-development.
Update:
If we comment out this section in ng-packagr.js it doesnt delete the folder and the browser updates whenever a file is changed and ngpackagr is run:
return Promise.all([
/*rimraf_1.rimraf(p.dest),*/
rimraf_1.rimraf(p.workingDirectory)
]);
But running ng-packagr takes some time depending on how big the library is. Since it builds the whole thing and not just files that are changed.
Ok I think I got it working. This solution feel much more straight forward and does not make use of ng-packagr. What I did was:
In module B I moved all my #angular-dependencies from dependencies to peerDependencies.
Added index.ts to root folder of module B containing:
export * from "./src/.../panel.module"
Run "npm link" from module B's root folder
Run "npm link module-B" from module A's root folder
Run ng serve --preserve-symlinks
The index.ts file does so that the import stays the same whether you take the module from an npm repository or developing locally:
import {moduleB} from "module-b";
I think this solution only works for Components that are meant to only be run in a "parent container", like a Project using the Component. If moduleB would have, lets say a demo-module/page, I think one would have to break it apart in two steps, first the demo module as a separate npm-project and the Component-module in a Child-npm-Project.
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.