Hi,
How to check if a file exists, using TypeScript?
So far I did:
Imported File System module via:
const fs = require("fs");
This gives error:
Module not found: Error: Can't resolve 'fs' in 'C:\Users\user\Documents\project\src'
Parsed request is a module
using description file: C:\Users\user\Documents\project\package.json (relative path: ./src)
Field 'browser' doesn't contain a valid alias configuration
resolve as module
Then found a possible solution. Added this to my webpack.config.js
resolve: {
fallback: {
"fs": false
},
},
After this, it runs, however I get an error where I use the fs. app.ts:180 Uncaught (in promise) TypeError: fs.existsSync is not a function
fs is NodeJS function, and it is not available in the browser. What do you want to achieve? Why do you need to check if the file exists in the browser?
I want to load and assign a texture. But I cannot be sure it exists.
var texture = new BABYLON.Texture("some/texture/path.jpg", scene) as BABYLON.Texture;
...
block.texture = texture;
I thought I could check if the file exists before attempting it to be casted as a Texture.
Checking if texture is null, does not work. Because even if path is empty, it creates something?
Okey, so first thing is that the Texture is trying to get the file via XHR (async) request. If it fails you will see the error in the console. So that is why nothing breaks (it’s async) and the app is still running.
Second thing is that you can use OnTextureLoadErrorObservable method to get notified if the request fail, like here:
I may be not the best person to ask this because I don’t really work with textures.
But IMO you can choose from two strategies:
assign the textures as you did before and observe on the errors - if there are errors - act. Let’s call it that we assume the positive case.
var texture = new BABYLON.Texture("some/texture/path.jpg", scene) as BABYLON.Texture;
...
block.texture = texture;
BABYLON.Texture.OnTextureLoadErrorObservable.add(e => {
// act here
console.log('err', e.name);
});
use onLoadObservable https://playground.babylonjs.com/#20OAV9#12910
But personally I don’t think it’s the way to go, because the observable is on the just instantiated texture itself. So you would need to check every texture you’re trying to use (instead of having one observable on the error as in the 1. option)
I never found a solution to this.
Though I did other mistake, I assumed I am checking something that I am not.
let blockInShader = cloneMat.getBlockByName(matchedBlockName) as BABYLON.TextureBlock;
I incorrectly assuming I should check for a texture: if (blockInShader.texture)
While I should have checked if the block was successfully casted to TextureBlock first.
So, the correct syntax should be:
let texture: BABYLON.Texture = new BABYLON.Texture(fullPath, scene, false, false) as BABYLON.Texture;
let matchedBlockName = "TextureBlock1");
let blockInShader = cloneMat.getBlockByName(matchedBlockName) as BABYLON.TextureBlock;
if (blockInShader?.texture) {
blockInShader.texture = texture;
}
This works and I no longer get nulls.
I have other question, which might be more simple.
How do I do that with Vector4/Color4 and float values?
Well, the initial question is still valid, - how to check if the file exists.
If it is not something I should do in the web based engine, TypeScript, then ok.