Could not find a declaration file for module ‘fawn’ in NodeJS - node.js

I am trying to install npm module Fawn into my NodeJS file. I have installed it in my root directory of my project file. It is loaded into my root directory node_module folder.
However when I try to load it into my project file, I am getting the following error: Could not find a declaration file for module ‘fawn’.
const Fawn = require(‘fawn’);
Has anyone had this same issue? Any help is much appreciated!

For the importing JS module in the typescript, you should have the typings file as well. Commonly it's declared in the package or can be installed like this: npm i -D #types/package-name. However, for this module, it's not available and the author isn't planning to add this: https://github.com/e-oj/Fawn/issues/57 . You can create your d.ts file for this library and declare required types there: https://www.ankursheel.com/blog/custom-type-definitions-for-java-script-dependencies

Fawn.init('mongodb://localhost/vidly');
try{
new Fawn.Task()
.update('movies', { _id: movie._id }, {
$inc: { numberInStock: -1 }
})
.save('rentals', rental)
.run()
res.send(rental)
} catch (ex) {
console.log(ex)
}
try to add a database link instead of a mongoose. this worked for me

Related

How does this nodejs project reference a shared library folder?

I'm looking at this project and they have multiple node projects like:
api
project2
project3
shared
So the various projects reference the shared folder like:
if (process.env.NODE_ENV === 'development') {
const logging = require('shared/middlewares/logging');
middlewares.use(logging);
}
https://github.com/withspectrum/spectrum/blob/alpha/api/routes/middlewares/index.js#L6
And the logging.js file is in the shared folder:
// #flow
// Log requests with debug
const debug = require('debug')('shared:middlewares:logging');
module.exports = (
req: express$Request,
res: express$Response,
next: express$NextFunction
) => {
debug(`requesting ${req.url}`);
next();
So I tried to do something similiar in my node/express project but I am getting this error:
This dependency was not found:
* shared/middlewares/logging in ./src/middlewares/index.js
To install it, you can run: npm install --save shared/middlewares/logging
Is there something they did in their project to allow this to work?
Naturally you have to show relative path for "require()" if you use your own modules, e.g.
require('./path/to/custom/module/file')
// In this case smth like
require('../../../shared/middlewares/logging')
If you do not use relative path, it will search for installed package, and that's why you got an error with suggestion to install because it's not found.
There are several ways to tell node to search package in custom directory. You can check this link for examples. In "spectrum" project it's configured by setting up NODE_PATH environment variable, you can see it here and here
At those lines you can see NODE_PATH=./, which tells node to look for packages in the root directory.
That's it, hope now it's clear :)

how to solve the error that fs module is not found when used react and next.js

Am using a react application without router settings. I want to build my sitemap.xml file. I tried some modules like sitemap.js, react-router-sitemap, sitemap-generator. But these module are throwing error as fs module is missing. I installed fs module via npm install --save. But it is still showing the error.
I found in some forums to add the below code in webpack.config file.
node: {
fs: "empty"
}
Am not sure where this file is. I couldn't find them nside the sitemap related modules.
Please help me to resolve this. Am new to react.
Here is my folder structure.
create next.config.js and put below code. It works fine for me.
next.config.js
module.exports = {
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
// Note: we provide webpack above so you should not `require` it
// Perform customizations to webpack config
// Important: return the modified config
// Example using webpack option
//config.plugins.push(new webpack.IgnorePlugin(/\/__tests__\//))
config.node = {fs:"empty"}
return config
},
webpackDevMiddleware: config => {
// Perform customizations to webpack dev middleware config
// Important: return the modified config
return config
},
}

Electron package - how to write/read files

I have file test.txt in my root directory of app. When I run my app with command npm start, I can write to my file without any problem, but when I make package using electron packager, writing text to my file is not possible anymore - I got error
Error: EACCES: permission denied, open './test.txt'
For this, I'm using node.js filesystem:
fs.writeFile("./test.txt",text,function(err){
if(err) {
return alert(err);
}
alert("saved");
});
How is possible to make this working? And is possible to include some extra folder in my app after package process? Thanks for your help!
There are a lot of options to choose to package your electron app in 2019, so in case you come to this question like I did and are using electron-builder, please try my suggestion below.
If you are using electron-builder to package your application and need to read/write a file that is stored within your solution, you can add it to your files property in your package.json. The properties in this file property are files that are copied when packaging your electron app - reference.
In my example, I was reading/writing to file.json.
let fs = require("fs");
fs.writeFile("./file.json", "data to file", "utf-8", (error, data) => {
if (error){
console.error("error: " + error);
}
});
My folder structure looked like this.
parent-folder
app/
assets/
configs/
images/
resources/
...
file.json
My app was not working after I packed it until I added the following file.json in my "build" property in my package.json.
"build": {
"productName": "MyApp",
"appId": "org.dev.MyApp",
"files": [
"app/dist/",
"app/app.html",
"app/main.prod.js",
"app/main.prod.js.map",
"package.json",
"file.json", // << added this line
],
//...
}
Didn't really found out what the problem was, so I tried another solution, which works for me (my main aim was to save data to some local memory of app).
I used npm package electron-store which is really easy to use.
You can get it by typing this to terminal
npm install electron-store
More info about it here: Electron store
Hope it helps someone else too :-)

Module not found error when trying to use a module as a local module

I am trying to understand as how to make a local module. At the root of node application, I have a directory named lib. Inside the lib directory I have a .js file which looks like:
var Test = function() {
return {
say : function() {
console.log('Good morning!');
}
}
}();
module.exports = Test;
I have modified my package.json with an entry of the path to the local module:
"dependencies": {
"chat-service": "^0.13.1",
"greet-module": "file:lib/Test"
}
Now, if I try to run a test script like:
var greet = require('greet-module');
console.log(greet.say());
it throws an error saying:
Error: Cannot find module 'greet-module'
What mistake am I making here?
modules.export is incorrect. It should be module.exports with an s.
Also, make sure after you add the dependency to do an npm install. This will copy the file over to your node_modules and make it available to the require function.
See here for a good reference.
Update:
After going through some examples to figure this out I noticed, most projects have the structure I laid out below. You should probably format your local modules to be their own standalone packages. With their own folders and package.json files specifying their dependencies and name. Then you can include it with npm install -S lib/test.
It worked for me once I did it, and it'll be a good structure moving forward. Cheers.
See here for the code.

Not able to require a node module

I'm new to node js and require js. I installed a node module via npm install(https://www.npmjs.org/package/box-view). The node_modules folder has a box-view/index.js containing:
module.exports = {
BoxView: BoxView,
createClient: function (key) {
return new BoxView(key);
}
};
When I try to access the module using require:
require ['box-view'], () ->
console.log("Ready")
I get:
GET http://127.0.0.1:9000/js/box-view.js 404 (Not Found).
Looks like I'm doing a basic mistake. Thanks in advance!
Node has a simple module loading system - files and modules are in one to one correspondence.
var boxView = require('box-view');
console.log("Ready");
I think problem is because you did a npm install box-view so it will be under node_modules/box_view/index.js.
But using require you are just saying require ['box-view'] so it's looking ./box-view.js
This will work
require(["node_modules/box-view/index"]
but this is not a good practice.
You should have a look on require node manual. It tells how to use requirejs with node.

Resources