'console' is 'undefined in the shell by using webpack with node.js - javascript

I have a file my-script.js that only returns this:
console.log('Hi I'm working')
When I type in shell only
node my-script.js
This works successfully.
But I'd like it to work too when I typed in just this:
npm start
How to make it work?
Because it is generating an error message:
Line: 1 Error: console is undefined Code: 800A1391 Source: Microsoft
JScript runtime error
I have a package.json file that points to this file to be the start but this is not resolving.
Here is my package.json file:
{
"name": "my-app",
"version": "1.0.0",
"main": "my-script.js",
"scripts": {
"start": "my-script.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"webpack-dev-server": "^3.9.0"
},
"devDependencies": {
"webpack": "^4.41.2"
},
"description": ""
}

Use node in start script
{
"name": "my-app",
"version": "1.0.0",
"main": "my-script.js",
"scripts": {
"start": "node my-script.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"webpack-dev-server": "^3.9.0"
},
"devDependencies": {
"webpack": "^4.41.2"
},
"description": ""
}

Related

import { pipe } from "lodash/fp" gives 'ERR_UNSUPPORTED_DIR_IMPORT' error

I was trying to import pipe from lodash/fp
import { pipe } from "lodash/fp";
But getting following errors
Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/home/user/Codes/redux-tut/node_modules/lodash/fp' is not supported resolving ES modules imported from /home/user/Videos/Codes/redux-tut/src/index.js
Did you mean to import lodash/fp.js?
at finalizeResolution (internal/modules/esm/resolve.js:267:17)
at moduleResolve (internal/modules/esm/resolve.js:668:10)
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:758:11)
at Loader.resolve (internal/modules/esm/loader.js:100:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:246:28)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:47:40)
at link (internal/modules/esm/module_job.js:46:36) {
code: 'ERR_UNSUPPORTED_DIR_IMPORT',
url: 'file:///home/user/Videos/Codes/redux-tut/node_modules/lodash/fp'
}
My package.json file is
{
"name": "redux-tut",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node src/index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"lodash": "^4.17.20"
}
}
This solved it for me:
import pipe from "lodash/fp/pipe.js";

npm start not picking up updated package.json

I am writing a node app from scratch, with the following package.json, which is pretty boiler-plate.
{
"name": "myfirstnodeproject",
"version": "1.0.1",
"description": "Learning node",
"main": "index.js",
"start": "node server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "ISC",
"dependencies": {
"request": "^2.88.2"
}
}
I created server.js to look like this:
var http = require("http");
http.createServer((inRequest, inResponse) => {
inResponse.end("Your IP Address is " + inRequest.connection.remoteAddress);
}).listen(9000);
When I started the app using npm start, it worked fine.
Then, I created a new file called server_time.js:
require("http").createServer((inRequest, inResponse) => {
const requestModule = require("request");
requestModule(
"http://worldtimeapi.org/api/timezone/America/New_York",
function (inErr, inResp, inBody) {
inResponse.end(
`Hello from my first Node Web server: ${inBody}`
);
}
);
}).listen(9000);
I changed the following line in my package.json:
"start": "node server_time.js",
However, Node still seems to pick up server.js instead. I tried npm cache verify, npm cache clear --force, rm -rf node_modules, rm package-lock.json, and npm install again, but the problem didn't seem to go away. I even removed package.json and redefined it, but the value of start when I call npm start is still stale.
Here's an output from my shell:
GsMacbookPro:MyFirstNodeProject g$ cat package.json
{
"name": "myfirstnodeproject",
"version": "1.0.1",
"description": "Learning node",
"main": "index.js",
"start": "node server_time.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "G",
"license": "ISC",
"dependencies": {
"request": "^2.88.2"
}
}
GsMacbookPro:MyFirstNodeProject g$ ls
node_modules package-lock.json package.json server.js server_time.js
GsMacbookPro:MyFirstNodeProject g$ npm start
> myfirstnodeproject#1.0.1 start /Users/g/Repos/MyFirstNodeProject
> node server.js
Before someone asks, node version is v10.16.3, and npm version is 6.9.0.
The reason why npm start currently works for you is because npm will default some script values based on package contents.
If there is a server.js file in the root of your package, then npm will default the start command to node server.js.
See here:
npm.js - default values
So your start field inside package.json wasn't actually doing anything because it was not under scripts, which is where it should belong.
The correct approach is to update your package.json so that it looks like the following:
{
"name": "myfirstnodeproject",
"version": "1.0.1",
"description": "Learning node",
"main": "index.js",
"scripts": {
"start": "node server_time.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "G",
"license": "ISC",
"dependencies": {
"request": "^2.88.2"
}
}
Try:
{
"name": "myfirstnodeproject",
"version": "1.0.1",
"description": "Learning node",
"main": "server_time.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server_time.js"
},
"license": "ISC",
"dependencies": {
"request": "^2.88.2"
}
}
and then npm run start.

problems with including dependencies in package.json

Why is it that when including dependencies in package.json such as
"dependencies": {
"nodemailer": "*"
}
and running
npm install
doesn't install the package but doing
npm install nodemailer
is ok?
in both instances, the message returned was
WARN Invalid name: "try node mailer"
WARN email No description
WARN email No repository field
WARN email No README field
WARN email No license field
but only in the second instance the node_modules directory is populated with the package
I'd suggest that you use some of the other version matching ranges and avoid the asterix one,, as per specs #page: https://docs.npmjs.com/files/package.json#dependencies
I did it fine with this one:
{
"name": "test",
"version": "1.0.0",
"description": "my test",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"nodemailer": "~2"
},
"author": "",
"license": "ISC"
}
hope that helps you
Rather than using a wildcard, you can try "" instead. Otherwise this seems to be working:
{
"name": "testr",
"version": "1.0.0",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"nodemailer": "^4.0.1"
},
"author": "",
"license": "ISC"
}

npm gulp 800a138f javascript error

When i put 'gulp hello' in cmd i got this error 800a138f javascript object expected error
This is my package.json file
`{
"name": "goes",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node gulp.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"gulp": "^3.9.1"
}
}`
This is gulp.js
`var gulp = require('gulp');
gulp.task('hello', function() {
console.log('Hello Zell');
});`

request.ref is not a function

I ran the node code on this site https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html by using:
node app
I get the following error:
TypeError: request.ref is not a function.
which corresponds to the following line:
sendNotificationToUser("username","new msg",function() {request.ref().remove();} );
This is my package.json file:
{
"name": "myApp",
"version": "1.0.1",
"description": "listen for addition of msgs",
"main": "app.js",
"scripts": {
"start": "node app.js",
"monitor": "nodemon app.js",
"deploy": "gcloud app deploy"
},
"author": "my name",
"engines": {
"node": "~4.2"
},
"license": "ISC",
"dependencies": {
"firebase": "^3.2.1",
"request": "^2.74.0"
}
}
There are indeed some typos in the code. The actual version I run with uses this to remove the messages that have been sent:
requestSnapshot.ref.remove();
The entire listenForNotificationRequests method (in case I made any other edit mistakes while porting to the blog):
function listenForNotificationRequests() {
var requests = ref.child('notificationRequests');
requests.on('child_added', function(requestSnapshot) {
var request = requestSnapshot.val();
sendNotificationToUser(
request.username,
request.message,
function() {
requestSnapshot.ref.remove();
}
);
}, function(error) {
console.error(error);
});
};

Categories

Resources