首页 > 解决方案 > Node.js Concat 反斜杠问题

问题描述

所以我目前正在为一个 Angular 项目设置微前端。在尝试创建要在不同项目之间共享的 polyfill、es5 脚本等包时,由于某种原因,正在创建的目录有反斜杠,导致找不到路径。如果这些是正斜杠,他们会工作得很好,想法?

const fs = require('fs-extra');
const concat = require('concat');

(async function build(){
const prgName = process.argv.slice(2)[0];
if(prgName === '' || prgName === undefined){
console.log("Project name is required as an argument");
}else{
const files_es2015 = [
    './dist/' + prgName + '/polyfill-webcomp-es5.js',
    './dist/' + prgName + '/polyfill-webcomp.js',
    './dist/' + prgName + '/polyfill-es2015.js',
    './dist/' + prgName + '/scripts.js',
    './dist/' + prgName + '/main-es2015.js',
]
await fs.ensureDir('./dist/' + prgName + '/elements');
await concat(files_es2015,'./dist/' + prgName + '/elements/' + prgName + '-elements-es2015.js');

const files_es5 = [
    './dist/' + prgName + '/polyfill-webcomp-es5.js',
    './dist/' + prgName + '/polyfill-webcomp.js',
    './dist/' + prgName + '/polyfill-es2015.js',
    './dist/' + prgName + '/scripts.js',
    './dist/' + prgName + '/main-es5.js',
]
await fs.ensureDir('./dist/' + prgName + '/elements');
await concat(files_es2015,'./dist/' + prgName + '/elements/' + prgName + '-elements-es5.js');

console.log('Done generating bundles for ' + prgName);
}
})()

这将在尝试创建构建时输出以下消息

    (node:26344) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open 'C:\Code\Project\Code Testing\project-UI-Workspace\dist\classroomApp\polyfill-es2015.js'
(Use `node --trace-warnings ...` to show where the warning was created)
(node:26344) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:26344) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

文件夹设置,从 project-UI-workspace 文件夹中查看。这是在运行脚本之后,因此您可以看到元素文件夹已创建,但由于路径错误而没有任何内容。

在此处输入图像描述

标签: javascriptnode.jsconcatenation

解决方案


  • 当我尝试将多个 javascript 文件合并到一个文件时,我遇到了同样的问题。通过用双反斜杠替换单反斜杠来修复它。

例如:如果路径是:C:\Users\samplecode.js将其更改为C:\\Users\\samplecode.js


推荐阅读