javascriptnode.jsfs

Renaming folder fails in node.js


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 become

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._compile (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! my_theme@1.0.0 rename: `./bin/rename.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the my_theme@1.0.0 rename script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete 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 npm@5.6.0
3 info using node@v9.11.1
4 verbose run-script [ 'prerename', 'rename', 'postrename' ]
5 info lifecycle my_theme@1.0.0~prerename: my_theme@1.0.0
6 info lifecycle my_theme@1.0.0~rename: my_theme@1.0.0
7 verbose lifecycle my_theme@1.0.0~rename: unsafe-perm in lifecycle true
8 verbose lifecycle my_theme@1.0.0~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 my_theme@1.0.0~rename: CWD: /Users/my_user/vagrant-local/www/me/wp-boilerplate/public_html
10 silly lifecycle my_theme@1.0.0~rename: Args: [ '-c', './bin/rename.js' ]
11 silly lifecycle my_theme@1.0.0~rename: Returned: code: 1  signal: null
12 info lifecycle my_theme@1.0.0~rename: Failed to exec rename script
13 verbose stack Error: my_theme@1.0.0 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 my_theme@1.0.0
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 my_theme@1.0.0 rename: `./bin/rename.js`
22 error Exit status 1
23 error Failed at the my_theme@1.0.0 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?


Solution

  • 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)}`);
          });
        });
      }