Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local node module (not npm registry) does not load with --node_modules-dir flag #18474

Open
abflow opened this issue Mar 28, 2023 · 11 comments
Open
Labels
feat new feature (which has been agreed to/accepted) node compat

Comments

@abflow
Copy link

abflow commented Mar 28, 2023

I have a node module test_node which is locally installed within the deno project into a node_modules folder via:

npm install --save ../test_node

package.json:

{
  "dependencies": {
    "test_node": "file:../test_node"
  }
}

When I run with the node-modules-dir flag:
deno run -A --node-modules-dir index.js

I get the following error:
error: npm package 'test_node' does not exist

@abflow
Copy link
Author

abflow commented Mar 28, 2023

Ok, I had previously used into my index.js file:

import Test from "npm:test_node";

But now, when I simply use:
import Test from "test_node";

this time, I get the following error:

error: Parsing version constraints in the application-level package.json is more strict at the moment.
Not implemented scheme 'file'

@abflow abflow mentioned this issue Mar 28, 2023
13 tasks
@abflow abflow changed the title Local node module (not npm) does not load with --node_modules-dir flag Local node module (not npm registry) does not load with --node_modules-dir flag Mar 28, 2023
@abflow
Copy link
Author

abflow commented Mar 28, 2023

One workaround could be to use the import map for loading a local node module without using npm install.

That would be possible if deno could somehow deduce internally from the import map value that it has to deal with a node module:
deno.json

{
	"imports": {
		"npm:test_node": "../test_node/index.js"
	}
}

error: Uncaught SyntaxError: The requested module 'npm:test_node' does not provide an export named 'default'

{
	"imports": {
		"npm:test_node": "../test_node"
	}
}

error: Accès refusé. (os error 5)

{
	"imports": {
		"npm:test_node": "../test_node/"
	}
}

error: Module not found

@abflow
Copy link
Author

abflow commented Mar 28, 2023

Though it is confusing from a node.js perspective, this is a minor issue, so I close it.

@abflow abflow closed this as completed Mar 28, 2023
@jsejcksn
Copy link
Contributor

jsejcksn commented Apr 7, 2023

@abflow I didn't find anything indicating that this has been resolved, so I don't think it should be closed — AFAICT, Deno can't yet load Node modules from the local filesystem.

Ref: https://deno.land/[email protected]/node/package_json

Repro:

Local Node module

./local_tools/package.json:

{
  "name": "local_tools",
  "version": "0.1.0",
  "type": "module",
  "main": "index.mjs"
}

./local_tools/index.mjs:

export { default as shout } from "./shout.mjs";

./local_tools/shout.mjs:

/** @param {string} text */
function shout(text) {
  console.log(text.toUpperCase());
}

export default shout;

Deno app

./deno_app/package.json:

{
  "name": "deno_app",
  "version": "0.1.0",
  "dependencies": {
    "local_tools": "file:../local_tools"
  }
}

./deno_app/main.ts:

import { shout } from "local_tools";

shout("hello world");

In the terminal

% pwd           
/Users/deno/example/deno_app

% deno --version
deno 1.32.3 (release, aarch64-apple-darwin)
v8 11.2.214.9
typescript 5.0.3

% deno run main.ts
error: Parsing version constraints in the application-level package.json is more strict at the moment.

Not implemented scheme 'file'
    at file:///Users/deno/example/deno_app/main.ts:1:23

@denizdogan
Copy link

I agree with @jsejcksn, and I don't think this is a minor issue, I'd say it's a pretty major issue, especially considering #18616. It seems that Deno can handle neither private npmjs packages nor local Node.js packages.

@abflow
Copy link
Author

abflow commented Apr 7, 2023

@jsejcksn @denizdogan You're right... sorry about that. I reopen the issue.

@festusyuma
Copy link

Hello, is there any update on this issue. Thank you

@dsherret
Copy link
Member

No, there was a bit of work done on the patch feature for JSR (#25068), but not for npm yet (I tried, but cut it out of the scope of the initial implementation).

@maheshbansod
Copy link

Are there any work-arounds? - this would be helpful for testing/debugging an NPM library with a deno project locally.

@SebastienGllmt
Copy link

SebastienGllmt commented Nov 25, 2024

I found a hacky workaround for this

  1. Set "nodeModulesDir": "auto", in your root deno.json (this will get a node_modules folder generated)
  2. Place your local dependency somewhere on your disk (say file path ./my-dep)
  3. Create a symlink from your folder to your node_modules

Here is an example of a deno task you can run to create the symlink assuming you placed the local dependency at the root of your deno project

"link:my-dep": "bash -c 'if ! [ -L ./node_modules/my-dep ]; then ln -s ../my-dep ./node_modules/my-dep; fi'",

WARNING: this may not always work. Notably, it has two downsides:

  1. You will have to relink if you ever clear your node_modules (I don't think there is a good way to do this automatically for a local folder given deno's lack of non-npm postinstall script support)
  2. This won't update your deno.lock (although presumably fewer things depend on this file being accurate)

@DreamwareDevelopment
Copy link

Trying to use Deno in Nx, total nightmare, thought this may help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat new feature (which has been agreed to/accepted) node compat
Projects
None yet
Development

No branches or pull requests

9 participants