I have a yaml file and i want it to be read by my browser website. When I run my function in browser console, i get below error for my variable yaml
TypeError: yaml is undefined
Whereas i have declare it inside function. PFB my function
function parseyam() {
var yaml = require('js-yaml');
var fs = require('fs');
// Get document, or throw exception on error
const doc = yaml.safeLoad(fs.readFileSync('services.yml', 'utf8'));
console.log(doc);
}
I also added require.js in my index.html and tried to work around with require.js. I think the variable yaml is not working fine because of require.
Please advise, how do i make it work?
Note: When i run same code in backend, it works fine and i get data.
Is there any other way i can make it work on front end? Any other alternatives? Would be thankful if anyone could help me here.
The problem here is that NodeJS uses CommonJS module format,but the RequireJS uses AMD module format. They not compatible. There are some possible solutions to fix that, please go ahead to RequireJS site to read more: https://requirejs.org/docs/commonjs.html
Related
In JavaScript project, I want to decompress an object compressed by zlib(Zopfli.js) and I'm trying it with pako.min.js.
However, the example at the official site of pako uses require function which does not exist in JavaScript. Maybe Node.js has this but I'm afraid it would take a lot of time and pains to combine this JavaScript project with Node.js, because I know nothing about Node.js.
Is it any way to get through with this, or another way to decompress the object?
Any information would be appreciated.
What I've already tried
I've already tried zlib.js library for decomressing, but the result is catching the error below which I couldn't find any solution:
const compressed = dataCompressedByZlib;
const inflate = new Zlib.Inflate(compressed);
const plain = inflate.decompress();// -> input buffer is broken
You may use pako.js from here for client side javascript and import it as -
<script type="text/javascript" src="pako.js"></script>
inside your html, as mentioned here-
https://stackoverflow.com/a/22675078/7895283
I have a file in #/assets called data.txt. Apparently, I'm unable to import Data from "#/assets/data.txt" and then create a function
computed: {
readFile: function(){
let reader = new FileReader();
reader.readAsText(Data)
return reader.result
}
}
because I can only import json and not txt without installing Webpack, and then configuring raw-loader, which seems like a massively complicated endeavor to read a simple text file. I also can't explicitly write reader.readAsText("./src/assets/data.txt") because I get an error of TypeError: Failed to execute 'readAsText' on 'FileReader': parameter 1 is not of type 'Blob'.
I know that I can circumvent this problem by simply making the text file into a json file and adding the line export const data="...", but, again, it seems like a rather circuitous solution to simply read a txt file.
(And I am aware of https://stackoverflow.com/a/54703169/376936, which is confusing to me, because I don't have a vue.config.js, which may be an entirely different problem, but it still doesn't explain why Vue can't read a simple text file without multiple steps. It seems like it's the very simple actions in Vue that cause the most confusion.)
Am I missing something very simple and basic?
I refactored some code that passed on an object as a require to use it from a feature file and now I get the error on the title.
Previously:
const schema = require (´path to schema´)
let schemafile = [schema]
Now:
const schemafile = (fs.readFileSync(__dirname+'path to schema'+schema+.js)toString())
the above passed the contents of the joi schema file I prepared, but when i do:
joi.assert (responsebody, schemafile)
or
joi.attempt(responsebody, schemafile)
I get the error
TyperError: schema.validate is not a function
I do have the require for joi, and it seems everything passes fine and the error seems to be internal to joi, as the expression does exist there. What I can't make up is why it used to work before. Rolling back is not an option because I modified many, many files before I got this to hit that part of the code.
Sorry if the code is too vague but I cannot share much more (can't even copypaste from the VDI I use).
I use joi's latest version, I know joi.validate was deprecated and it is not used anywhere in my code.
Any help is appreciated.
Thanks!
PS: I am new to js, node and joi so sorry if the question is too basic.
I have an array and I want to create a .json to store the array in it. This is what I have, but i receive require is not defined. I know it has something to do with NodeJS, but I dont know what I should do.
let answersString = JSON.stringify(answersArray);
const fs = require('fs');
fs.writeFileSync("answers.json", answersString);
Thanks!
EDIT: Now I know this was a pretty dumb question, sorry. In the meantime I learned about node, bundling, testing etc.
You're using the code in client, and require() does not exists in javascript browser-side (nodeJS is server side...).
My proposal would be to send file to server side where you will have fs and do the work there.
Check this answer
You still can use require at the client side but you need to use a bundler. The notorious ones are Webpack and Browserify.
As found on the official documentation IE11 does not support FormData at all or at least not much enough I need to.
Inside my code I have to loop through the entries of an FormData element. For this task I use the entries() function which is not working on IE giving me the error
Object doesn't support property or method 'entries'
I already added https://github.com/jimmywarting/FormData via npm to my project and added it to my entry in webpack.config.js which then looked something like this.
const webpack = require("webpack");
const path = require("path");
module.exports = {
entry: {
app: ["babel-polyfill", "formdata-polyfill", "whatwg-fetch", "./Scripts/Modules/index.ts"]
},
}
In the console I can see that formdata.min.js is loaded. As the error still occurs it seems like it does not use the polyfill as it should. It still uses the default one which fails.
How can I tell my TypeScript code to use the polyfill-version instead of the browser's default implementation as the package does not provied any d.ts files?
Is this even possible? If not - what are possible workarounds for such a scenario?
This is what the FormData object is looking like in the dev-tools:
which only has append as method defined
Not sure if it may helps you but I got this issue today and it seems that the version of the polyfill provided via NPM does not completely override the FormData object, so is not suitable to IE11 or Edge.
I found switching to the V3 of the library fixed that. In order to do that I got the content of the FormData.js file from the github repository and added it as a component in my own custom JavaScript code.
I use WebPack to, got some other issues with IE11, still investigating.
Let me know how it goes by including the content directly instead of getting it from NPM.