Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

node.js - gulp-4 concate multiple js files into one: Error Identifier has already been defined

I am using Nodejs- v12.9.0 & gulp-4.0.2

I want to concatenate & minify multiple js files from different src directories and bundle then into a single JS file. It does concatenate, minify & generates a single js file. While running this generated js file it gives below error because in almost all the js files we have this module const fs = require('fs').

const fs = require('fs');

Getting below error

SyntaxError: Identifier 'fs' has already been declared

gulp task:

function copyOtherJs(){
    return src(['main.js','app.js', 'src/*.js'])
        .pipe(terser({ parse: { bare_returns: false}, mangle: false }))//{ parse: { bare_returns: true } 
        .pipe(concat('srcJs.js'))
        .pipe(dest('dist/src'));
}

How to overcome this error while running my minified js file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Bundling is generally used for client-side files. There's no reason to bundle and concatenate server-side code, it won't make anything noticeably faster or easier to manage. The only good use case I can think of for trying to bundle your whole server into one file might be obfuscation, but that's not what it looks like you're doing. If on the other hand you have those declarations in client-side code, that also wouldn't work, because those modules don't exist in the browser.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...