Visual Studio Code failed to import node module package - node.js

I am new to Node.js and I am trying to import module exports module.exports. But it does not give me any auto suggestions and displays a quick fix (an error) of that line.
logger.js
var url = 'https://www.google.com';
function log(message){
//send http request
console.log(message);
}
module.exports.log = log;
error.
var module: {
"d:/NotitiaZone/Node/logger": typeof import("d:/NotitiaZone/Node/logger");
}
File is a CommonJS module; it may be converted to an ES6 module.ts(80001)
my node version -v10.16.3 my npm versoin - 6.9.0
I have already installed below extensions.

Related

Module `fs` does not exist in the Haste module map

I'm new to Node.js and react-native. I followed the sample on send_telemetry.js exactly but when I run my react-native app I get an error: "The development server returned response error code 500.
the error message is:
bundling failed: Error: Unable to resolve module fs from ProjectPath\node_modules\azure-iot-device\lib\module_client.js: Module fs does not exist in the Haste module map";
Im running:
Node.js v10.15.3
NPM 6.4.1
react-native#0.59.2
First error was the same with Unable to resolve module events,
I can install events,
but the fs module is: "This package name is not currently in use, but was formerly occupied by another package. To avoid malicious use, npm is hanging on to the package name, but loosely, and we'll probably give it to you if you want it."
var Protocol = require('azure-iot-device-http').Http;
var DeviceClient = require('azure-iot-device').Client;
var Message = require('azure-iot-device').Message;
var connectionString = 'my connection string';
var client = DeviceClient.fromConnectionString(connectionString, Protocol);
function ConnectionTest(err) {
if (err) {
console.log('Could not connect: ' + err);
} else {
console.log('Client connected');
}
client.close(function () {
process.exit(0);
});
};
export async function Test() {
client.open(ConnectionTest);
};
Basically I need to know how to get the azure IOT hub client working in my react-native app (not using Expo).
Im pretty much stumped so any help would greatly be appreciated.
A dependency module is missing ... which is fs ...
this file-system npm module is incompatible with react-native ... cause it has it own different environment.
I had "import { symlink } from 'fs';" randomly pop up in one of my scripts. Once I deleted this line same issue you had went away. I would search your whole project for that line.

Error: Cannot find module 'togeojson'

I am attempting to use this module in node.js and am running into an "Error: Cannot find module 'togeojson'" error when I attempt to use the documented example code:
// using togeojson in nodejs
var tj = require('togeojson'),
fs = require('fs'),
// node doesn't have xml parsing or a dom. use xmldom
DOMParser = require('xmldom').DOMParser;
var kml = new DOMParser().parseFromString(fs.readFileSync('foo.kml', 'utf8'));
var converted = tj.kml(kml);
var convertedWithStyles = tj.kml(kml, { styles: true });
I ran npm init in the same directory that my app.js file (where the above code resides) is stored and I used the --save flag when installing the #mapbox/togeojson package to my application.
I am running node version 8.11.2 and npm v 6.1.0.
How do I go about debugging an issue like this in node/npm?
It is #mapbox/togeojson package, not togeojson, so it should be required like:
var tj = require('#mapbox/togeojson');

Cannot find module './lib/BufferMaker' when use buffermaker in Meteor 1.5.1

I have encountered a problem when use some npm package in Meteor (version 1.5.1), any help on it will be much appreciated.
My Environment:
meteor: 1.5.1
buffermaker: 1.2.0
What I Did:
Create a sample Meteor app.
meteor create test
Install buffermaker
meteor npm install --save buffermaker
Import buffermaker in Meteor app by editing test/client/main.js, add line:
import { BufferMaker } from 'buffermaker';
Full content of test/client/main.js:
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import { BufferMaker } from 'buffermaker';
import './main.html';
Template.hello.onCreated(function helloOnCreated() {
// counter starts at 0
this.counter = new ReactiveVar(0);
});
Template.hello.helpers({
counter() {
return Template.instance().counter.get();
},
});
Template.hello.events({
'click button'(event, instance) {
// increment the counter when button is clicked
instance.counter.set(instance.counter.get() + 1);
},
});
Run the Meteor app
meteor npm install
meteor
I got this error in the console of browser (Chrome).
modules-runtime.js?hash=8587d18…:231 Uncaught Error: Cannot find module './lib/BufferMaker'
at makeMissingError (modules-runtime.js?hash=8587d18…:231)
at require (modules-runtime.js?hash=8587d18…:241)
at index.js (modules.js?hash=e9fc8db…:1016)
at fileEvaluate (modules-runtime.js?hash=8587d18…:343)
at require (modules-runtime.js?hash=8587d18…:238)
at main.js (main.js:1)
at fileEvaluate (modules-runtime.js?hash=8587d18…:343)
at require (modules-runtime.js?hash=8587d18…:238)
at app.js?hash=3f48780…:101
Did you try:
import BufferMaker from 'buffermaker';
Some if not most modules do a default export meaning that you don't need the curley braces in your import statement
Turns out buffermaker re-exports it’s main module in a strange way, so first step is to bypass it by importing BufferMaker directly:
import BufferMaker from 'buffermaker/lib/BufferMaker';
Then you’ll find when you call .make(), it will complain about Buffer not existing. To get Buffer on the client, first install meteor-node-stubs
$ meteor npm install --save meteor-node-stubs
Then load the buffer module and stick it on the window so BufferMaker can access it
import { Buffer } from 'buffer';
window.Buffer = Buffer;
/* OR do it with require */
window.Buffer = require('buffer').Buffer;

Require node.js module in an internal typescript module

I want to port a a multi-file node.js project to a multiple file Typescript project compiling to a single .js by using Typescript internal modules. How I can require a node.js module from an internal Typescript module? Here is a sample application:
a.js:
var os = require('os')
exports.foo = os.hostname()
b.js:
var os = require('os')
exports.foo = os.hostname()
How do I port the code to Typescript?
I have no problems until I want to require:
a.ts:
/// <reference path="b.ts" />
console.log(b.foo)
b.ts:
module b {
export var foo = 42;
}
How do I change the code so foo = os.hostname() works?
Unfortunately you cannot mix --out with --module at the moment. TypeScript does not give an error the but the generated javascript is wrong

TypeScript VS 2012 plugin is not generating JavaScript for this specific code

I'm writing this code in the editor
///<reference path='../node/express3.d.ts' />
///<reference path='../node/node.d.ts' />
import http = module('http');
var reqRecieved = function (req, res): void {
res.end();
};
var server = http.createServer(reqRecieved);
server.listen("1337");
console.log("server started");
And the problem is that the TypeScript vs2012 plugin is not generating the JavaScript code for the same. but if I change line:
import http = module('http');
to line
var http = require('http');
then it generates fine.
what am I doing wrong here?
I would check you have the latest version of WebEssentials 2012 installed, as there were some issues with module import code not generating in builds 1.8.*
Also, there's an oddity in TS where imports inside a module generate no code:
export module test {
import myMod = module("MyMod"); // Generates no JS output
var class = myMod.someClass;
}
But:
import myMod = module("MyMod"); // Outside the module. Generates JS as expected.
export module test {
var class = myMod.someClass;
}
If you aren't getting an explanation via Visual Studio, it can sometimes help to hit the command line and see that is going on:
tsc --debug c:\path\to\yourfile.ts
This may give you more detailed errors.

Resources