EPERM fs.rename - javascript

I am coming up with an error for this node script I wrote to rename a bunch of files.
> node test/rename.js
fs.js:809
return binding.rename(pathModule._makeLong(oldPath),
^
Error: EPERM: operation not permitted, rename '/path/to/file v2 032.png' -> '/path/to/file 032.png'
at Error (native)
at Object.fs.renameSync (fs.js:809:18)
at Object.<anonymous> (/Users/[user]/Documents/test/rename.js:9:8)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (bootstrap_node.js:352:7)
This is the code I am working with.
var fs = require('fs');
var path = require('path');
var folder = '/path/to/'
var regExp = /\sv2/gi;
var directory = fs.readdirSync(folder);
for (i=0; i < directory.length; i++) {
if (directory[i].match(regExp)) {
fs.renameSync(path.join(folder, directory[i]), path.join(folder, directory[i].replace(regExp, '')))
};
}
I tried it with as both node test/rename.js and sudo node test/rename.js with both throwing back errors. I don't see where I goofed up.

Solution: Files were locked within OSX's Get Info tab. I unlocked the files, re-ran the node script, and vola! It twerked and it worked.

Related

Can't use a class defined in an npm package

I am trying to use a typescript-parser:
https://www.npmjs.com/package/typescript-parser
but I'm having difficulties with using the package properly.
I installed the package successfully using npm install; when I run npm list --depth=0 it shows up in the list.
Eventually, I tried running the following line of code that is suggested in the link attached above (using node command):
const parser = new TypescriptParser();
but the problem is that I get the following error:
const parser = new TypescriptParser();
^
ReferenceError: TypescriptParser is not defined
at Object.<anonymous> (C:\Users\...\parser_example.js:3:16)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Alternatively, I tried running this:
const TypescriptParser = require('typescript-parser');
const parser = new TypescriptParser();
yet I still get a (different) error:
const parser = new TypescriptParser();
^
TypeError: TypescriptParser is not a constructor
at Object.<anonymous> (C:\Users\...\parser_example.js:3:16)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
It seems as if although the package was installed, it doesn't recognize the class TypescriptParser.
Any help would be much appreciated!
Try importing using:
import { TypescriptParser } from 'typescript-parser';
I tried the code below and it works for me :
index.js file
const parser = require('typescript-parser');
const parser = new TypescriptParser();
I run it with node index.js
This code works for me:
index.js file
const tp = require('typescript-parser');
const parser = new tp.TypescriptParser();

"SyntaxError: Unexpected token ' in JSON at position 0" when requiring a module using strict mode

In index.js I have
'use strict';
const config = require('./config');
In config.js I have
'use strict';
const config = new function() {
this.port = 3000;
this.redirectUri = "http://localhost:" + this.port + "/auth";
}
module.exports = config;
On x64 Windows running node v6.9.5 this runs fine.
On a Raspberry Pi Zero (Raspbian Pixel, ARM v6) running node 6.10.2 however, I get the following error:
module.js:590
throw err;
^
SyntaxError: /home/pi/pihas-api/config.json: Unexpected token ' in JSON at position 0
at Object.parse (native)
at Object.Module._extensions..json (module.js:587:27)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/pi/pihas-api/index.js:8:16)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
I know this is because I'm using 'use strict'; in config.js, but I'm wondering why it does work on Windows, and whether or not there's a way to get this to work on the Pi Zero as well.
JSON is not Javascript. You cannot "use strict"; in JSON.

plaid connect to client with node error

I'm using node backend and am trying to connect to plaid.
My code:
var plaid = require('plaid');
var clientID = "bla";
var secretID = "bla";
var publicKey = "bla";
var client = new plaid.Client(
clientID,
secretID,
publicKey,
plaid.environments.sandbox
);
However I am getting this error in my console:
throw new Error('Invalid Plaid environment');
^
Error: Invalid Plaid environment
at new Plaid.Client (C:\Users\me\Documents\found\SPA\node_modules\plaid\index.js:26:11)
at module.exports (C:\Users\me\Documents\found\SPA\app\routes.js:48:22)
at Object.<anonymous> (C:\Users\me\Documents\found\SPA\server.js:48:24)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
I have tried using each of these with no luck:
plaid.environments.sandbox
plaid.environments.development
plaid.environments.tartan
What's going on?
Ref: https://plaid.com/docs/quickstart/#accessing-item-data
UPDATE:
I've gotten this code below to work in another part of my app:
var plaidClient = new plaid.Client(clientID, secretID, plaid.environments.tartan);
However I am trying to use client.getTransactions() and it is required to have var plaidClient = new plaid.Client(clientID, secretID, publicID, plaid.environments.sandbox);
in order for client.getTransactions() to work.
UPDATE?
For some reason I had the wrong version of plaid installed. I needed to uninstall and reinstall to fix. Works now.

