Meteor + Langchain + Ollama - Error

I have a Meteor installation from scratch on a M2 mac, I only installed langchain for use with ollama and I can’t get it to work, below are the steps to reproduce and log in full.

meteor create project

meteor npm i @langchain/ollama @langchain/core

my server/main.js file I only added the example from the langchain ollama documentation

import { Meteor } from 'meteor/meteor';
import { ChatOllama } from '@langchain/ollama';

Meteor.startup(async () => {
  const llm = new ChatOllama({
    model: "llama3.1",
    temperature: 0,
    maxRetries: 2,
    // other params...
  });
  
  const aiMsg = await llm.invoke([
      [
        "system",
        "You are a helpful assistant that translates English to French. Translate the user sentence.",
      ],
      ["human", "I love programming."],
    ]);
    
    console.log(aiMsg);
});

below log when running meteor:

[[[[[ ~/workspace/project ]]]]]             

=> Started proxy.                             
=> Started HMR server.                        
                                              
Unable to resolve some modules:

  "ollama/browser" in                         
/Users/robersonfaria/workspace/project/node_modules/@langchain/ollama/dist/chat_models.js
(os.osx.arm64)
                                              
If you notice problems related to these missing modules, consider running:
                                              
  meteor npm install --save ollama            
                                              
=> Started MongoDB.                           
W20241010-21:55:18.119(-3)? (STDERR) packages/core-runtime.js:189
W20241010-21:55:18.128(-3)? (STDERR)             throw error;
W20241010-21:55:18.128(-3)? (STDERR)             ^
W20241010-21:55:18.128(-3)? (STDERR) 
W20241010-21:55:18.128(-3)? (STDERR) Error: Cannot find module 'ollama/browser'
W20241010-21:55:18.128(-3)? (STDERR)     at makeMissingError (packages/modules-runtime.js:221:12)
W20241010-21:55:18.128(-3)? (STDERR)     at Module.require (packages/modules-runtime.js:240:17)
W20241010-21:55:18.128(-3)? (STDERR)     at Module.mod.require (/Users/robersonfaria/.meteor/packages/modules/.0.20.1.z1mc19.sfl2m++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/@meteorjs/reify/lib/runtime/index.js:30:33)
W20241010-21:55:18.128(-3)? (STDERR)     at Module.moduleLink [as link] (/Users/robersonfaria/.meteor/packages/modules/.0.20.1.z1mc19.sfl2m++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/@meteorjs/reify/lib/runtime/index.js:104:22)
W20241010-21:55:18.128(-3)? (STDERR)     at module (packages/modules.js:257:261)
W20241010-21:55:18.128(-3)? (STDERR)     at fileEvaluate (packages/modules-runtime.js:335:7)
W20241010-21:55:18.128(-3)? (STDERR)     at Module.require (packages/modules-runtime.js:237:14)
W20241010-21:55:18.128(-3)? (STDERR)     at Module.mod.require (/Users/robersonfaria/.meteor/packages/modules/.0.20.1.z1mc19.sfl2m++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/@meteorjs/reify/lib/runtime/index.js:30:33)
W20241010-21:55:18.128(-3)? (STDERR)     at Module.moduleLink [as link] (/Users/robersonfaria/.meteor/packages/modules/.0.20.1.z1mc19.sfl2m++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/@meteorjs/reify/lib/runtime/index.js:104:22)
W20241010-21:55:18.128(-3)? (STDERR)     at module (packages/modules.js:242:8)
W20241010-21:55:18.128(-3)? (STDERR)     at fileEvaluate (packages/modules-runtime.js:335:7)
W20241010-21:55:18.128(-3)? (STDERR)     at Module.require (packages/modules-runtime.js:237:14)
W20241010-21:55:18.128(-3)? (STDERR)     at Module.mod.require (/Users/robersonfaria/.meteor/packages/modules/.0.20.1.z1mc19.sfl2m++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/@meteorjs/reify/lib/runtime/index.js:30:33)
W20241010-21:55:18.128(-3)? (STDERR)     at Module.moduleLink [as link] (/Users/robersonfaria/.meteor/packages/modules/.0.20.1.z1mc19.sfl2m++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/@meteorjs/reify/lib/runtime/index.js:104:22)
W20241010-21:55:18.129(-3)? (STDERR)     at module (packages/modules.js:231:8)
W20241010-21:55:18.129(-3)? (STDERR)     at fileEvaluate (packages/modules-runtime.js:335:7)
W20241010-21:55:18.129(-3)? (STDERR) 
W20241010-21:55:18.129(-3)? (STDERR) Node.js v20.17.0
=> Exited with code: 1
=> Your application is crashing. Waiting for file change.

I’ve already tried following the ollama installation suggestion, I also tried installing ollama/browser, nothing works

