Iman
Iman

Reputation: 829

migration-mongo not treated as common js

I have a nodejs server part in my project in which I have defined migre-mongo. My type in package.json is module. Running the migration leads to

> migrate-mongo  up && node node.js
ERROR: module is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/home/igharib/IdeaProjects/sdx-licence-manager/server/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension. ReferenceError: module is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/home/igharib/IdeaProjects/sdx-licence-manager/server/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///home/igharib/IdeaProjects/sdx-licence-manager/server/migrate-mongo-config.js:34:1
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async Object.read (/usr/local/lib/node_modules/migrate-mongo/lib/env/config.js:69:30)

And when I change the config extention into csj as recommanded I get file not found!

ERROR: Cannot find module '/home/igharib/IdeaProjects/sdx-licence-

manager/server/migrate-mongo-config.js'
Require stack:
- /usr/local/lib/node_modules/migrate-mongo/lib/utils/module-loader.js
- /usr/local/lib/node_modules/migrate-mongo/lib/env/config.js
- /usr/local/lib/node_modules/migrate-mongo/lib/env/migrationsDir.js
- /usr/local/lib/node_modules/migrate-mongo/lib/actions/init.js
- /usr/local/lib/node_modules/migrate-mongo/lib/migrate-mongo.js
- /usr/local/lib/node_modules/migrate-mongo/bin/migrate-mongo.js Error: Cannot find module '/home/igharib/IdeaProjects/sdx-licence-manager/server/migrate-mongo-config.js'

My package json:

   {
  "name": "server",
  "version": "0.8.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "react-scripts test",
    "start": "NODE_ENV=production node app.js",
    "start:migrate": "migrate-mongo  up && node node.js",
    "start:win": "SET NODE_ENV=production && node app.js",
    "start:srv": "node index.js",
    "dev": "nodemon --watch src/server/ app.js",
    "backend:test": "jest server --runInBand --forceExit --detectOpenHandles --verbose",
    "backend:test-coverage": "jest server --collect-coverage --runInBand --forceExit --detectOpenHandles --verbose",
    "lint:server": "eslint server app.js"
  },
  "author": "Iman",
  "license": "",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "express-session": "^1.17.2",
    "fetch": "^1.1.0",
    "jwt-simple": "^0.5.6",
    "mongoose": "^5.13.2",
    "nodemon": "^2.0.10"
  }
}

created migrate config:

// In this file you can configure migrate-mongo

const config = {
  mongodb: {
    url: "mongodb://localhost:27017",

    databaseName: "ldb",

    options: {
      useNewUrlParser: true, // removes a deprecation warning when connecting
      useUnifiedTopology: true, // removes a deprecating warning when connecting
      //   connectTimeoutMS: 3600000, // increase connection timeout to 1 hour
      //   socketTimeoutMS: 3600000, // increase socket timeout to 1 hour
    }
  },

  // The migrations dir, can be an relative or absolute path. Only edit this when really necessary.
  migrationsDir: "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: ".js",

  // Enable the algorithm to create a checksum of the file contents and use that in the comparison to determine
  // if the file should be run.  Requires that scripts are coded to be run multiple times.
  useFileHash: false,

  // Don't change this, unless you know what you're doing
  moduleSystem: 'commonjs',
};

module.exports = {config};

How can I solve the problem without changing the type=module?

Node version 16.15.0

Upvotes: 1

Views: 1315

Answers (1)

Nuno Aparício
Nuno Aparício

Reputation: 41

Since you are using '"type": "module"', in your migrations config file you should change the last line:

module.exports = {config};

to:

export default config;

That should solve the issue

Upvotes: 3

Related Questions