0

In my Nest js app, i want to add libraries which doesnt have any nest specific implementation. For example sharp or jimp.

I have added these in my project like

npm i jimp.

and imported in my code like this.

import Jimp from 'jimp';

It works fine when i build and serve locally, but when i serve it from docker and build production version, i get the following error :

> planiac@0.0.0 serve /app
backend_1  | > node dist/apps/api/main.js
backend_1  | 
backend_1  | internal/modules/cjs/loader.js:965
backend_1  |   throw err;
backend_1  |   ^
backend_1  | 
backend_1  | Error: Cannot find module 'jimp'
backend_1  | Require stack:
backend_1  | - /app/dist/apps/api/main.js
backend_1  |     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:962:15)
backend_1  |     at Function.Module._load (internal/modules/cjs/loader.js:838:27)
backend_1  |     at Module.require (internal/modules/cjs/loader.js:1022:19)
backend_1  |     at require (internal/modules/cjs/helpers.js:72:18)
backend_1  |     at Object.<anonymous> (/app/dist/apps/api/main.js:2687:18)
backend_1  |     at __webpack_require__ (/app/dist/apps/api/main.js:20:30)
backend_1  |     at Object.<anonymous> (/app/dist/apps/api/main.js:1874:62)
backend_1  |     at __webpack_require__ (/app/dist/apps/api/main.js:20:30)
backend_1  |     at Object.<anonymous> (/app/dist/apps/api/main.js:3423:73)
backend_1  |     at __webpack_require__ (/app/dist/apps/api/main.js:20:30) {
backend_1  |   code: 'MODULE_NOT_FOUND',
backend_1  |   requireStack: [ '/app/dist/apps/api/main.js' ]
backend_1  | }
Shofiqul Alam
  • 585
  • 1
  • 7
  • 29

3 Answers3

1

Try this instead of import

const jimp = reuire("jimp")
0

In your Docker, it looks like jimp is not loaded in your node_modules. Try running npm i jimp with the --save option, so that the package is saved into package-lock.json, and make sure your Docker build runs npm install (which it probably already does since you are using Nest)

Terenoth
  • 2,458
  • 1
  • 14
  • 21
0

When you just have the require, like padmakar stated, you won't have the types included. If you want to have type intellisense:

import { default as jimpi } from 'jimp';
const Jimp = require('jimp') as typeof jimpi;
Don
  • 366
  • 1
  • 10