最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Renaming folder fails in node.js - Stack Overflow

programmeradmin1浏览0评论

I'm trying to rename a folder (a WordPress theme), after some search-replacing using a node script, but the renaming of the folder seems to fail.

I want for this

public_html/wp-content/my_theme/

to bee

public_html/wp-content/something_other/

Where the name of the folder is taken from the prompt (this part works, since search-replace within the files works fine).

The script looks like this

const fs = require('fs');
const path = require('path');
const rootDir = path.join(__dirname, '..');
// themePackageName is taken from the prompt and is defined

if (themePackageName !== 'my_theme') {
    fs.renameSync(`${rootDir}/wp-content/my_theme/`, `${rootDir}/wp-content/${themePackageName}/`, (err) => {
      if (err) {
        throw err;
      }
      fs.statSync(`${rootDir}/wp-content/${themePackageName}/`, (error, stats) => {
        if (error) {
          throw error;
        }
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
  }

Which was basically taken from here

The error I'm getting is

fs.js:781
  return binding.rename(pathModule.toNamespacedPath(oldPath),
                 ^

Error: ENOENT: no such file or directory, rename '/vagrant-local/www/me/wp-boilerplate/public_html/wp-content/my_theme/' -> '/vagrant-local/www/me/wp-boilerplate/public_html/wp-content/aws-theme/'
    at Object.fs.renameSync (fs.js:781:18)
    at Object.<anonymous> (/vagrant-local/www/me/wp-boilerplate/public_html/bin/rename.js:163:8)
    at Module._pile (internal/modules/cjs/loader.js:654:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
    at Module.load (internal/modules/cjs/loader.js:566:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
    at Function.Module._load (internal/modules/cjs/loader.js:498:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:695:10)
    at startup (internal/bootstrap/node.js:201:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:516:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] rename: `./bin/rename.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] rename script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A plete log of this run can be found in:
npm ERR!     /.npm/_logs/2018-05-06T10_06_25_961Z-debug.log

And the debug.log is

0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/Cellar/node/9.11.1/bin/node',
1 verbose cli   '/usr/local/bin/npm',
1 verbose cli   'run',
1 verbose cli   'rename' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prerename', 'rename', 'postrename' ]
5 info lifecycle [email protected]~prerename: [email protected]
6 info lifecycle [email protected]~rename: [email protected]
7 verbose lifecycle [email protected]~rename: unsafe-perm in lifecycle true
8 verbose lifecycle [email protected]~rename: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/my_user/vagrant-local/www/me/wp-boilerplate/public_html/node_modules/.bin:/usr/local/sbin:/Users/my_user/wpcs/vendor/bin:/Users/my_user/.rbenv/shims:/Users/my_user/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin
9 verbose lifecycle [email protected]~rename: CWD: /Users/my_user/vagrant-local/www/me/wp-boilerplate/public_html
10 silly lifecycle [email protected]~rename: Args: [ '-c', './bin/rename.js' ]
11 silly lifecycle [email protected]~rename: Returned: code: 1  signal: null
12 info lifecycle [email protected]~rename: Failed to exec rename script
13 verbose stack Error: [email protected] rename: `./bin/rename.js`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:285:16)
13 verbose stack     at EventEmitter.emit (events.js:180:13)
13 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:180:13)
13 verbose stack     at maybeClose (internal/child_process.js:936:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:220:5)
14 verbose pkgid [email protected]
15 verbose cwd /Users/my_user/vagrant-local/www/me/wp-boilerplate/public_html
16 verbose Darwin 17.5.0
17 verbose argv "/usr/local/Cellar/node/9.11.1/bin/node" "/usr/local/bin/npm" "run" "rename"
18 verbose node v9.11.1
19 verbose npm  v5.6.0
20 error code ELIFECYCLE
21 error errno 1
22 error [email protected] rename: `./bin/rename.js`
22 error Exit status 1
23 error Failed at the [email protected] rename script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

What am I doing wrong?

I'm trying to rename a folder (a WordPress theme), after some search-replacing using a node script, but the renaming of the folder seems to fail.

I want for this

public_html/wp-content/my_theme/

to bee

public_html/wp-content/something_other/

Where the name of the folder is taken from the prompt (this part works, since search-replace within the files works fine).

The script looks like this

const fs = require('fs');
const path = require('path');
const rootDir = path.join(__dirname, '..');
// themePackageName is taken from the prompt and is defined

if (themePackageName !== 'my_theme') {
    fs.renameSync(`${rootDir}/wp-content/my_theme/`, `${rootDir}/wp-content/${themePackageName}/`, (err) => {
      if (err) {
        throw err;
      }
      fs.statSync(`${rootDir}/wp-content/${themePackageName}/`, (error, stats) => {
        if (error) {
          throw error;
        }
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
  }

Which was basically taken from here

The error I'm getting is

fs.js:781
  return binding.rename(pathModule.toNamespacedPath(oldPath),
                 ^

Error: ENOENT: no such file or directory, rename '/vagrant-local/www/me/wp-boilerplate/public_html/wp-content/my_theme/' -> '/vagrant-local/www/me/wp-boilerplate/public_html/wp-content/aws-theme/'
    at Object.fs.renameSync (fs.js:781:18)
    at Object.<anonymous> (/vagrant-local/www/me/wp-boilerplate/public_html/bin/rename.js:163:8)
    at Module._pile (internal/modules/cjs/loader.js:654:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
    at Module.load (internal/modules/cjs/loader.js:566:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
    at Function.Module._load (internal/modules/cjs/loader.js:498:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:695:10)
    at startup (internal/bootstrap/node.js:201:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:516:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] rename: `./bin/rename.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] rename script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A plete log of this run can be found in:
npm ERR!     /.npm/_logs/2018-05-06T10_06_25_961Z-debug.log

And the debug.log is

0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/Cellar/node/9.11.1/bin/node',
1 verbose cli   '/usr/local/bin/npm',
1 verbose cli   'run',
1 verbose cli   'rename' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prerename', 'rename', 'postrename' ]
5 info lifecycle [email protected]~prerename: [email protected]
6 info lifecycle [email protected]~rename: [email protected]
7 verbose lifecycle [email protected]~rename: unsafe-perm in lifecycle true
8 verbose lifecycle [email protected]~rename: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/my_user/vagrant-local/www/me/wp-boilerplate/public_html/node_modules/.bin:/usr/local/sbin:/Users/my_user/wpcs/vendor/bin:/Users/my_user/.rbenv/shims:/Users/my_user/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin
9 verbose lifecycle [email protected]~rename: CWD: /Users/my_user/vagrant-local/www/me/wp-boilerplate/public_html
10 silly lifecycle [email protected]~rename: Args: [ '-c', './bin/rename.js' ]
11 silly lifecycle [email protected]~rename: Returned: code: 1  signal: null
12 info lifecycle [email protected]~rename: Failed to exec rename script
13 verbose stack Error: [email protected] rename: `./bin/rename.js`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:285:16)
13 verbose stack     at EventEmitter.emit (events.js:180:13)
13 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:180:13)
13 verbose stack     at maybeClose (internal/child_process.js:936:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:220:5)
14 verbose pkgid [email protected]
15 verbose cwd /Users/my_user/vagrant-local/www/me/wp-boilerplate/public_html
16 verbose Darwin 17.5.0
17 verbose argv "/usr/local/Cellar/node/9.11.1/bin/node" "/usr/local/bin/npm" "run" "rename"
18 verbose node v9.11.1
19 verbose npm  v5.6.0
20 error code ELIFECYCLE
21 error errno 1
22 error [email protected] rename: `./bin/rename.js`
22 error Exit status 1
23 error Failed at the [email protected] rename script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

What am I doing wrong?

Share Improve this question asked May 6, 2018 at 10:15 dingo_ddingo_d 11.7k12 gold badges82 silver badges136 bronze badges 7
  • 2 Are you sure the file exists? Does this change when you use the account that owns the directory, you want to rename – Luca Kiebel Commented May 6, 2018 at 10:20
  • Well it should exist, but I'm not explicitly checking for it tbh :/ The script should have privileges to make changes, because it changes strings inside files just fine – dingo_d Commented May 6, 2018 at 10:23
  • 1 Oh I think I just realized that my path is not ${rootDir}/wp-content/my_theme/ but ${rootDir}/wp-content/themes/my_theme/ -_-" – dingo_d Commented May 6, 2018 at 10:25
  • I think your're in the wrong place. You have /vagrant-local/www/me/ on the attempted rename, yet CWD: /Users/my_user/vagrant-local/www/me/ in your earlier output. – Neil Lunn Commented May 6, 2018 at 10:26
  • 1 Did that! ;) ... – Luca Kiebel Commented May 6, 2018 at 10:37
 |  Show 2 more ments

2 Answers 2

Reset to default 3

The error you are getting:

Error: ENOENT: no such file or directory, rename '/vagrant-local/www/me/wp-boilerplate/public_html/wp-content/my_theme/' -> '/vagrant-local/www/me/wp-boilerplate/public_html/wp-content/aws-theme/'

Says, that the directory you are trying to rename does not exist.

Before renaming a directory, you should check, whether or not it exists in the first place, use fs.existsSync():

const fs = require('fs');
const path = require('path');
const rootDir = path.join(__dirname, '..');
// themePackageName is taken from the prompt and is defined

if (themePackageName !== 'my_theme' && fs.existsSync(`${rootDir}/wp-content/my_theme/`)) {
    fs.renameSync(`${rootDir}/wp-content/my_theme/`, `${rootDir}/wp-content/${themePackageName}/`, (err) => {
      if (err) {
        throw err;
      }
      fs.statSync(`${rootDir}/wp-content/${themePackageName}/`, (error, stats) => {
        if (error) {
          throw error;
        }
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
  }

ESM version of @Luca's answer:

//  rename folder
import { renameSync, mkdirSync, existsSync } from 'node:fs';
import { resolve } from 'node:path';
export const renameFolder = (oldPath, newPath, createPathIfNeeded= true) => {
   try {
     if (!existsSync(resolve(newPath)) && createPathIfNeeded) {
        mkdirSync(resolve(newPath), { recursive: true });
     }
     renameSync(resolve(oldPath), resolve(newPath));
   } catch (e) {
    console.error(e);
   }
};
发布评论

评论列表(0)

  1. 暂无评论