Unable to use any Root path in Intellij only Absolute path

已回答

I am having trouble with IntelliJ recognizing file paths when the file is located outside of the src folder. This problem only occurs when the file path is not an absolute path, and it is causing me issues as I need to manually change all file paths. My project structure is as follows:

  • ProjectName
    • .idea
    • db
    • src
      • controllers dir
      • routers dir
      • database dir
      • index.js

For example, I cannot access the db folder from the src/database directory. I am experiencing this issue across multiple projects with assets located outside the src folder. Any assistance would be greatly appreciated. I have also tried ../ and / in front with no success.

The code I am trying, "new Database('db/data.sqlite')". I opened the project in VSC to see if the issue was there and it was not. I will happily record a video of the issue if required as I know it is not easy to replicate since many others in my class have no issues.

0

Hello, 541270. Could you please check if specifying the path to file from the  Project Name  directory helps, for example, ./db/data.sqlite?

0

Unfortunately it does not work. Some people have said about the working directory, and when I open the project structure and go to modules, the project folder name is the final part of the path. Below is the whole error, it does not appear when I use only the absolute path.

 

0

Looks like the working directory specified in your Node.js run configuration is wrong. Please make sure to set it to the same folder as you normally use when running your app in terminal

If this doesn't help, please share a project you have the issue with (including the .idea folder)

0

Sorry for the delayed reply. I am unsure how to configure the working directory of Node.js but I tried "process.chdri(C:\\Users\\5600X\\Desktop\\MTBHub_backend')" and it worked but that still requires a part of the absolute path. Here is the gitlab location as I am unsure the best way to share a project. Go to src > database > database-manager.js > line 47+48. Line 47 works but no matter what I have done Line 48 will not work. I appreciate the help.

0

You don't need doing anything special, a working directory is a folder the program is started in, unless it's explicitly changed in the program code

For example, if you start the application in the project root folder, by running node src/index.js, the working directory will be a project root folder. In this case, you need specifying a path relative to the project root directory in your database constructor:

db = new Database('./db/data.sqlite');

once you do this, the folder will be correctly resolved, now you can just start your app with your 

"start": "node src/index.js"

npm script

When using the Node.js run configuration, the path in the Working directory: field should be your project root folder, like

 

1

Thank you so much for the help, that has solved it!! I've usually just been running the 'Current File' option and clicking the run button along the top. Is it preferred to create custom run configurations?

 

0

By default, when running from file right-click menu, the working directory is set to current file directory. If you need it to be different, you have to modify the auto-created run configuration

 

So the answer is 'yes' - custom configuration is preferable if the defaults don't work for you for this or that reason:)

1

请先登录再写评论。