0
votes

I have a node script which needs to create a file in the root directory of my application before it builds the file.

The data this file will contain is specific to each build that gets triggered, however, I'm having no luck on Azure DevOps in this regards.

For the writing of the file I'm using fs.writeFile(...), something similar to this:

fs.writeFile(targetPath, file, function(err) { // removed for brevity });

however, this throws an expection:

[Error: ENOENT: no such file or directory, open '/home/vsts/work/1/s/data-file.json']

Locally this works, I'm assuming this has got to do with permissions, however, I tried adding a blank version of this file to my project, however, it still throws this exception.

1
How do you run this file? What task do you use? Can you paste here your yaml file?Krzysztof Madej
Hi friend, is there any update for this issue? Feel free to let me know if you're still blocked by this issue. Just a reminder of this :)LoLance

1 Answers

1
votes

Possible to create file in sources directory on Azure DevOps during build

The answer is Yes. This is fully supported scenario in Azure Devops Service if you're using Microsoft ubuntu-hosted agent.

If you met this issue when using microsoft-hosted agent, I think this issue is more related to one path issue. Please check:

  1. The function where the error no such file or directory comes. Apart from the fs.writeFile function, do you also use fs.readFile in the xx.js file? If so, you should make sure the two paths are same.

  2. The structure of your source files and your real requirements. According to your question you want to create it in Source directory /home/vsts/work/1/s, but the first line indicates that you actually want to create file in root directory of my application.

    1).If you want to create file in source directory /home/vsts/work/1/s:

    In your js file: Use something targetpath like './data-file.json'. And make sure you're running command node xx.js from source directory. (Leaving CMD task/PS task/Bash task's working directory blank!!!)

    2).If you want to do that in root of application folder like /home/vsts/work/1/s/MyApp:

    In your js file: Use __dirname like fs.writeFile(__dirname + '/data-file.json', file, function(err) { // removed for brevity }); and fs.readFile(__dirname + '/data-file.json',...).