Is there a way to have intellisense in VSC work with json files imported via the require method in nodeJS?
var jsonObj = require('path/to/jsonFile.json')
I'd like for it to predict the properties of the json object imported from the file as I code.
Not possible in all instances, but when I'm creating JSON for configurations or other static data that could have been stored in a JSON file, I store it in a .js file and use the module.exports to expose it. This way the Intellisense picks it up and you get the auto-complete in VSC.
module.exports = {"data":{"foo":"bar"}}
Related
Is there a way to load a javascript file without require, but with fs for example?
I know that for json files I can do:
const jsonFile = JSON.Parse(fs.readFileSync("./jsonfile.json")
Is there a way to do the same for a javascript file?
The reason I am asking is because, i have a configuration file that I kinda want to detach from my app, because when I bundle it with webpack, I cant exclude the config file.
I have tries to directly read it, but I just got some raw format in a buffer:
const jsFile = fs.readFileSync("./jsFile.js")
EDIT ANSWER: I was able to do it by utilizing readFileSync and eval:
const jsFile = eval(fs.readFileSync("./jsFile.js", "utf-8"));
Keep in mind that if you are requiring other files within that file, the paths change! They will NOT be relative to jsFile.js, but relative to the file which reads the jsFile.js
I was able to do it by utilizing readFileSync and eval:
const jsFile = eval(fs.readFileSync("./jsFile.js", "utf-8"));
Keep in mind that if you are requiring other files within that file, the paths change! They will NOT be relative to jsFile.js, but relative to the file which reads the jsFile.js
I've got a project which has an "old" javascript/php app. It contains dozen of json files for getting data from the database.
Besides that I'm going to migrate some modules to react but still want to use those json files for now. Is it possible to fetch the json files from the react app? Both apps are in the same repository.
Lets pretend you have this folder structure:
/
/LegacyApp
/LegacyApp/file.json
/NewApp/index.js
Then you can simply import the files from your new app via the require function:
// inside index.js
const data = require('../LegacyApp/file.json')
// Or newer ES6 Syntax
import data from '../LegacyApp/file.json'
That JSON-file will be imported as a JavaScript object.
I'm working on a express API and i want to connect to a mysql server with this api. Settings are stored in a settings.json file. I read this using
const config = JSON.parse(require(`fs`).readFileSync('../../settings.json'));
This works if the json file is in the same directory. But in this case, the settings file is in the base directory(./settings.json) but the file from where i want it to access is: ./modules/sql/mysql.js. It doesnt work:
Error: ENOENT: no such file or directory, open '../../settings.json'
Is there a better way to access/read a json file? or what am i doing wrong?
Btw. i dont want to pass the settings as a variable. I already tried it but if possible - i want to avoid it.
File/Directory strcture
did you not miss a ../ there?
try replace '../../settings.json' for '../../../settings.json'
btw, its better to use the __dirname approach, but it should work just fine without that too :)
How i can use a ts file in java script file.
I have an angular project in that i have a constant file which is environment.ts file and data is as below
export const Environment = {
PRODUCTION: false
};
and i have used one external java script library and in that library i want to access this environment.ts file i.e want to use constant.
i have tried with
var en = require('./environment.ts');
But not able to access it.
The problem probably comes from the fact that TS is compiled into JS during the build process.
Since you're just exporting the single object, it doesn't really need to be a typescript file.
Try to rename the file into 'environment.js', it shouldn't break anything and it will allow you to require it in other js files.
Try this,
import { Environment } from './environment';
Now we can access the properties like Environment.PRODUCTION
What I did is created dev.json and prod.json with my configurations.
I then imported dev.json into environment.ts and prod.json into environment.prod.ts for exporting to ts files.
I also imported dev.json into an environment.js and environment.prod.js for exporting to js files. You could also use the json directly if you want, but this will give you the flexibility to manipulate in js if you want later.
Now to update configs you just keep updating the json files.
Make sure your external js knows which env configs to read depending on how you're building/compiling it.
Is it possible to have the same config file for server node.js and client js code?
Now I use .json config for node.js, but can convert it to any format
Without more information, all I can offer is a simple suggestion: Require.js. You can reference a config module in the Node.js code and reference it in the client script. Just make sure that the config file can be served to the client.
// Example reference
var config = require('./data/config.js');