Adding new dependency to local development isnt successfull

I’m trying to add dotenv dependency to my node app

 "dependencies": {
        "dotenv": "^16.0.3",
        "express": "^4.18.1",
        "nodemon": "^2.0.18"
nodemon index.js

is successful and can reach my server and get expected results
but npm start says me somthing like this

Error [ERR_MODULE_NOT_FOUND]: Cannot find package ‘dotenv’ imported from /usr/src/app/index.js

did i miss something?

Might be a silly question, but have you done npm install after updating the package.json file?

i dont think that’s necessary - i performed npm i dotenv
part of yarn.lock

  "integrity" "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ=="
  "resolved" ""
  "version" "16.0.3"

npm start works fine localy but not within okteto up

I suspect you ran npm i … locally on your computer?

So, stop the yarn start process and run the npm install in the shell of the container, to install the missing dotenv dependency. After that I believe npm start should work?

Note that npm install is run when the container is first created/setup per Dockerfile (e.g. as can be seen in the node-getting-started example), and by default event locally installed dependencies should be synced to the dev container. But if you have node_modules listed in the .stignore file it would explain why the local install didn’t get reflected in the dev container. See docs for more information how that works: File Synchronization | Okteto Documentation

Also keep in mind that some dependencies may have pre/post-install builds that are platform dependent, e.g. if npm install builds something locally for Apple/M1 architecture and dev container is running some Linux variant it would not work. In that scenario it may not be desirable to sync node_modules (ignore it through .stignore) and run the install separately somehow in the dev container instead. Either manually or as a prestart script, for example.

Hope that helps!

1 Like

thank you so much! im really newbie when it comes to Kuber. now it works