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. 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.