Requiring other files in node

I'm trying to require another file within a node project I'm working on; this will be a command line tool. What I'm trying to do is create a formatted color output using the following file format.js:
modules.exports = {
warning: function(input){
say("\033[31m" + input)
},
info: function(input){
say("\033[36m" + input)
}
}
From there I want to create the colored output and put it into a file named gen_email.js. That file has these two functions in it:
function say(input){
console.log(input)
}
function helpPage(){
say('');
format.info("test")
}
When I attempt to run this it outputs the following:
C:\Users\thomas_j_perkins\bin\javascript\node\email\lib\format.js:1
(function (exports, require, module, __filename, __dirname) { modules.exports = {
^
ReferenceError: modules is not defined
at Object.<anonymous> (C:\Users\thomas_j_perkins\bin\javascript\node\email\lib\for
mat.js:1:63)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Users\thomas_j_perkins\bin\javascript\node\email\gen_ema
il.js:3:16)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
I'm not understanding what I'm doing wrong, according to this I am requiring the file the correct way. What am I doing wrong here, do I need to move the say function into the other file?
It should be
module.exports = {
warning: function(input){
say("\033[31m" + input)
},
info: function(input){
say("\033[36m" + input)
}
}
in the other file
const format = require("whatEverPathIsOn/format.js")
if the file is under the same path just
const format = require("./format.js")
That should be module, not modules.

node.js cannot find 'neo4j' module (Thingdom)

After many tries, I am unnable to connect node.js to Neo4j installed in my computer. I am able to access both separately, and both work fine. I have install in my Node.js directory the Thingdom ('neo4j') module in the directory, but when require('neo4j') prints an error.
Image of my Node.js folder with Neo4j installed in modules
var neo4j = require("neo4j");
var db = new neo4j.GraphDatabase("http://localhost:7474");
var node = db.createNode({hello: 'world'}); // instantaneous, but...
node.save(function (err, node) { // ...this is what actually persists.
if (err) {
console.error('Error saving new node to database:', err);
} else {
console.log('Node saved to database with id:', node.id);
}
});
And when using in the cmd: "node index.js" it throws me this error:
C:\Users\RRamos\Documents\Projects\test-neo4j>node index.js
module.js:341
throw err;
^
Error: Cannot find module 'neo4j'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (C:\Users\RRamos\Documents\Projects\test-neo4j\index.js:1:75)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
I got the same problem. As there's no solution in this post, I just add mine.
After running $ npm init and $ npm install --save neo4j-driver, I copy and paste the neo4j example code in index.js:
var neo4j = require("neo4j");
var db = new neo4j.GraphDatabase('http://neo4j:<password>#localhost:7474');
And then I got the same error when running $ node index.js.
In my package.json, I found:
"dependencies": {
"neo4j-driver": "^1.1.0-M02"
}
It's neo4j-driver not neo4j. So replace it in index.js:
var neo4j = require("neo4j-driver");
var db = new neo4j.GraphDatabase('http://neo4j:<password>#localhost:7474');
Now you will get rid of the Cannot find module 'neo4j' error!
In addition, if you use the 1.1.0 version of neo4j-driver(for Neo4j 3.0.0+), you may get this error:
var db = new neo4j.GraphDatabase('http://neo4j:<password>#localhost:7474');
^
TypeError: neo4j.GraphDatabase is not a constructor
at Object.<anonymous> (D:\Codes\neo4j_test\server.js:2:10)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (bootstrap_node.js:352:7)
at startup (bootstrap_node.js:144:9)
at bootstrap_node.js:467:3
It seems neo4j.GraphDatabase is only available in older version of neo4j-driver.
Here's the up-to-date tutorial of neo4j-driver.
Use the following code instead:
var neo4j = require('neo4j-driver').v1;
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "<password>"));
// Create a session to run Cypher statements in.
// Note: Always make sure to close sessions when you are done using them!
var session = driver.session();
// Run a Cypher statement, reading the result in a streaming manner as records arrive:
session
.run("MERGE (alice:Person {name : {nameParam} }) RETURN alice.name", { nameParam:'Alice' })
.subscribe({
onNext: function(record) {
console.log(record._fields);
},
onCompleted: function() {
// Completed!
session.close();
},
onError: function(error) {
console.log(error);
}
});

Categories

Resources