Search code examples
node.jstypescriptdiscord.jssodium

When trying to use npm install I get a "msvsVersion is not defined" error on a discord bot


I've been trying to self-host a TypeScript discord bot, but the setup process has been nothing but confusing. I think it's supposed to create a build directory with an index.js file, but I'm not sure. I've installed Visual Studio Build Tools 2017 because it seems to need them, but running npm install gives this error:

> [email protected] preinstall C:\Users\fang2\Documents\Coding\Discord Bots\Myu-Bot-master\node_modules\sodium
> node install.js --preinstall

MS Version: 2017
C:\Users\fang2\Documents\Coding\Discord Bots\Myu-Bot-master\node_modules\sodium\install.js:312
    console.log('Invalid msvs_version ' + msvsVersion + '\n');
                                          ^

ReferenceError: msvsVersion is not defined
    at errorInvalidMSVSVersion (C:\Users\fang2\Documents\Coding\Discord Bots\Myu-Bot-master\node_modules\sodium\install.js:312:43)
    at checkMSVSVersion (C:\Users\fang2\Documents\Coding\Discord Bots\Myu-Bot-master\node_modules\sodium\install.js:329:9)
    at Object.<anonymous> (C:\Users\fang2\Documents\Coding\Discord Bots\Myu-Bot-master\node_modules\sodium\install.js:353:5)
    at Module._compile (node:internal/modules/cjs/loader:1092:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] preinstall: `node install.js --preinstall`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] preinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\fang2\AppData\Roaming\npm-cache\_logs\2021-06-01T04_30_07_597Z-debug.log

I've tried both the 2019 build tools and the 2017 build tools, and tried setting the msvs version manually, but nothing seems to have worked. Any help would be greatly appreciated.


Solution

  • I am also attempting to get sodium working for the purposes of a Discord bot and spent a considerable amount of time failing to build it. At the time of writing sodium only supports msvs_version configured to 2010, 2012, 2013 and 2015.

    The following managed to work for me:

    1. Make sure 2015 Visual Studio Build Tools is installed.
    2. Install the Windows 8.1 SDK (it appears the node-addon-api module sodium is dependent on requires this). Microsoft maintains an archive of their SDKs here. Alternatively, you can install it from the optional drop-down menu in the Visual C++ build tools section from the 2017 Build Tools installer. Here is a screenshot of the menu for clarification.
    3. In your Windows environment variables, add VCTargetsPath and set it to C:\Program Files (x86)\MSBuild\Microsoft.cpp\v4.0\v140. Source
    4. Run npm config set msvs_version 2015
    5. Now sodium will build when running npm install sodium or npm install if already listed as a dependency of your project.

    Stuff worth mentioning:

    Step 3 prevents the error MSB4019: The imported project "C:\Microsoft.Cpp.Default.props" was not found. from throwing and failing the build. From my project folder it traces to node_modules\sodium\node-addon-api\nothing.vcxproj(20,3)

    A lot of resources online recommend installing windows-build-tools with npm install --global --production windows-build-tools --vs2015 to get this build to succeed but as of February 2020, the Node.js installer now includes build tools for Windows as an option. This should only be relevant if you don't already have 2015 build tools installed.

    Hope I was able to help.