Write file in home directory when running `npm install` - json

When install a foo module, as developer, I want to write ~/.foo.json file (where ~/ is the user's home directory).
For this I did in package.json:
{
...
"scripts": {
"preinstall": "./installation/preinstall.js"
},
...
}
And in /installation/preinstall.js (that is executable), I have:
#!/usr/bin/env node
// Dependencies
var Fs = require("fs");
function getUserHome() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}
console.log("Creating configuration file ...")
Fs.writeFileSync(getUserHome() + "/" + ".foo.json", JSON.stringify(
require("./sample-config"), null, 4
));
When running sudo npm install ...#... -g I get the following output:
ionicabizau#laptop:~$ sudo npm install ...#...-alpha1 -g
npm http GET https://registry.npmjs.org/...
npm http 304 https://registry.npmjs.org/...
> ...#...-alpha1 preinstall /usr/lib/node_modules/...
> ./installation/preinstall.js
Creating configuration file ...
fs.js:432
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: EACCES, permission denied '/home/ionicabizau/.foo.json'
at Object.fs.openSync (fs.js:432:18)
at Object.fs.writeFileSync (fs.js:971:15)
at Object.<anonymous> (/usr/lib/node_modules/.../installation/preinstall.js:11:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
Why EACCESS error, even if I run it with sudo?
Running Ubuntu 14.04, if it's relevant.

According to documentation, If npm was invoked with root privileges, then it will change the uid to the user account or uid specified by the user config, which defaults to nobody. Set the unsafe-perm flag to run scripts with root privileges.
So, that's the problem here. The environment variables HOME or USERPROFILE remain the same, but the user is nobody.
However, --unsafe-perm prevents this:
sudo npm install foo -g --unsafe-perm

Related

getting error while doing minification of css files with gulp

Please advise i want to minify the .css files of my project with gulp so i have written the below task in my gulp.js file as shown below but still upon execution i am getting the error , please advise how to fix the below error
below is the code that I have written is
var gulp = require('gulp');
var minifyCss = require("gulp-minify-css");
gulp.task('minify-css', function () {
gulp.src('C:\\Softwares\\Spring4MVCAngularJSExample\\target\\Spring4MVCAngularJSExample\\static\\css\\*.css') // path to your file
.pipe(minifyCss())
.pipe(gulp.dest('C:\\Softwares\\Spring4MVCAngularJSExample\\target\\Spring4MVCAngularJSExample\\static\\cssoutput'));
});
below is the error i am getting upon execution
C:\Softwares\Spring4MVCAngularJSExample>gulp minify-css
module.js:471
throw err;
^
Error: Cannot find module 'gulp-minify-css'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Softwares\Spring4MVCAngularJSExample\gulpfile.js:10:13)
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)
It appears that it cannot locate the module. Have you run npm install gulp-minify-css --save-dev?
You may want to try re-installing your local packages / dependencies:
rm -rf node_modules/
npm install

yo polymer building error

