Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

make npm.load re-evaluate the cwd each time it is called #9059

Closed
trusktr opened this issue Jul 25, 2015 · 4 comments
Closed

make npm.load re-evaluate the cwd each time it is called #9059

trusktr opened this issue Jul 25, 2015 · 4 comments

Comments

@trusktr
Copy link

trusktr commented Jul 25, 2015

I'd like to change the cwd of npm after it's been loaded. For example, I've got something like this, where I run npm.load and npm.command.install twice, Meteor.wrapAsync just causes the execution to be "synchronous":

            fs.existsSync(path.resolve(DIR_ONE, 'node_modules')) // false
            fs.existsSync(path.resolve(DIR_TWO, 'node_modules')) // false

                process.chdir(DIR_ONE)
                Meteor.wrapAsync((callback) =>
                    npm.load({}, () => callback(null, 'blah'))
                )()
                Meteor.wrapAsync((callback) => {
                    npm.commands.install([], () => callback(null, 'blah'))
                })()

            fs.existsSync(path.resolve(DIR_ONE, 'node_modules')) // true
            fs.existsSync(path.resolve(DIR_TWO, 'node_modules')) // false
            shell.exec('rm -rf '+path.resolve(DIR_ONE, 'node_modules')) // start over
            fs.existsSync(path.resolve(DIR_ONE, 'node_modules')) // false
            fs.existsSync(path.resolve(DIR_TWO, 'node_modules')) // false

                process.chdir(DIR_TWO)
                Meteor.wrapAsync((callback) =>
                    npm.load({}, () => callback(null, 'blah'))
                )()
                Meteor.wrapAsync((callback) => {
                    npm.commands.install([], () => callback(null, 'blah'))
                })()

            fs.existsSync(path.resolve(DIR_ONE, 'node_modules')) // true again
            fs.existsSync(path.resolve(DIR_TWO, 'node_modules')) // still false

The second time, npm doesn't install inside of DIR_TWO as expected. How can we change npm's cwd?

@trusktr
Copy link
Author

trusktr commented Jul 25, 2015

I see that Meteor uses child_process to manipulate the CLI version on NPM. Maybe that's what I'll have to do. It would be cool to easily change npm dir programmatically though.

@trusktr
Copy link
Author

trusktr commented Jul 25, 2015

Aha, found the solution that (as far as my case goes) works:

            Meteor.wrapAsync((callback) => {
                npm.load({ prefix: DIR_ONE }, callback)
            })()
            Meteor.wrapAsync((callback) => {
                npm.commands.install(DIR_ONE, [], callback)
            })()

...

            Meteor.wrapAsync((callback) => {
                npm.load({ prefix: DIR_TWO }, callback)
            })()
            Meteor.wrapAsync((callback) => {
                npm.commands.install(DIR_TWO, [], callback)
            })()

Now, I don't know if the prefix value is changing between these two npm installs, but luckily both prefixes contain the same package.json, so even if the second location get installed based on the first prefix, it works out.

@trusktr trusktr closed this as completed Jul 25, 2015
@trusktr
Copy link
Author

trusktr commented Jul 25, 2015

Actually, re-opening, as I think that the second call to npm.load should re-evaluate the cwd.

@trusktr trusktr reopened this Jul 25, 2015
@trusktr trusktr changed the title How do we change npm's cwd after it's loaded? [suggestion] Make npm.load re-evaluate the cwd each time it is called. Jul 25, 2015
@othiym23 othiym23 changed the title [suggestion] Make npm.load re-evaluate the cwd each time it is called. make npm.load re-evaluate the cwd each time it is called Jul 27, 2015
@othiym23
Copy link
Contributor

The current programmatic API for npm is unsupported for use outside itself, and there are no guarantees that it will remain stable or has any particular fitness for purpose. If you want to invoke multiple npm commands from another program, the best thing to do is to call npm as a child process twice. At some point, we'll have a better interface for using the installer programmatically, but we're not there yet. As such, I'm closing this.

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

No branches or pull requests

2 participants