10 examples of 'js read file' in JavaScript

Every line of 'js read file' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
26function read (file) {
27 return fs.readFileSync(file, {encoding: 'utf-8'})
28}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

17function read(file) {
18 var finished;
19 var error;
20 var value;
21 var cb;
22
23 fs.readFile(file, 'utf8', function(err, str){
24 if (cb) {
25 cb(err, str);
26 } else {
27 finished = true;
28 error = err;
29 value = str;
30 }
31 });
32
33 return function(fn){
34 if (finished) {
35 fn(error, value);
36 } else {
37 cb = fn;
38 }
39 }
40}
18function _readJs(labelAndPath, cb)
19{
20 fs.readFile(config.filePath(labelAndPath), 'utf8', function(err, src) {
21 if ( err )
22 return void cb(err);
23
24 cb(null,{label: config.fileLabel(labelAndPath), src: src});
25 });
26}
8function read(file) {
9 return fs.readFileSync(path.join(__dirname, file + '.js'), 'utf8');
10}
66private static async _read(file: string): Promise<string> {
67 return await new Promise<string>((resolve, reject) => {
68 fs.readFile(file, "utf8", (err, data) => {
69 if (err) reject(err);
70
71 resolve(data);
72 });
73 });
74}
89function read(filename){
90 return fs.readFileSync(filename, { encoding: 'utf8' });
91}
37function js(file) {
38 var opts = {
39 fromString: true,
40 sourceMapUrl: false
41 };
42
43 if (file.map) {
44 opts.inSourceMap = _.isObject(file.map) ? file.map : JSON.parse(file.map);
45 opts.outSourceMap = file.destFilename + '.map';
46 }
47
48 var result = uglifyjs.minify(file.content, opts);
49
50 return Promise.resolve({ content: result.code, map: result.map });
51}
5function loadJS(libs) {
6 libs = libs.split('|');
7 for (var i=libs.length; i--;) {
8 document.write("<script src='/~/"+libs[i]+".js'></script>");
9 }
10}
5function loadJs({
6 url,
7 options = {}
8}) {
9 return new Promise((resolve, reject) => {
10 if (!url) {
11 reject(new Error('NO URL'))
12 }
13 const timeout = options.timeout || 60000
14 const target = document.getElementsByTagName('head')[0] || document.head
15 let timer; let
16 script
17 // 超时自动取消
18 if (timeout) {
19 timer = setTimeout(() => {
20 // eslint-disable-next-line no-use-before-define
21 cleanup()
22 reject(new Error('Timeout'))
23 }, timeout)
24 }
25 // 清理函数
26 function cleanup() {
27 if (script.parentNode) {
28 script.parentNode.removeChild(script)
29 }
30 if (timer) {
31 clearTimeout(timer)
32 }
33 }
34 // 创建js文件
35 script = document.createElement('script')
36 script.type = 'text/javascript'
37 script.charset = 'utf-8'
38 script.async = true
39 script.src = url
40 target.appendChild(script)
41 // IE的script 元素支持onreadystatechange事件,不支持onload事件 FF的script
42 // 元素不支持onreadystatechange事件,只支持onload事件
43 // eslint-disable-next-line no-multi-assign
44 script.onload = script.onreadystatechange = function () {
45 if (!script.readyState || script.readyState === 'loaded' || script.readyState === 'complete') {
46 resolve()
47 if (timer) {
48 clearTimeout(timer)
49 }
50 }
51 }
52 })
53}
35function readFileData(src) {
36 try { let _content = fs.readFileSync(src, { encoding: 'utf8' }); return _content; }
37 catch(error) {
38 throw new Error('Could not read file: ' + JSON.stringify(error));
39 }
40}

Related snippets