所以我目前正在为 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文件夹查看。这是运行脚本之后的结果,因此您可以看到 elements 文件夹被创建,但由于路径错误,没有任何内容进入。
最佳答案
- 当我尝试将多个 javascript 文件合并到一个文件中时,我遇到了同样的问题。通过用双反斜杠替换单反斜杠修复了这个问题。
例如:如果路径是:C:\Users\samplecode.js
将其更改为 C:\\Users\\samplecode.js
关于javascript - Node.js 连接反斜杠问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66649488/