obsidian-execute-code icon indicating copy to clipboard operation
obsidian-execute-code copied to clipboard

Unclear how to import packages for Typescript/Javascript

Open MarcCoquand opened this issue 1 year ago • 9 comments

Hi! I absolutely love this tool. Thank you so much for developing it.

I can't really figure out how we are supposed to import packages in typescript. It says that we can use the {pre} script to add our dependencies. I assume there we add all import x, however the runtime will complain that it can't find that package. I can not figure out what is best practice for installing the package itself.

Thank you so much again for support and for developing this tool.

MarcCoquand avatar Jan 26 '23 17:01 MarcCoquand

When you're importing, TS/JS expect you to have a node_modules directory. The plugin creates a new file for each code block, so iirc that's why it can't find them.

You could try installing the dependencies globally with npm i -g <package>; I think that will help?

Sorry :)

chlohal avatar Jan 27 '23 17:01 chlohal

I tried adding it globally, but it didn't seem to work.

const _ = require('lodash')

Error: Cannot find module 'lodash'
Require stack:
- <repl>
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:101:18)
    at eval (eval at <anonymous> (REPL6:1:7), <anonymous>:4:11)
    at REPL6:1:7
    at Script.runInContext (vm.js:144:12)
    at REPLServer.defaultEval (repl.js:568:29)
    at bound (domain.js:421:15)
    at REPLServer.runBound [as eval] (domain.js:432:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '<repl>' ]

after successfully running npm i -g lodash

MarcCoquand avatar Feb 06 '23 16:02 MarcCoquand

I can confirm also that it works when I try it in a regular node REPL in a terminal.

which node
/Users/me/.nvm/versions/node/v14.20.0/bin/node

which is the same as node path I've set in my Obsidian code execute config 🤔

/Users/me/.nvm/versions/node/v14.20.0/bin/node

MarcCoquand avatar Feb 08 '23 18:02 MarcCoquand

Hey! I am running into the same issue. Maybe it has something to do with nvm? I am also using nvm and Obsidian Code Blocks don't find any packages that were installed.

JustinS1209 avatar Feb 14 '23 15:02 JustinS1209

Hi everyone,

I have the suspicion that this problem is connected to the fact that we currently do not allow access to the path variable. We are planning to add path, maybe this will fix it.

twibiral avatar Feb 24 '23 11:02 twibiral

@MarcCoquand and @JustinS1209 can you try to update the plugin and then try it again? With release 1.7.1 the JS notebook mode can access the environment variables. This may fix it. Tell me how it worked, please.

twibiral avatar Feb 27 '23 23:02 twibiral

@twibiral

I have similar issues but with Python. Should I open a new issue or you are working on paths within this one?

e.g. when my python code has importing it fails:

No module named 'gi'

hvitis avatar Apr 28 '23 16:04 hvitis

Python imports work completely fine for me. Are you still having issues with them?

If the javascript/typescript import issue is resolved as well, then this issue could be closed.

mayurankv avatar Jun 05 '23 20:06 mayurankv

Are you still having issues with them?

Just checked and still have issues

pereirfe avatar Sep 13 '23 00:09 pereirfe