Unable to get grunt to run any task - node.js

Very new to this, sorry.
I'm running node.js v6.9.5 npm 4.2.0.
Installed grunt-cli on Windows 10.
And this is where I think I've gone wrong, I did npm install -g grunt-init which has created a node_modules folder in C:\User[my name]\AppDate\Roaming directory.
I've since uninstalled this using npm uninstall grunt --save.
I created the following package.jason in my project root:
{
"name": "manifest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {},
"devDependencies": {
"grunt": "^1.0.1"
}
}
run npm install which created a new node_modules directory in my project root.
Create a Gruntfile.js containing the following:
module.exports = function(grunt) {
grunt.registerTask('speak', function() {
console.log('hello');
})
}
But when I run grunt speak I'm getting the message:
Loading "Gruntfile.js" tasks...ERROR
>> SyntaxError: Invalid or unexpected token
Warning: Task "speak" not found. Use --force to continue.
Aborted due to warnings.
What am I doing wrong please?

Ok, managed to get round this by totally uninstalling grunt, grunt-cli, npm and node.js, and reinstalling them all from scratch.

Related

can not setup ReactJS on mac

I need to setup ReactJS on my Mac. I have npm version of 4.1.2 and node version of v7.7.4. I clone the project from Git and in the project folder trying to execute npm install and following npm start. But I get error.
I removed&uninstalled node and npm and then re-installed with homebrew, but it did not help. Here is the screen of error I get in terminal? Do you have any idea what is the problem?
Here is my package.json file
{
"name": "viaopt",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {},
"devDependencies": {
"webpack": "^2.3.0"
},
"description": ""
}
From the error you are showing I can tell that there is no start script in your package.json file. For npm start to work there should be a command under the start key under the scripts attribute in your package.json file.
When you run npm start npm looks in package.json and runs whatever is listed there under the start value.
I'm guessing you have gulp or webpack configured with your project?
Try running webpack in your terminal and see if your project fires up or smth.
Hopes this helps.
Cheers.
If your trying to setup a new React App you can use
Create React App. It will setup React / Webpack for you.

Refusing to install 'module' as a dependency of itself

Recently, I was playing with a gulp tutorial and had this error
Refusing to install gulp as a dependency of itself
when executing
npm install --save-dev gulp
what could be the issue?
The problem was in the name of my own application.
In package.json, I accidently named it gulp
{
"name": "gulp",
"version": "1.0.0",
"description": "tutorial",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "rmv",
"license": "ISC",
"devDependencies": {}
}
Make sure your app is not called as one of the dependencies you will be using.
This error occurs when you are trying to install the package from the path (or in the folder) in which you had created the package.. Just navigate to some other folder or path and then try to install the package It should work.

TypeError: Invalid Version: undefined using Gulp in Visual Studio

