I want to create a CLI for bootstrap my project. Everything works find but in order to copy my template files I use ncp where I give the template directory as a argument. So I tried,
import chalk from 'chalk';
import path from 'path';
import fs from 'fs';
import { promisify } from 'util';
const access = promisify(fs.access);
...
const templateDir = path.resolve(
new URL(import.meta.url).pathname,
'../../templates',
'project_name'
);
try {
await access(templateDir, fs.constants.R_OK);
} catch (err) {
console.error('%s Invalid template name', chalk.red.bold('ERROR'));
process.exit(1);
}
...
I got ERROR Invalid template name
Then I tried to figure out the problem,
//* after getting ERROR Invalid template name
//* checking file exist or not
fs.stat(templateDir, function (err, stat) {
if (err == null) {
console.log('File exists');
} else if (err.code === 'ENOENT') {
// file does not exist
console.log("file doesn't exit");
} else {
console.log('Some other error: ', err.code);
}
});
file doesn't exit
[Error: ENOENT: no such file or directory, access 'G:\G:\CLI%20project\create-project\templates\javascript'] {
errno: -4058,
code: 'ENOENT',
syscall: 'access',
path: 'G:\\G:\\CLI%20project\\create-project\\templates\\javascript'
}
ERROR Invalid template name
I expect the access should be G:\CLI%20project\create-project\templates\javascript
but it's not giving the that. where was the wrong happened? by the way I use esm module loader.
Give your errors a good read.
[Error: ENOENT: no such file or directory, access 'G:\G:\CLI%20project\create-project\templates\javascript'] {
errno: -4058,
code: 'ENOENT',
syscall: 'access',
path: 'G:\\G:\\CLI%20project\\create-project\\templates\\javascript'
}
They're not trying to trick you. They're just telling you/
G:\\G:\\
Error messages are there for a reason.
I had the same issue and found the answer:
const templateDir = path.resolve(
new URL(import.meta.url).pathname, // This is where it gives the error
'../../templates',
options.template
);
Change new URL (import.meta.url).pathname
to __filename
. It worked for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.