I have a very basic index.js that has these imports like so:
import { MikroORM } from "#mikro-orm/core";
import { __prod__ } from "./constants";
import { Post } from "./entities/Post";
Here's my package.json:
{
"name": "reddit-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"watch": "tsc -w",
"dev": "nodemon dist/index.js",
"start": "node dist/index.js",
"start2": "ts-node src/index.ts",
"dev2": "nodemon --exec ts-node src/index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#types/node": "^16.3.2",
"nodemon": "^2.0.12",
"ts-node": "^10.1.0",
"typescript": "^4.3.5"
},
"dependencies": {
"#mikro-orm/cli": "^4.5.7",
"#mikro-orm/core": "^4.5.7",
"#mikro-orm/migrations": "^4.5.7",
"#mikro-orm/postgresql": "^4.5.7",
"pg": "^8.6.0"
}
}
I compile them with tsc into a dist directory with structure like:
[1]: https://i.stack.imgur.com/uC5pU.png
It gives me the error:
Cannot find module '/Users/kevinli/reddit-clone/reddit-server/dist/constants' imported from /Users/kevinli/reddit-clone/reddit-server/dist/index.js
I'm confused because they seem to be in the same directory and should be importing correctly?
If I try to remove "type": "module",, it tells me I cannot import something outside a module.
I faced the same issue today.
Try adding the extension .js or whatever it is to the import path.
I learnt without the extension nodejs assumes the path is a directory, so it will be expecting an index.js (default) file inside it.
Related
I am working on a graphql project. I have installed nodemon globally but whenever I make any change I have to restart manually because for some reason nodemon is not working correctly. Following is my environment
Ubuntu
Nodejs 10.24.1
nodemon 2.0.15
and my Package.json file is following
{
"name": "graphql-basic",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon --exec babel-node src/index.js",
"test": "echo \"Error: no test specified\" && exit 1",
"get-schema": "graphql get-schema -p prisma"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.7.0",
"graphql-cli": "^2.16.4",
"graphql-yoga": "^1.16.7",
"prisma-binding": "^2.1.1",
"uuid": "^3.3.2"
},
"devDependencies": {}
}
I have a simple node.js API was written in typescript.
however, when I try to run my build. it return me an error as
node_modules/socket.io/dist/socket.d.ts:63:39 - error TS1005: ',' expected.
63 export declare type Event = [eventName: string, args: any[]];
~
node_modules/socket.io/dist/socket.d.ts:63:53 - error TS1005: ',' expected.
63 export declare type Event = [eventName: string, args: any[]];
It looks like something wrong with the socket.io. it is because the socket.io version is not compatiable with the typescript version?
here is my package.json
"name": "project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"tsc": "tsc",
"test": "echo \"Error: no test specified\" && exit 1",
"start": "npx ts-node app.ts",
"start1": "npm run build && node build/index.js",
"dev": "nodemon ./src/app.ts",
"dev1": "ts-node-dev --respawn ./src/app.ts",
"build": "rimraf ./build && tsc"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.3",
"socket.io": "^4.4.1",
"uuid": "^8.3.2"
},
"devDependencies": {
"#types/express": "^4.17.13",
"#types/node": "^17.0.18",
"nodemon": "^2.0.15",
"rimraf": "^3.0.2",
"ts-node": "^10.5.0",
"ts-node-dev": "^1.1.8",
"typescript": "^3.9.10"
}
}
I am running my build using
rimraf ./build && tsc
change the typescript to 4.4.2 in package.json file and it works....I didn't know the package version is not same as my local..super mistake..
So I am simply trying to deploy my node server to Heroku cloud. It is crashing because of the ES modules not supported issue. The specific error I am getting is
Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/app/src/routes' is not supported resolving ES modules imported from /app/src/server.js
I am trying to import my routes directory. I have read the solutions on Stack related to this issue, and none of them are working for me. For example inlcuding "type":"module" in my package.json, is NOT working.
What's going on? It runs perfectly locally using 'npm run dev' script.
Ironman
package.json
{
"name": "back-end",
"type": "module",
"version": "1.0.0",
"description": "",
"engines": {
"node": "14.x"
},
"main": "index.js",
"scripts": {
"dev": "nodemon --exec babel-node -r dotenv/config ./src/server.js",
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node -r dotenv/config ./src/server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#babel/core": "7.13.10",
"#babel/node": "7.13.12",
"#babel/preset-env": "7.13.12",
"#sendgrid/mail": "^7.6.0",
"axios": "^0.24.0",
"bcrypt": "^5.0.1",
"dotenv": "^10.0.0",
"esm": "^3.2.25",
"express": "4.17.1",
"googleapis": "^92.0.0",
"jsonwebtoken": "^8.5.1",
"mongodb": "3.6.5",
"uuid": "^8.3.2"
},
"devDependencies": {
"nodemon": "2.0.7"
}
}
The answer, the solution, was to require the esm package on startup:
"start": "node -r esm -r dotenv/config ./src/server.js"
My process.env file has this:
DB_NAME = hello
When I run my index.ts file which has this:
import { MikroORM } from '#mikro-orm/core';
import { __prod__ } from './constants';
import { Post } from './entities/Post';
import microConfig from './mikro-orm.config';
console.log(`console log is : ${process.env.DB_NAME}`);
const main = async () => {
const orm = await MikroORM.init(microConfig);
const post = orm.em.create(Post, { title: 'my first post' });
orm.em.persistAndFlush(post);
};
main();
It gives the value of process.env.DB_NAME as undefined.
My package.json is set up as follows:
{
"name": "shreddit-backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node dist/index.js",
"dev": "nodemon dist/index.js",
"watch": "tsc -w",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#types/node": "^14.14.10",
"nodemon": "^2.0.6",
"ts-node": "^9.0.0",
"typescript": "^4.1.2"
},
"dependencies": {
"#mikro-orm/cli": "^4.3.2",
"#mikro-orm/core": "^4.3.2",
"#mikro-orm/migrations": "^4.3.2",
"#mikro-orm/postgresql": "^4.3.2",
"pg": "^8.5.1"
},
"mikro-orm": {
"useTsNode": true,
"configPaths": [
"./src/mikro-orm.config.ts",
"./dist/mikro-orm.config.js"
]
}
}
I even tried using dotenv but that too wasn't able to fix the problem. Am I missing something?
Thanks a lot.
Doesn't the process.env file automatically get loaded?
No, You're going to have to use dotenv.config as the following (at the very start of the script).
require('dotenv').config({ path: './process.env' })
// the rest of your code
Note that you can simply call .config without any arguments should your file be named just '.env' (which the de facto standard name for such purposes) - and not 'process.env'
I tried to run test automatically using nodemon instead of running them manually. I installed mocha and nodemonn on single time. This is how my package.json looks like:
{
"name": "Ti Ri Ho",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "nodemon --exec 'mocha -R min'"
},
"author": "",
"license": "ISC",
"dependencies": {
"mocha": "^5.2.0",
"mongoose": "^5.3.15",
"nodemon": "^1.18.7"
}
}
I change "scripts": {"test": "mocha" } to "scripts": {"test": "nodemon --exec 'mocha -R min'"}.What could be the possible reason?