Just for testing, I created a test project with a simple npm init and installed the same packages and ran the same code, and everything worked normally, so I believe it is a bug with meteor.

I would like to use meteor in this project, if you know how to help me, I would appreciate it.

1 Like
  1. my server/main.js file
  2. Unable to resolve some modules: “ollama/browser”
  3. From Ollama documentation: Browser Usage - To use the library without node, import the browser module.

Luckily Meteor works as expected and you seem to mix server and client components.

Hi @robersonfaria in Meteor the environments are seperated by the folders /server and /client (exact code-splitting), so if you want to use the package only in the browser then make sure it’s only imported in /client or an import that itself is imported by /client.

@jkuester and @paulishca , how exactly Meteor decides if that lib is client or server? I’m asking because that code of ollama doesn’t seem using any specific thing from a Browser. Thanks in advance.

From the Application Structure shared by @jkuester you have 2 important folders in your app root: client and server.

In the packages.json of your project you should have this:

 "meteor": {
    "mainModule": {
      "client": "client/main.js",
      "server": "server/main.js"
    },

What happens in your case, the library depends on browser features but you import it in a server folder. On the server side you can use Node features while on the client side you can use browser features. For instance, window.innerWidth cannot be called server side because the window object doesn’t exist on a Node server.

Hello, I think I found the problem but I don’t know how to solve it. Thanks in advance to those who will help. Below you will see two examples where I imported the ollama package in the client. One of them works correctly. Is there any way to parse the paths in the export object defined in package.json for meteor?

Unfortunately I can’t use the working example because the @langchain/ollama package imported it as follows. import { Ollama } from 'ollama/browser'

ex1:

import { Ollama } from 'ollama/browser'
const ollama = new Ollama()
export const ollamaClient = ollama
Unable to resolve some modules:

  "ollama/browser" in /Users/recepozen/bordo/monochat/lib/utils/app-util/client/0-app-util.js (web.browser)
                                              
If you notice problems related to these missing modules, consider running:
                                              
  meteor npm install --save ollama  

ex2:

import { Ollama } from 'ollama/dist/browser'
const ollama = new Ollama()
export const ollamaClient = ollama
// no error

ollama folders:

image

ollama package.json

{
  "name": "ollama",
  "version": "0.5.15",
  "description": "Ollama Javascript library",
  "main": "dist/index.cjs",
  "module": "dist/index.mjs",
  "types": "dist/index.d.ts",
  "exports": {
    ".": {
      "require": "./dist/index.cjs",
      "import": "./dist/index.mjs",
      "types": "./dist/index.d.ts"
    },
    "./browser": {
      "require": "./dist/browser.cjs",
      "import": "./dist/browser.mjs",
      "types": "./dist/browser.d.ts"
    },
    "./*": "./*"
  },
  "scripts": {
    "format": "prettier --write .",
    "test": "vitest --run",
    "build": "unbuild",
    "lint": "eslint ./src/*",
    "prepublishOnly": "npm run build"
  },
  "homepage": "https://github.com/ollama/ollama-js",
  "repository": {
    "type": "git",
    "url": "https://github.com/ollama/ollama-js.git"
  },
  "author": "Saul Boyd",
  "license": "MIT",
  "devDependencies": {
    "@swc/core": "^1.3.14",
    "@types/whatwg-fetch": "^0.0.33",
    "@typescript-eslint/eslint-plugin": "^5.42.1",
    "@typescript-eslint/parser": "^5.42.1",
    "eslint": "^8.29.0",
    "vitest": "^2.1.6",
    "prettier": "^3.2.4",
    "typescript": "^5.3.2",
    "unbuild": "^2.0.0"
  },
  "dependencies": {
    "whatwg-fetch": "^3.6.20"
  }
}

This isn’t a direct response to your question. However in case it may be of interest –

Ollama may be pretty big (too big?) to load on a client browser. Can you connect to it on the server? If so, it may be more efficient, and a faster home page load, to leave it on the server, and let the client talk to it via an API.

They’re using the new ESM module + Node export path stuff, you’ll probably want to set up jorgenvatle’s Vite plugin for meteor, I had to do the same to get Chakra V3 working (it depends on Ark UI which has this configuration), because by default Meteor currently doesn’t support this and gets confused by the path apparently not existing due to all the aliasing going on.

2 Likes

Actually, I did a test on the client to identify the problem. Currently, we cannot use this package on the client or server whether we want to. I already have a plan to ollama with langchain and use other packages on the server side.

To summarize, I think this new import feature is not available in meteor and it will be a problem for the meteor community as the packages that use it increase. I think there is a pr for this issue. Resolve package.json exports by zodern · Pull Request #13520 · meteor/meteor · GitHub

2 Likes

Thank you very much for your answer. Can this gear package be used with Blaze? What exactly does it change for Meteor?