I'm new to all of this Node.js, npm, Bower and Gulp stuff so I'm trying to start get familiar with it. I started by installing Node.js which seemed to go smoothly. I then installed gulp both globally and locally according to what I've found online:
> npm install --global gulp
> npm install --save-dev gulp
The second one was of course done in the directory of my project and it did indeed update my package.json file.
So I now have a package.json file that looks like this:
{
"name": "typescripttestapp",
"version": "1.0.0",
"description": "",
"main": "index.html",
"dependencies": {
},
"devDependencies": {
"del": "^1.2.0",
"gulp": "^3.9.0",
"gulp-concat": "^2.6.0",
"gulp-copy": "0.0.2",
"gulp-typescript": "^2.7.8"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
I've also installed Task Runner Explorer in Visual Studio. I have the following gulpfile.js:
var gulp = require('gulp');
gulp.task("default",function(){});
However, any time I try to refresh the Task Runner Explorer, I get the following error in the Output Window:
Failed to load
"F:\Business\Development\TestSandBoxes\TypeScriptTestApp\TypeScriptTestApp\gulpfile.js"...
C:\Users\rodd_harris\AppData\Roaming\npm\node_modules\gulp\node_modules\semver\semver.js:281
throw new TypeError('Invalid Version: ' + version);
^ TypeError: Invalid Version: undefined
at new SemVer (C:\Users\rodd_harris\AppData\Roaming\npm\node_modules\gulp\node_modules\semver\semver.js:281:11)
at SemVer.compare (C:\Users\rodd_harris\AppData\Roaming\npm\node_modules\gulp\node_modules\semver\semver.js:348:13)
at compare (C:\Users\rodd_harris\AppData\Roaming\npm\node_modules\gulp\node_modules\semver\semver.js:571:31)
at Function.gt (C:\Users\rodd_harris\AppData\Roaming\npm\node_modules\gulp\node_modules\semver\semver.js:600:10)
at Liftoff.handleArguments (C:\Users\rodd_harris\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:99:14)
at Liftoff. (C:\Users\rodd_harris\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\index.js:192:16)
at module.exports (C:\Users\rodd_harris\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\node_modules\flagged-respawn\index.js:17:3)
at Liftoff. (C:\Users\rodd_harris\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\index.js:185:9)
at C:\Users\rodd_harris\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\index.js:159:9
at C:\Users\rodd_harris\AppData\Roaming\npm\node_modules\gulp\node_modules\v8flags\index.js:91:14
What am I doing wrong?
Edit
By some experimenting, I found that gulp will run if I do gulp -v first. If I open a command prompt and go to my project directory and just run gulp I get the same error as I do from Task Runner. However, if, as I said, I do the gulp -v first, then run gulp it works.
It would still be nice to know if I've got something configured wrong because at present, I'm still unable to set this up with Task Runner Explorer.
I found a solution to this issue, make sure you have run npm init in the base directory of your gulp file and then install gulp locally npm install gulp --save-dev. This seems to resolve it for me when running gulp in Visual Studio Code.

how to automate dependency resolution (local and remote) in node

I have a very small data-cap so I want to install dependencies for node projects by first linking to whatever it finds installed on local machine and fetching remotely if not found locally. I don't want to manually go through all the require statements in the apps because theres a lot of them - is there a node way to do this?
eg. this except not manually determining what is or isn't installed
Project A
npm install -g connect
npm install -g serve-static
then later
Project B
npm link connect
npm link serve-static
The preferred way to go about dependency management is with a package.json.
To get started with one of these in a project, run npm init, and answer all the questions. This will leave you with a file like this:
{
"name": "test-project",
"version": "1.0.0",
"description": "A test project",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Josh",
"license": "ISC"
}
Once this is in place, add the --save flag to each of your npm install commands. For example, if you run npm install --save connect, your package.json changes to this
{
"name": "test-project",
"version": "1.0.0",
"description": "A test project",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Josh",
"license": "ISC",
"dependencies": {
"connect": "^3.3.5"
}
}
Now whenever you need to install the project's dependencies, run npm install, and all the dependencies listed in the package.json will be automatically installed.

npm WARN install Refusing to install hapi as a dependency of itself

I tried to do the following (per instructions from official site):
mkdir hapi && cd hapi
npm init
npm install hapi --save
But this gives me an error like this:
npm WARN install Refusing to install hapi as a dependency of itself
Now, I made a new test folder called hapiTest and repeated the commands and then everything worked fine.
I tried the same process with a folder gulp and npm install gulp --save, and got the same error, so my conclusion is that I can't have the name of the folder be the same as the package that I want to install, but can someone back this statement up with some official documentation?
When you did the command npm init, there were probably some relevant questions you needed to answer. Specifically, the name of your module. When you use npm init, it assumes you want the name of the module you're creating to be called the name of the folder it is in.
So it's not the name of the folder that is stopping you from installing the dependency, it is the name of the npm module that you are creating.
Open the resulting package.json within your hapi directory, and rename the module to something other than hapi. Here's an example 'package.json' that works, even when residing in a folder called hapi:
{
"name": "hapi-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"hapi": "^7.5.2"
}
}
Added Note
I've not been able to find any documentation thus-far explaining this phenomena in the context of npm; though it is a bit of a no-brainer. Requiring modules with the same name within the same application would conflict with the CommonJS philosophy.
The name of your module is same as the module you are trying to install. NPM thinks that you are installing the module to itself. Change the name of your module and it will install perfectly.
Reason
Module name is same with library name
Solution
Change the module name to something else
Change 'name' in package.json
The issue can be simply explained as follows
the name of your package or module in package.json cannot be same as that of the package or module you are trying to install.
Here hapi is the name of your module and you are trying to install a module with name hapi with npm install hapi --save
This was my initial code
{
"name": "react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^15.6.1"
}
}
which threw error
npm WARN package.json react#1.0.0 No description
npm WARN package.json react#1.0.0 No repository field.
npm WARN package.json react#1.0.0 No README data
npm WARN install Refusing to install react as a dependency of itself
then i renamed the name from react to react_app and my code looks like
{
"name": "react_app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^15.6.1"
}
}
then it worked

Resources