I have just updated my Polymer development tools and I get some errors, although everything was working fine before. Here is what I get when trying 'yo polymer' in the console :
/usr/lib/node_modules/generator-polymer/node_modules/yeoman-generator/lib/base.js:444
this.env.sharedFs.on('change', writeFiles);
^
TypeError: Object # has no method 'on'
at run (/usr/lib/node_modules/generator-polymer/node_modules/yeoman-generator/lib/base.js:444:21)
at Environment.run (/usr/lib/node_modules/yo/node_modules/yeoman-environment/lib/environment.js:330:20)
at /usr/lib/node_modules/yo/lib/cli.js:91:9
at Environment.resolver.lookup (/usr/lib/node_modules/yo/node_modules/yeoman-environment/lib/resolver.js:50:12)
at init (/usr/lib/node_modules/yo/lib/cli.js:66:7)
at pre (/usr/lib/node_modules/yo/lib/cli.js:53:3)
at Object. (/usr/lib/node_modules/yo/lib/cli.js:155:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
Or when I type 'yo' in the console and choose 'polymer' :
/usr/lib/node_modules/yo/node_modules/inquirer/node_modules/rx/dist/rx.all.js:9379
throw e;
^
TypeError: Object # has no method 'on'
at run (/usr/lib/node_modules/generator-polymer/node_modules/yeoman-generator/lib/base.js:444:21)
at Environment.run (/usr/lib/node_modules/yo/node_modules/yeoman-environment/lib/environment.js:330:20)
at module.exports (/usr/lib/node_modules/yo/lib/routes/run.js:20:11)
at Router.navigate (/usr/lib/node_modules/yo/lib/router.js:36:30)
at PromptUI.completed (/usr/lib/node_modules/yo/lib/routes/home.js:76:20)
at PromptUI.onCompletion (/usr/lib/node_modules/yo/node_modules/inquirer/lib/ui/prompt.js:69:10)
at AnonymousObserver.Rx.AnonymousObserver.AnonymousObserver.completed (/usr/lib/node_modules/yo/node_modules/inquirer/node_modules/rx/dist/rx.all.js:1965:12)
at AnonymousObserver.Rx.internals.AbstractObserver.AbstractObserver.onCompleted (/usr/lib/node_modules/yo/node_modules/inquirer/node_modules/rx/dist/rx.all.js:1902:14)
at AutoDetachObserverPrototype.completed (/usr/lib/node_modules/yo/node_modules/inquirer/node_modules/rx/dist/rx.all.js:9397:23)
at AutoDetachObserver.Rx.internals.AbstractObserver.AbstractObserver.onCompleted >(/usr/lib/node_modules/yo/node_modules/inquirer/node_modules/rx/dist/rx.all.js:1902:14)
Any idea on what when wrong with the updates (no error thrown).
Thanks for reading and all the best.
PS : I am running on
Fedora 20
npm 2.5.1
yo 1.4.5
generator-polymer 0.7.0
gem 2.1.11
sass 3.4.12
compass 1.0.3
Update yo (npm install -g yo). I had the same problem.

grunt Cannot read property 'name' of undefined

i try grunt on my windows machine. if i use yo webapp all work fine, but if i try to use grunt on exist project i have some problem.
my Gruntfile.js
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
banner:"<%= pkg.name %>"
});
//Load the plugin that provides the "uglify" task.
//grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.log.writeln("____________ log __________________");
grunt.log.writeln(grunt.pkg.name);
grunt.log.writeln(grunt.pkg.banner);
grunt.log.writeln();
grunt.log.writeln();
// Default task(s).
grunt.registerTask('default');}
package.json
{
"name": "bsworld",
"version": "0.1.0",
"description": "mio progetto personale",
"devDependencies": {
"grunt": "^0.4.1"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "grunt test"
}
}
this is the output of grunt --verbose
Initializing
Command-line options: --verbose
Reading "Gruntfile.js" Gruntfile...OK
Registering Gruntfile tasks.
Initializing config...OK
______ log ____________
Loading "Gruntfile.js" tasks...ERROR
>> TypeError: Cannot read property 'name' of undefined
>> at Object.module.exports (C:\Apache\htdocs\bottonisworld.com\Gruntfile.js:13:29)
at loadTask (C:\Apache\htdocs\bottonisworld.com\node_modules\grunt\lib\grunt \task.js:325:10)
at Task.task.init (C:\Apache\htdocs\bottonisworld.com\node_modules\grunt\lib\grunt\task.js:437:5)
at Object.grunt.tasks (C:\Apache\htdocs\bottonisworld.com\node_modules\grunt\lib\grunt.js:120:8)
at Object.module.exports [as cli] (C:\Apache\htdocs\bottonisworld.com\node_modules\grunt\lib\grunt\cli.js:38:9)
at Object.<anonymous> (C:\Users\l.bottoni\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt:45:20)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
No tasks specified, running default tasks.
Running tasks: default
Warning: Task "default" not found. Use --force to continue.
Aborted due to warnings.
why grunt don' load the file json?????
resolved, the json is loaded but i must get data with the correct method
grunt.log.writeln(grunt.config("pkg.name"));

node.js fnoc module gives error -- probably something simple

I'm sure I'm doing something stupid, but I can't get node-fnoc to work. I have a directory with a simple package.json in it, I have a /config/ directory in that directory with a simple JSON file in it. And, I'm basically just trying to do what they have in their example.
var fnoc = require('fnoc');
fnoc(function(err, configs){
console.log(configs);
});
and this is what I'm getting:
fnoc(function(err, configs){
^
TypeError: object is not a function
at Object.<anonymous> (/opt/backups/readconfig.js:3:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
Any ideas?
The document on github is for version 0.2.2
You need to install fnoc#0.2.2
npm install fnoc#0.2.2
Run your js
node your.js
Output:
{ package:
{ name: 'asdf',
dependencies:
{ express: '*',
jade: '*',
Version is in the changelog file:
https://github.com/jprichardson/node-fnoc/blob/master/CHANGELOG.md

Can't install Node.js plugin on Ubuntu 12.04

I have installed this database plugin for Node.js called 'db-mysql' and I've gone through all the steps mentioned on the site without getting errors, which must mean that the plugin is installed. But when I run my .js file (given below) in the Apache server I get this error.
Error: Cannot find module 'db-mysql'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/var/www/server.js:4:13)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
Here is the sample code I'm running.
var http = require("http");
var mysql = require("db-mysql");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
//Database Connectivity//
new mysql.Database({
hostname: 'localhost',
user: 'root',
password: '',
database: 'test'
}).on('error', function(error) {
console.log('ERROR: ' + error);
}).on('ready', function(server) {
console.log('Connected to ' + server.hostname + ' (' + server.version + ')');
}).connect();
///////////////////////
response.end();
}).listen(8889);
Did I miss anything during installation?
If you install module like in example
npm install db-mysql
Node will search it here
./project_root/node_modules/db-mysql
So make sure this path is correct in your case.
Other option is to install it globally by using -g flag
npm install db-mysql -g
Simply make a sym link that points to where your modules are physically installed
Name this link node_modules and put it inside the folder of your project