migrate-mongo
migrate-mongo copied to clipboard
Unable to run on the deployed instance
Describe the bug
Migration works on local environment but does not on the deployed instance.
It failed stating: Error: Cannot find module '../lib/migrate-mongo'
Symlinks in node_modules/.bin
are not kept by the ci pipeline and are copies of the executable.
To Reproduce
Steps to reproduce the behavior:
Copy the executable into node_modules/.bin
to replace the symlink.
Expected behavior Should display the migration status
Additional context
- complete error trace:
npm info using [email protected]
npm info using [email protected]
npm info lifecycle [email protected]~premigrate:status: [email protected]
npm info lifecycle [email protected]~migrate:status: [email protected]
> [email protected] migrate:status /home/site/wwwroot
> migrate-mongo status -f dist/migrate-mongo-config.js
internal/modules/cjs/loader.js:892
throw err;
^
Error: Cannot find module '../lib/migrate-mongo'
Require stack:
- /home/site/wwwroot/node_modules/.bin/migrate-mongo
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)
at Function.Module._load (internal/modules/cjs/loader.js:745:27)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
at Object.<anonymous> (/home/site/wwwroot/node_modules/.bin/migrate-mongo:6:22)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/home/site/wwwroot/node_modules/.bin/migrate-mongo' ]
}
npm info lifecycle [email protected]~migrate:status: Failed to exec migrate:status script
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] migrate:status: `migrate-mongo status -f dist/migrate-mongo-config.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] migrate:status script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm timing npm Completed in 463ms
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2022-01-03T16_04_12_200Z-debug.log
- scripts in package.json
"migrate:status": "migrate-mongo status -f dist/migrate-mongo-config.js",
"migrate:undo-last": "migrate-mongo down -f dist/migrate-mongo-config.js && npm run migrate:status",
"migrate": "migrate-mongo up -f dist/migrate-mongo-config.js"
- migrate-mongo-config.ts
const { config } = require('dotenv');
const { build, get } = require('@xxx/node-config');
// For local development
config();
// Not really typscripty but required for the trick of an async export
module.exports = (async function () {
await build({
ignoreKvMapping: !!process.env.CONFIG_IGNORE_KV_MAPPING,
});
return {
mongodb: {
url: get('db.url'),
databaseName: get('db.options.dbName'),
options: get('db.options') || {},
},
// The migrations dir, can be an relative or absolute path. Only edit this when really necessary.
migrationsDir: process.env.NODE_ENV === 'local' ? 'migrations' : 'dist/migrations',
// The mongodb collection where the applied changes are stored. Only edit this when really necessary.
changelogCollectionName: 'changelog',
// The file extension to create migrations and search for in migration dir
migrationFileExtension: process.env.NODE_ENV === 'local' ? '.ts' : '.js',
// Enable the algorithm to create a checksum of the file contents and use that in the comparison to determin
// if the file should be run. Requires that scripts are coded to be run multiple times.
useFileHash: false,
};
})();
I had a similar problem recently and it turns out the way I was zipping the deployed packages was the problem. You can preserve symbolic links by call zip
with the --symlinks
argument.
Closing this, see last comment for resolution.