When I try to compile my Typescript project (NestJS) I came across the following problem.
Some packages like nanoid, cookie-parser give error Error: Cannot find module 'nanoid'
The code where they used is below:
...
import { nanoid } from "nanoid";
...
const confirmToken = nanoid(32);
...
Here are what I tried:
I checked the node_modules directory. The package is there
Tried to completely remove node_modules and package.json.lock.
Used v16.14.0, v17 versions of NodeJS
Used yarn and npm
None of these ways helped.
Again, this error happens with only certain packages.
What could be an issue?
Related
This is my error and I can't fix it :(((
This error appeared while I
import { GuardProvider, GuardedRoute } from 'react-router-guards'
and use it.
It's not expressly limited by react-router-guard's package.json the repo and README both state there's a peer dependency on react-router-dom#5.
See Requirements.
This package has the following peer dependencies:
React v16.8.0+ (for hooks ⚓️)
React Router DOM v5.0.0+
Based on the error it seems you have react-router-dom#6 installed and it has many different exports than the previous version. To resolve you will need to revert to the v5 version of react-router-dom.
To install, run from the project's root directory:
npm i -s react-router-dom#5
[15:03:25] [snowpack] Welcome to Snowpack! Because this is your first
time running this project, Snowpack needs to prepare your
dependencies. This is a one-time step and the results will be cached
for the lifetime of your project. Please wait... [15:03:25] [snowpack]
Package "fs" not found. Have you installed it?
When I try to start snowpack I got the error above...
Is 'fs' not include in node by default? When I tried to install 'fs' via npm install fs, not started as well. Some of my files(test files) use 'fs' to run some tests...
Obs: I am using to import:
import fs from 'fs';//(not require('fs'))
Thanks. André
I discovered how to deal with it in the snowpack website using the configuration in snowpack.config.js (packageOptions.external: ["fs", "path"])
module.exports = {
packageOptions: {
/* ... */
external: ["fs", "path"]
}
}
I have a Nest JS application for which I am writing tests for some of the Provider classes. The beforeAll fn. of jest defined in my *.spec.ts file runs fine but the jest todo() fn. fails with the following error:
Cannot find module 'rxjs' from 'node_modules/#nestjs/common/cache/interceptors/cache.interceptor.js'
Require stack:
node_modules/#nestjs/common/cache/interceptors/cache.interceptor.js
node_modules/#nestjs/common/cache/interceptors/index.js
node_modules/#nestjs/common/cache/index.js
node_modules/#nestjs/common/index.js
node_modules/#nestjs/testing/testing-module.builder.js
node_modules/#nestjs/testing/test.js
node_modules/#nestjs/testing/index.js
src/bookmark/test/integration/bkm.service.int-spec.ts
at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:491:11)
at Object.<anonymous> (node_modules/#nestjs/common/cache/interceptors/cache.interceptor.js:5:16)
My package.json has rxjs defined in the "dependencies" section with a version of ^7.2.0.
My test in the *.spec.ts testing class looks like this:
describe('BookmarkService Integration', () => {
let prisma: PrismaService;
let bookmarkService: BookmarkService
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile();
prisma = moduleRef.get(PrismaService);
bookmarkService = moduleRef.get(BookmarkService);
await prisma.cleanDatabase();
});
it.todo('Default test pass')
})
I have not tried explicitly doing a npm install of rxjs since I don't understand the consequence of installing it when it already came as a default dependency when I initialized my Nest application. Any help would be greatly appreciated.
Thanks
Cannot find module is a common error that is thrown when a dependency requested by your project code or by one of the imported modules cannot find the target module inside the node_module directory.
This issue can be caused by a multitude of scenarios, but going by your response to my comment, it was likely caused by the fact that npm had cached the package-lock.json properties and upon rerunning npm install would still add incorrect modules to the node_modules directory. Hence the resolution is a simple deletion of the node_modules directory, the package-lock.json file and the rerunning of the npm install command.
To answer your question on how this was still a problem even when package-lock.json did contain the rxjs library reference, there could have been a number of scenarios that I would have to speculate; for one, what you were inspecting could have been a transitive rxjs dependency which is being used by another library and is not the rxjs library that is actually being requested for in the error. Alternatively, there is a chance that even though rxjs was defined in the package-lock.json file, it may not have actually been present in the node_modules directory as it may have failed to install. This really depends on the exact sequence of things that you have done prior to seeing the issue, but I hope my explanation clarifies the problem you were facing.
I am currently trying to develop an app using Ionic 2 and Angular 2 with Typescript version. I decided to use the library amqp-ts to include messaging in my app. I installed the library via npm like:
npm install amqp-ts
Everything went fine and now I've got something like this:
/ app root directory
+ node_modules
- amqp-ts
- lib
- amqp-ts.d.ts
- node_modules
- amqplib
- bluebird
- winston
The problems begin now: I import the library into my component as it is done in the example of the documentation...
import * as Amqp from "amqp-ts";
... and when I try to deploy the app I get the next error messages:
TypeScript error: C:/APPs/Test/Ionic2Angular2App/node_modules/amqp-ts/lib/amqp-ts.d.ts(2,26): Error TS2307: Cannot find module 'bluebird'.
TypeScript error: C:/APPs/Test/Ionic2Angular2App/node_modules/amqp-ts/lib/amqp-ts.d.ts(50,12): Error TS2304: Cannot find name 'Buffer'.
1. The line related to the first error message
// exported Typescript type definition for AmqpSimple
import * as Promise from "bluebird";
[...]
2. The line related to the second error message (same file: amqp-ts.d.ts)
export class Message {
content: Buffer;
[...]
}
I hope you can help me, please.
Additionally to regular package install you need to install TypeScript typings. Typings are like header files, they contain all methods/classes/interfaces definitions.
To install typings you need a typings tool. The best way is to install it globally so you can use it in every project
npm install typings --global
Then installing new typings inside your project is pretty simple, first search for library:
typings search bluebird
Install it :
typings install --save bluebird
More info : https://github.com/typings/typings
I too faced same issue, but for me above answer is not working.
While simply running :
npm i bluebird
resolving the issue
I am attempting to install localForage into a node.js application (with Angular) and Browserify.
Here is a link to the localForage documentation
It appears that using localForage and angular-localForage causes a problem with browserify based around the use of 'require'
If I require the localforage.js file in the src file I get the following error:
Warning: module "promise" not found from "/Users/mgayhart/Sites/epson- receipts/bower_components/localforage/src/localforage.js" Use --force to continue.
If I require the localforage.js file in the dist file, I get the following error:
Warning: module "./drivers/indexeddb" not found from "/Users/mgayhart/Sites/epson- receipts/bower_components/localforage/dist/localforage.js" Use --force to continue.
Anyone know of a workaround to be able to move forward with these libraries?
There is an issue on github with this problem: https://github.com/ocombe/angular-localForage/issues/26
I'll be working on it soon, you can subscribe to the github notifs on this issue to know when it's fixed !
For me installing it through bower and using it with browserify-shim worked. So in package.json:
"browser": {
"localforage":"./src/lib/vendor/localforage/dist/localforage.min.js"
},
"browserify-shim": {
"localforage":"localforage"
}
And to expose it as an angular-service (if you don't want to use angular-localforage):
app.factory "localforage",-> require 'localforage'
I've just been having this issue myself tonight, but I think I found a fix.
Instead of trying to get the bower modules to work with browserify, why not just use npm like its made for?
npm install localforage
and then when you use require you don't have to give it the path
but it still didn't work for me until I copied the folder:
localforage/src/drivers TO localforage/dist/drivers
Then it found all the dependencies and worked like a champ!
Alternatively if you must use bower you could try to use the debowerify tranform w/ gulp:
https://github.com/eugeneware/debowerify