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

--require flag does not load from node_modules #257

Closed
jonaskello opened this issue Dec 28, 2016 · 5 comments
Closed

--require flag does not load from node_modules #257

jonaskello opened this issue Dec 28, 2016 · 5 comments
Labels

Comments

@jonaskello
Copy link
Contributor

jonaskello commented Dec 28, 2016

As added in #181 ts-node accepts a --require flag. When trying to use this with the tsconfig-paths package it fails to locate the package. Example:

$ ts-node -r tsconfig-paths main.ts 
Error: Cannot find module 'tsconfig-paths'

The reason seems to be this line which does Module._load(id) in order to require the modules specified by --require. If it is changed to require(id)it will locate the package. It seems like Module._load(id) skips loading from node_modules.

The mocha package also has a -r option and looking here it seems they use require to load the modules rather than Module._load. So this example works:

$ mocha --compilers ts:ts-node/register -r node-tsconfig-paths mocha-example.ts
@blakeembrey
Copy link
Member

We shouldn't technically be doing require as that could result in loading our own dependencies instead of the users, which would be a confusing situation. I believe this code was based on the node CLI itself here: nodejs/node#881. I'm not sure about the current implementation in node anymore, I'll need to find out how to do a better implementation.

@blakeembrey
Copy link
Member

Found it. Let's use _preload_modules found here: https://github.com/nodejs/node/blob/0fb21df6e692bef9f55b9bfa876f3c59dc590332/lib/internal/bootstrap_node.js#L391. We may need to fallback to Module._load on older node.js versions (I can't recall if this ever worked now, I'll check though).

@blakeembrey
Copy link
Member

Ok, exists all the way back to 4.0, should work well enough if you want to submit a PR. The other reason we can't use require is that it wouldn't work for relative --require ./ calls - they would resolve relative to our module instead of to the CLI execution.

@jonaskello
Copy link
Contributor Author

Nice! I'm collaborating with @Jontem on the node-tsconfig-paths package and he'll make a PR for this.

@blakeembrey
Copy link
Member

Looks like this should have been resolved with #258 IIRC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants