While importing in node js module not found error occurring - node.js

I am using module type in node
So exported the route
as
export default router ;
But while importing as
import authRouter from './Routes/AuthRoute.js';
Module not found error occurs

Related

Using express-routemagic es6

I want to use express-routemagic with es6 syntax (import). My approach so far goes like this:
Router.js
import express from "express";
import homepageRouter from "./homepage_router.js";
import usersRouter from "./users_router.js";
const app = express();
app.use('/', homepageRouter);
app.use(process.env.pathUrl, usersRouter);
export default app;
app.js
import magic from 'express-routemagic';
magic.use(app);
I want to note that, so far I've built the application using es5 and it worked fine. When I migrate it to es6, I get the error:
node:internal/process/esm_loader:94
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/grigorisbachtsevanos/Documents/EaaS-platform-1/routes/index.js from /Users/grigorisbachtsevanos/Documents/EaaS-platform-1/node_modules/express-routemagic/index.js not supported.
Instead change the require of /Users/grigorisbachtsevanos/Documents/EaaS-platform-1/routes/index.js in /Users/grigorisbachtsevanos/Documents/EaaS-platform-1/node_modules/express-routemagic/index.js to a dynamic import() which is available in all CommonJS modules.'.
I also have added type: module to my package.js. How should I approach this?

Nodejs (Next) Cannot import object from module: Module not found: Can't resolve 'fs'

I have nodejs app using react and nextjs. The most important pieces of the app are kept in the server file.
main.js(server file)
const app = next({ dev }) //dev = true
...
const DR_LINK = "anything"
module.exports.app = app;
module.exports.DR_LINK = DR_LINK;
Now I need the app object for some of my routers.
//any router file
const {app} = require("../main")
And it works perfectly fine.
But when I try to import the string DR_LINK object
import {DR_LINK} from "../main";
//not in the same file as the import for app
I get this error
error - ./node_modules/destroy/index.js:14:0
Module not found: Can't resolve 'fs'
Import trace for requested module:
./node_modules/send/index.js
./node_modules/express/lib/response.js
./node_modules/express/lib/express.js
./node_modules/express/index.js
./main.js
./components/CreateForm.js
./pages/create.js
https://nextjs.org/docs/messages/module-not-found
error - unhandledRejection: OverwriteModelError: Cannot overwrite `Declaration` model once compiled.
//for some reason it affects the db models too
The export order or exporting it like
module.exports = { app, DR_LINK};
makes not difference. The app is allways exported but the string object is not. This only happens when app object is exported in any way from the server file. By removing it, everything works fine.
I think you are mixing up server side and client side code. My guess is you are trying to doimport {DR_LINK} from "../main"; on client side from a server file. The clue is the error Module not found: Can't resolve 'fs'. This is because fs is a built in node.js (server side) package, so your frontend will not know what it is.
I suspect your router files are still server side, hence why the imports (require) work there.
You can define env variables inside next config like so:
https://nextjs.org/docs/api-reference/next.config.js/environment-variables
Or maybe this could also be an option for you as well:
https://nextjs.org/docs/basic-features/environment-variables#exposing-environment-variables-to-the-browser

Import ES Module

I am trying to create a standalone node.js project. The steps I followed are -
Created a new directory and initialised it with npm init.
Installed the new module for node-fetch.
Trying to import the fetch module using const fetch = require("node-fetch"); statement.
Getting the following error -
const fetch = require("node-fetch");
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/jatin/Desktop/test-app/node_modules/node-fetch/src/index.js from /Users/jatin/Desktop/test-app/index.js not supported. Instead change the require of /Users/jatin/Desktop/test-app/node_modules/node-fetch/src/index.js in /Users/jatin/Desktop/test-app/index.js to a dynamic import() which is available in all CommonJS modules. at Object.<anonymous> (/Users/jatin/Desktop/test-app/index.js:2:15) { code: 'ERR_REQUIRE_ESM' }
The node version I have on my machine is - v16.9.0.
You should use an ES module import instead of require.
import * as fetch from 'node-fetch';
You could also use a dynamic import, as the error message states.
const fetch = import('node-fetch');

Issue With Trying to Import a Local File in Node v14.15.1

I am trying to import a local file from my project's directory to my server.js. The directory structure is like this:
server
node_modules
server.js (error occurs here)
src
models
api
controllers
routes
posts.js (trying to import this file, edited to show actual filename)
Here's the error it throws
internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'E:\ReactProjects\memoirs\server\src\api\routes\posts'
imported from E:\ReactProjects\memoirs\server\server.js
Did you mean to import ../src/api/routes/posts.js?
at finalizeResolution (internal/modules/esm/resolve.js:276:11)
at moduleResolve (internal/modules/esm/resolve.js:699:10)
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:810:11)
at Loader.resolve (internal/modules/esm/loader.js:85:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:229:28)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:51:40)
at link (internal/modules/esm/module_job.js:50:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
[nodemon] app crashed - waiting for file changes before starting...
and my server.js file:
import express from 'express';
import posts from './src/api/routes/posts'; // is there a problem with this file path?
const app = express();
app.use('/api/v1/posts', posts); // error seems to be here, server works if commented out
app.listen(3004, (err)=>{
if(err){
console.log(err);
}
console.log('server at port 3004');
});
The issue seems to be with the file path. I have tried re-arranging it but for some weird reason can't load that particular file into server.js Is there any clear explanation for why this is happening and how to rectify it? Thanks.
It seems like you made a typo. You wrote posts instead of post.
import post from './src/api/routes/post';
EDIT: Seeing that this was just a typo in the question, here is something you could try t fix it:
I would rely on CommonJS Require rather than ES6 Imports on this one, because you can specify relative paths easily by utilizing __dirname:
let express = require('express');
let posts = require(__dirname + '/src/api/routes/posts.js');
I've finally solved this! I got clue from react which throws exact error on not writing .js while importing. You must write extension of even a .js file for node 14+ if you are using type: 'module' in package.json.
Check this image
Solution
So the solution is edit the second line of server.js,
from
import posts from './src/api/routes/posts'
to
import posts from './src/api/routes/posts.js'

Deploy node server on heroku and receive "SyntaxError: Unexpected token import"

I have a server written in node.js using node 6.10.2 and ecmascript. I want to upload it to heroku, locally when i rung the server with "npm start" the server starts without a problem, but on heroku I recevied the following error
import express from 'express';
SyntaxError: Unexpected token import
How can I fix this error?
Thanks
You have 2 options:
Use require() instead of import
Transpile your code
Node doesn't support the import keyword. In Node you import modules with the require() function. For example you can change this:
import express from 'express';
to this:
const express = require('express');
Or you can transpile the code e.g. with Babel if you really want to use import. See:
https://babeljs.io/

Resources