2

I'm trying to separate my routes to a separate module in routes.js and then importing in app.js. I'm getting a lot of errors in the console.

internal/modules/esm/default_resolve.js:96 let url = moduleWrapResolve(specifier, parentURL); ^ Error: Cannot find module /Users/rhoxh/Desktop/24/routes imported from /Users/rhoxh/Desktop/24/app.js at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:96:13) at Loader.resolve (internal/modules/esm/loader.js:73:33) at Loader.getModuleJob (internal/modules/esm/loader.js:147:40) at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:41:40) at link (internal/modules/esm/module_job.js:40:36) { code: 'ERR_MODULE_NOT_FOUND' }

routes.js

import express from 'express';

const router = express.Router();

router.get('/', (req, res) => {
  res.send('home page');
});

export default router;

app.js

import express from 'express';
import { router } from './routes';

const app = express();
const PORT = 8080;

app.listen(PORT, () => {
  console.log(`Server running at: http://localhost:${PORT}/`);
});

// Routes
app.use('/', router);

What am I doing wrong here?

11
  • 1
    Have you tried to change import { router } from './routes'; to import router from './routes.js'; ? Commented Jan 25, 2020 at 22:14
  • which version of node do you use? Do you use a bundler or babel? Commented Jan 25, 2020 at 22:14
  • 1
    try full filename './routes.js' Commented Jan 25, 2020 at 22:19
  • 1
    that's the spec of es6 modules. You have to provide full path. Commented Jan 25, 2020 at 22:23
  • 1
    with babel you don't need .js extension. But when you work directly with es6 modules in node, you have to use extensions. The same goes for using modules in browsers Commented Jan 25, 2020 at 22:25

4 Answers 4

18

You need to use the full file name:

import router from './routes.js';

From the documentation:

module-name

The module to import from. This is often a relative or absolute path name to the .js file containing the module. Certain bundlers may permit or require the use of the extension; check your environment. Only single quoted and double quoted Strings are allowed.

Sign up to request clarification or add additional context in comments.

2 Comments

No matter the reasons, this makes for an extremely frustrating development experience. Node should prioritize fixing this IMO.
2

You can check this link it could help you https://github.com/nodejs/node/issues/27408

You can try to use --es-module-specifier-resolution=node as it says.

Comments

1

For anyone looking for a typescript solution use the .js file extension of your transpiled files.

// index.ts file.
import router from './routes.js';

Comments

0

You are destructuring your import yet you are exporting as default. When you import a default there is no need for destructuring

import router from './routes';

You can use destructuring when you are using either a named export

export const router = express.Router()

or you pull out a property from the default export

export default {
  router: express.Router()
}

3 Comments

Im not using destructing anymore and still the same error
While it might be true that also no named imports should be used, the error clearly states "Cannot find module"
the file is in the same dir as app.js?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.