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

Cannot find module 'blahblahblah.js' #28

Closed
aryeharmon opened this issue Aug 28, 2016 · 25 comments
Closed

Cannot find module 'blahblahblah.js' #28

aryeharmon opened this issue Aug 28, 2016 · 25 comments

Comments

@aryeharmon
Copy link

i have the following script:
var thread = spawn(function(input, done) {
var blahblah = require('./inc/blahblahblah.js');
});
i am not being able to import modules from within the process.
Error: Cannot find module

@aryeharmon
Copy link
Author

i am able to require npm modules but not custom built modules.

@andywer
Copy link
Owner

andywer commented Aug 28, 2016

Hi @aryeharmon!

Thanks for opening the issue. One question: What exactly do you mean by "custom built modules"?

@aryeharmon
Copy link
Author

@andywer modules i have made. (not using npm modules)

@andywer
Copy link
Owner

andywer commented Aug 28, 2016

Like require('lodash') works, but require('./my-stuff') does not work?

Btw: Are you experiencing those troubles in node or in the browser (using webpack, browserify or such)?

@aryeharmon
Copy link
Author

yes, i found the issue. apparently the path inside the thread is not the same path as my app.
inside the thread __dirname does not seem to work.

@aryeharmon
Copy link
Author

in node

@andywer
Copy link
Owner

andywer commented Aug 28, 2016

Nice to hear that it's already solved. Not sure if the documentation needs to be improved though...

@pietrop
Copy link

pietrop commented Sep 20, 2016

I am having the same issue. What was the solution you found to get require('./my-stuff') work?

@aryeharmon
Copy link
Author

aryeharmon commented Sep 21, 2016

@pietrop i passed __dirname to the thread so that i can use it.

        var thread = spawn(function(input, done) {
            var someModule = require(input.__dirname + '/my-stuff.js');
            // do your stuff
        });
        thread.send({__dirname: __dirname})......

@aryeharmon
Copy link
Author

@andywer yes, i think we should update the documentation

andywer added a commit that referenced this issue Sep 21, 2016
@andywer
Copy link
Owner

andywer commented Sep 21, 2016

I totally agree! Added a FAQ section to the README: https://github.com/andywer/threads.js#faq-frequently-asked-questions

Thanks for your feedback @aryeharmon @pietrop! :)

@alejandrosofter
Copy link

Error: Cannot find module '/server/utilidades.js' .. also?

@aryeharmon
Copy link
Author

aryeharmon commented Apr 17, 2017

@alejanrosofter send a code snippet, did you pass the dirname to the worker?

@alejandrosofter
Copy link

yes this is error log:
W20170417-14:14:34.631(-4)? (STDERR) Error: Cannot find module '/server/utilidades.js'
W20170417-14:14:34.634(-4)? (STDERR) at Function.Module._resolveFilename (module.js:325:15)
W20170417-14:14:34.634(-4)? (STDERR) at Function.Module._load (module.js:276:25)
W20170417-14:14:34.635(-4)? (STDERR) at Module.require (module.js:353:17)
W20170417-14:14:34.635(-4)? (STDERR) at require (internal/module.js:12:17)
W20170417-14:14:34.636(-4)? (STDERR) at module.exports (evalmachine.:4:22)
W20170417-14:14:34.636(-4)? (STDERR) at process. (/home/cabox/workspace/app/node_modules/threads/lib/worker.node/slave.js:77:5)
W20170417-14:14:34.637(-4)? (STDERR) at emitTwo (events.js:87:13)
W20170417-14:14:34.637(-4)? (STDERR) at process.emit (events.js:172:7)
W20170417-14:14:34.638(-4)? (STDERR) at internal/child_process.js:721:12
W20170417-14:14:34.638(-4)? (STDERR) at nextTickCallbackWith0Args (node.js:489:9)
W20170417-14:14:34.639(-4)? (STDERR) at process._tickCallback (node.js:418:13)

and this is part my code:
const spawn = require('threads').spawn;

const thread = spawn(function(input, done) {
var mongo = require('mongodb-bluebird');
var someModule = require(input.__dirname + '/utilidades.js');
...

thread .send({doc:doc,hola:"see",__dirname: __dirname})

@aryeharmon
Copy link
Author

what is your project file structure? are you sure '/server/utilidades.js' exists?

@alejandrosofter
Copy link

alejandrosofter commented Apr 17, 2017

app
--->client
---->main.js
---- >views

---->server
--->main.js
---->utilidades.js
---->consultas.js
----->_generarDeuda.js

---->lib
---->collections.js
---->routes.js

i make thread from _generarDeuda.js

@andywer
Copy link
Owner

andywer commented Apr 18, 2017

Error: Cannot find module '/server/utilidades.js'. Why is there a leading /?

I guess somewhere you have a require('/server/...') instead of require('./server/...').

@alejandrosofter
Copy link

not seem require.. andywer can pass the proyect from codeanywhere (without obligation) with your email

@brennanerbz
Copy link

Tried...

const thread = spawn(function(input, done) {
	const program = require(input.__dirname + '/program.js')
});
thread.send({ __dirname: __dirname })

Getting webpack_require is not defined.

Any suggestions for how to use with webpack?

@andywer
Copy link
Owner

andywer commented May 31, 2017

Hey @brennanerbz. Yeah, that won't work.

You can create a small separate bundle for program.js using webpack and pass this file to spawn, though.

@brennanerbz
Copy link

@andywer is there any way to not create a separate bundle?

@andywer
Copy link
Owner

andywer commented May 31, 2017

@brennanerbz I don't think there is a way around it. Since the function you pass to spawn() will be run in another execution context, you cannot access identifiers from outside the function, incl. webpack_require .

Sorry for the bad news 😕😉

@brennanerbz
Copy link

@andywer I removed webpack as a dependency, and it works now.

Also, do you have any links to projects using this in the wild? I want to see a more complete example.

@andywer
Copy link
Owner

andywer commented Jun 1, 2017

@brennanerbz Not really, but you can check out https://www.npmjs.com/browse/depended/threads

@andywer
Copy link
Owner

andywer commented Jun 1, 2017

PS: Nice to see you got it working :) What do you mean by removed webpack as a dependency?

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

No branches or pull requests

5 participants