grunt Cannot read property 'name' of undefined - json

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"));

Related

npm start: Manual browser refresh to reflect changes

I'm learning Node.js and in the introductory course, I had to set-up node and npm. Now according to the instructor, after typing "npm start" in the terminal and then making changes in the index.html file, the browser is supposed to automatically refresh the tab for the changes to take effect, but in my case every time I make a change I had to manually refresh Safari tab to see the changes.
Please note I'm using the same machine, i.e. there is no server and client side per se. Operating system is macOS Catalina 10.15.6.
Any possible fixes please?
Thanks
File Structure:
package.json file:
{
"name": "confusion",
"version": "1.0.0",
"description": "This is a website for Ristorante Con Fusion",
"main": "index.html",
"scripts": {
"start": "npm run lite",
"test": "echo \"Error: no test specified\" && exit 1",
"lite": "lite-server"
},
"author": "Raffay",
"license": "ISC",
"devDependencies": {
"lite-server": "^2.5.4"
},
"dependencies": {
"bootstrap": "^4.5.0",
"jquery": "^3.5.1",
"nodemon": "^2.0.4",
"popper.js": "^1.16.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/RaffaySajjad/conFusion.git"
},
"keywords": [
"conFusion"
],
"bugs": {
"url": "https://github.com/RaffaySajjad/conFusion/issues"
},
"homepage": "https://github.com/RaffaySajjad/conFusion#readme"
}
Starting nodemon from terminal using command "nodemon" prints following error
raffaysajjad#192 ~ % cd /Users/raffaysajjad/Desktop/Full\ Stack\ Web\ Development\ \(React\ Specialization\)/Course\ 1\ \(Front-End\ WebUI\ Frameworks\ \&\ Tools\ -\ Bootstrap\ 4\)/Week\ 1/Exercise/conFusion
raffaysajjad#192 conFusion % nodemon
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: html,json
[nodemon] starting `node index.html`
/Users/raffaysajjad/Desktop/Full Stack Web Development (React Specialization)/Course 1 (Front-End WebUI Frameworks & Tools - Bootstrap 4)/Week 1/Exercise/conFusion/index.html:1
<!DOCTYPE html>
^
SyntaxError: Unexpected token '<'
at wrapSafe (internal/modules/cjs/loader.js:1053:16)
at Module._compile (internal/modules/cjs/loader.js:1101:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
[nodemon] app crashed - waiting for file changes before starting...
this is done by using the npm package of nodemon,
How to Use
npm install -g nodemon
and in your package.json use this
"scripts": {
"start": "nodemon npm run lite"
},
more about nodemon learn here
Yeah.Try installing nodemon.
npm install nodemon
It is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.
To run your server :
nodemon [your node app]
Nodemon will not helo you to browser reload for that kind of things you jest try plugins like connect-livereload
There are so many packages are there for this.
https://www.npmjs.com/package/reload
https://www.browsersync.io/

PhpStorm error when running debugger: SyntaxError: Unexpected token = (on class property)

static UNUSED_TILE = " ";
^
SyntaxError: Unexpected token =
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
Waiting for the debugger to disconnect...
When I comment out that specific static property, it errs on the next one in the file:
class Tile {
static UNUSED_TILE = " ";
static HUMAN_MARKER = "X";
static COMPUTER_MARKER = "O";
I have Node 12.4.0. This only happens when running my debugger in PhpStorm 2019.3. node fileName.js in terminal runs without issues. Any idea what the the debugger could be doing to still have issue?
//package.json
"babel": {
"presets": [
"#babel/preset-env"
],
"plugins": [
"#babel/plugin-proposal-class-properties",
"babel-plugin-transform-class-properties"
]
},
"devDependencies": {
"#babel/plugin-proposal-class-properties": "^7.8.3",
"#babel/preset-env": "^7.9.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"eslint": "^6.8.0",
"jest": "^25.2.3",
"jest-cli": "^25.2.3"
}
Debugger settings
Your screenshots show that you have node 8.10.0 used as node.js interpreter; this node.js version doesn't support class properties. According to this table, they are supported since Node.js 12.x. If you have the appropriate version installed (you wrote that you have Node 12.4.0), please make sure to select it as a Node interpreter: in your run configuration

How to gulp a task without "primordials is not defined" and "Assertion error" errors

Today I tried to gulp a task inside my web application project folder on Tomcat8. I got a source code for a web app and I am trying to customize it.
I know there are several explanations about the errors I am going to show you, but it's always about switching from V3.9.1 to V4.0.2 and from V4.0.2 to V3.9.1 my gulp version. And I get a different problem on both :D
V3.9.1 Gulp:
C:\Users\Administrator\Documents\Source code for Apache\pa-downtime-uApp-master>gulp
fs.js:27
const { Math, Object } = primordials;
^
ReferenceError: primordials is not defined
at fs.js:27:26
at req_ (C:\Users\Administrator\Documents\Source code for Apache\pa-downtime-uApp-master\node_modules\natives\index.js:143:24)
at Object.req [as require] (C:\Users\Administrator\Documents\Source code for Apache\pa-downtime-uApp-master\node_modules\natives\index.js:55:10)
at Object.<anonymous> (C:\Users\Administrator\Documents\Source code for Apache\pa-downtime-uApp-master\node_modules\vinyl-fs\node_modules\graceful-fs\fs.js:1:37)
at Module._compile (internal/modules/cjs/loader.js:774:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
at Module.load (internal/modules/cjs/loader.js:641:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:681:19)
at require (internal/modules/cjs/helpers.js:16:16)
C:\Users\Administrator\Documents\Source code for Apache\pa-downtime-uApp-master>gulp -v
CLI version: 2.2.0
Local version: 3.9.1
V4.0.2 Gulp
AssertionError [ERR_ASSERTION]: Task function must be specified
at Gulp.set [as _setTask] (C:\Users\Administrator\Documents\Source code for Apache\pa-downtime-uApp-master\node_modules\undertaker\lib\set-task.js:10:3)
at Gulp.task (C:\Users\Administrator\Documents\Source code for Apache\pa-downtime-uApp-master\node_modules\undertaker\lib\task.js:13:8)
at Object.<anonymous> (C:\Users\Administrator\Documents\Source code for Apache\pa-downtime-uApp-master\gulpfile.js:18:6)
at Module._compile (internal/modules/cjs/loader.js:774:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
at Module.load (internal/modules/cjs/loader.js:641:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:681:19)
at require (internal/modules/cjs/helpers.js:16:16)
at execute (C:\Users\Administrator\AppData\Roaming\npm\node_modules\gulp-cli\lib\versioned\^4.0.0\index.js:36:18) {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
}
Task script
I was also wondering why in my task scripts there is no task name … I would like to try my commands with a task name after "gulp". When I check on other task files there is always a task name:
'use strict';
const gulp = require('gulp'),
war = require('gulp-war'),
zip = require('gulp-zip'),`enter code here`
path = require('path');
module.exports = function (gulp) {
return function () {
return gulp.src('dist/bundled/**')
.pipe(war({
welcome: 'index.html',
displayName: 'downtime-app',
}))
.pipe(zip('downtime-app.war'))
.pipe(gulp.dest('dist/'));
};
};
Sorry if I am saying any blunders, I am a total beginner and didn't even know what was HTML 1 week ago … :)
If you are using gulp 3.. and node 12+ => it will give the error.
Uninstall it and use node 11.. with gulp 3..
Ok, when I replace all my tasks by a single and basic one, the gulp 4.0.2V works well:
C:\Users\Administrator\Documents\Source code test\Source code test\Source code for Apache\pa-downtime-uApp-master>gulp bacon
[14:46:12] Using gulpfile ~\Documents\Source code test\Source code test\Source code for Apache\pa-downtime-uApp-master\gulpfile.js
[14:46:12] Starting 'bacon'...
miam miam
[14:46:12] The following tasks did not complete: bacon
[14:46:12] Did you forget to signal async completion?
So I think the problem remains in the dependencies between the several tasks or, in the bonds between the gulpfile.js and the task folder that was made by my web application creator.
Finally I found the task names in the gulpfile.js but even using them doesn't work.

Parsing JSON file using Lambda

My main objective is to parse a JSON file and write the content of the JSON file to a scrollable list in the browser. I created a JSON file and stored it in an S3 bucket. I then created the following Lambda function:
// ----receive function----v
function get_json(url, callback) {
http.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var response = JSON.parse(body);
// call function ----v
callback(response);
});
});
}
var mydata = get_json("https://s3.amazonaws.com/scrollablelist/scrollableList.json", function (resp) {
console.log(resp);
});
I get the following error:
{
"errorMessage": "http is not defined",
"errorType": "ReferenceError",
"stackTrace": [
"Object.<anonymous> (/var/task/index.js:20:18)",
"Module._compile (module.js:570:32)",
"Object.Module._extensions..js (module.js:579:10)",
"Module.load (module.js:487:32)",
"tryModuleLoad (module.js:446:12)",
"Function.Module._load (module.js:438:3)",
"Module.require (module.js:497:17)",
"require (internal/module.js:20:19)"
]
}
The Log Output says this:
START RequestId: 516f164b-7ee3-11e7-97a7-0373caaf493b Version: $LATEST
module initialization error: ReferenceError
at get_json (/var/task/index.js:3:9)
at Object.<anonymous> (/var/task/index.js:20:18)
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.require (module.js:497:17)
at require (internal/module.js:20:19)
END RequestId: 516f164b-7ee3-11e7-97a7-0373caaf493b
REPORT RequestId: 516f164b-7ee3-11e7-97a7-0373caaf493b Duration: 80.81 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 19 MB
Any help would be greatly appreciated.
You have to install npm package for http in your project folder. Please follow below steps,
a. Open node command prompt
b. Navigate (CD ) to your project folder
c. Type command "npm init" if you don't have a package.json file already
e. Type command "npm install http --save" (Which will add http module to package.json)
f. At the beggining of you js file you using "http" please refer http moduleas below,
var http = require("http");
g. Add package.json file as well in the .zip to upload to Lambda (You don't need to include node modules folder)
You have to install the http module. npm install --save http
https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/

Write file in home directory when running `npm install`

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