6

Is it possible to copy a set of NPM installed files and associated files from a Mac computer to a Windows computer, and for all those files to work?

For example, transfering Node.js files with some other NPM files from Mac to Windows, then running node app.js in that directory (on the Windows Command Prompt).

Thanks! :)

4
  • 3
    What do you mean by "transfering Node.js files with some other NPM files"? Do you mean pieces or the actual npm and node.js installation or stuff from your dependencies? Either way, you could run into issues especially if one of you modules uses some sort of node-gyp module which could potentially make it platform and architecture dependent. That's the whole point of the package manager, to encapsulate these issues and handle them for you. What are you trying to do?
    – zero298
    Feb 6, 2016 at 7:57
  • @zero298 - By transferring I meant emailing a folder of the project files. I tried deleting the node-modules folder, and using npm install. The error I got was: [Error: %1 is not a valid Win32 application. \\?\D:\~\node_modules\mongodb\node_modules\bson\build\Releas e\bson.node] js-bson: Failed to load c++ bson extension, using pure JS version fs.js:844 return binding.stat(pathModule._makeLong(path));
    – Luke
    Feb 6, 2016 at 8:10
  • yes the npm binary itself is dependent, and the downloads it does are are also dependent. You cannot copy one npm instance from a PC to a MAC it will not work, or commit node_modules and use in another system, it won't work either Feb 6, 2016 at 8:26
  • Why do you want to do this?
    – user663031
    Feb 6, 2016 at 11:47

2 Answers 2

9

The binary, npm, that you install is platform dependent, as is node.js. That's why there are different releases for each platform available on the download site.

For the most part, your project files are platform independent. For example, most of your JavaScript files will all be used by node.js and work just fine without having to worry about what platform you are on because the system details will be dealt with by node.js itself.

However, some modules are platform dependent. For example, anything that uses node-gyp will try to compile on your platform whenever the module is installed by npm. You do not have to worry about that though because it is handled by npm, that's why you're using a package manager.

Copying node_modules can be done; but it's more often than not better and easier to just run npm i on whatever machine is going to be running your application. You can avoid having to worry about version problems using something like npm shrinkwrap which will lock down the version of a package that your module depends on.

5

NPM packages that contain native addons/dependencies are tied to the OS + NodeJS version and have to be rebuilt specifically for the system you intend to use them on. This is why you're seeing the error mentioning bson.node, it is a native addon that has to be rebuilt, this can be done with the npm rebuild command.

2
  • I tried npm rebuild, but it came back with > [email protected] install ~\node_modules\mongodb\node_modules \bson > (node-gyp rebuild 2> builderror.log) || (exit 0) ~\node_modules\mongodb\node_modules\bson>if not defined n pm_config_node_gyp (node "C:\Program Files\nodejs\node_modules\npm\bin\node-gyp- bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild ) else (node rebuild ) Terminate batch job (Y/N)?
    – Luke
    Feb 6, 2016 at 8:28
  • Make sure you've setup your Windows machine to build native addons: github.com/Microsoft/nodejs-guidelines/blob/master/… Feb 6, 2016 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.