Node.js 14 ES module import from another folder - node.js

This is a FYI and a question.
I am using Node.js 14.17.3 (LTS) and wanted to take advantage of using import ES syntax to use some files in another project (which need to remain where they are).
So, I made a simple node.js app to test the import. I added "type": "module" to the package.json file in the test app folder.
The (cut down test) file mixin-modules-definitions.js to import is:
const classModuleDefs = {
name: 'mydef'
}
export default classModuleDefs
Now, if I have that file in the same folder as the package.json file of my test app, this works fine:
import classModuleDefs from './mixin-modules-definitions.js'
console.log('classModuleDefs=', classModuleDefs)
But if I try to import from where the file actually lives like the following, I get the dreaded SyntaxError: Unexpected token 'export' error.
import classModuleDefs from '../src/services/mixin-modules-definitions.js'
And, if I then add "type": "module" to the nearest package.json file relevant to that path, the error goes away and the file is imported correctly.
As it turns out, the app I am importing from is ok with adding "type": "module" to its package.json file but I could imagine a scenario where it might not be ok.
Is it possible to import a file and have node resolve to use the package.json in my test node app without needing to also change the package.json in the "source" app.
Thanks,
Murray

Related

Resolved: Nodejs | Typescript | Router | ts-node-esm: Receiving: Cannot find module

I could really use some help. I'm trying to create my first nodejs/react app with typescript. I am simply trying to import routes from /src/routes/tickerRouter to server/index.ts but I am running into an error.
Error:
/.../node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:366
throw new ERR_MODULE_NOT_FOUND(
^
CustomError: Cannot find module '/.../react-node-app/server/src/routes/tickerRouter' imported from /.../react-node-app/server/index.ts
What I've done so far:
setup all my files with .ts instead of .js
tried adding .js to the import line in index.ts, while keeping the actual file extension .ts
running start with ts-node-esm server/index.ts
Added "type": "module" in package.json.
Using export default instead of module.exports =
Spent way too long scouring the web for answers :'(
Okay, it turns out
tried adding .js to the import line in index.ts, while keeping the actual file extension .ts
worked. It just caused the same error in another file. For whatever reason, ts files need to be imported with the .js extension.

Accessing typeDefs in the dist folder | NestJS/GraphQL

Context: I had a project at which backend was written with GraphQL/lambdas. Now I want to convert whole project to NestJS/GraphQL.
I have joined.graphql file in src/GraphQL folder and there is an import (usage) of this file in another .ts file. When I try to start the project with nest start command, none of *.graphql files are copied to dist folder. Then, that import throws an error as there is no joined.graphql file in the dist folder.
I tried to add nest-cli.json file with assets field to include ./**/*.graphql files as assets. Now the issue is indeed fixed. But...
Problem:
Another error occured as
/<MY_PROJECT>/dist/src/GraphQL/joined.graphql
type Query {
^^^^^
SyntacError: Unexpected identifier
The code was trying to import typeDefs. I think there should be something a bit tricky since I'm not starting the project with nest but trying to convert to nest. Maybe missing some steps to work correctly with graphql. I'm not sure.

Cannot use import statement outside a module with #pusher/push-notifications-web nodejs - beams

I am trying to follow this tutorial using nodejs and express: https://pusher.com/docs/beams/reference/web/#npm-yarn
First I did: npm install #pusher/push-notifications-web before adding the code.
But when I add this code in the index.js file:
import * as PusherPushNotifications from "#pusher/push-notifications-web";
const beamsClient = new PusherPushNotifications.Client({
instanceId: "<YOUR_INSTANCE_ID_HERE>",
});
beamsClient.start().then(() => {
// Build something beatiful ๐ŸŒˆ
});
I get this error:
SyntaxError: Cannot use import statement outside a module
It's also not very clear to me from the tutorial if the code has to be in the frontend or the backend. I tried both but got the same result.
How can I fix this problem?
The error is caused by the fact that you're trying to use ES module specific features in a regular CommonJS file (the default behavior in Node.js). However, what you're looking at is the Web SDK for Pusher which won't help you achieve your goals.
You need the server SDK for Node.js - https://pusher.com/docs/beams/reference/server-sdk-node/.
Verify that you have the latest version of Node.js installed and you have 2 ways of fixing that
Set "type" field with a value of "module" in package.json. This will ensure that all .js and .mjs files are interpreted as ES modules.
// package.json
{
"type": "module"
}
Use .mjs as file extension instead of .js.

Azure App Service (Windows) - Nodejs ES Module Problems with SvelteKit app

really hoping someone can point me in the right direction with this one as i'm having no luck at all. I'm trying to host a simple nodejs sveltekit application on a Windows based azure app service, but cannot get the application to start / run.
I'm using the adapter-node adapter for sveltekit to generate the build output as a self contained node app. After sveltekit generates the build output I inject a simple package.json file to the root of the build folder to instruct node to use the ESM style imports which simply contains a single property of type="module".
package.json
{
"type": "module"
}
Lastly I also inject a web.config into the root of the build folder for use with IISNode. The web.config file used is the same as from the nodejs quickstart guide provided by MS. The web.config can be seen here.
The final folder structure of the build output is simply:
build
โ””โ”€โ”€โ”€assets
โ”‚ โ””โ”€โ”€โ”€_app
โ”‚ โ”‚ ...
โ””โ”€โ”€โ”€prerendered
โ”‚ index.js
โ”‚ package.json
โ”‚ web.config
Locally I can take this build folder, place it anywhere on my machine and it runs perfectly by simply running:
node index.js
The Problem
Even though it works perfectly locally, when I deploy the application to the Azure app service the application will not start with the browser simply displaying "This page isnโ€™t working right now".
When I check the logs I see the following error:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\home\site\wwwroot\index.js
require() of ES modules is not supported.
require() of D:\home\site\wwwroot\index.js from D:\Program Files (x86)\iisnode\interceptor.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from D:\home\site\wwwroot\package.json.
The error tells me that MS's iisnode\interceptor.js is using the commonjs style require syntax and cannot import the ES module of my index.js.
I found someone having a similar problem and a suggested solution here. The suggested solution is to create a new file next to my index.js file and configuring it as the app service's (or more specifically iisnode's) entry point in the web.config. The new file would be named run.cjs and only contain the following:
import("./index.js");
I tried this option, adding the new run.cjs file and updating the web.config to set this as IISNodes entry point:
<add name="iisnode" path="run.cjs" verb="*" modules="iisnode"/>
When I try the site after doing this I get a new problem. The site now loads but instead of seeing the app, the js from index.js renders as raw text into the browser.
The azure app service WEBSITE_NODE_DEFAULT_VERSION is set to ~14 and I can see from Kudu that the version running is 14.16.0 - my local machine is 14.17.0 so the node version looks to be ok.
Can anyone help??
Thanks in advance
Please re-install/update the npm module on your project.
Make sure all these files are present in your project.
Do not import your index.js file in other files like run.cjs or run.mjs, after building your application in your local and publish it in azure app service.
{
"type": "module"
}
This above code is required in the package.json file.
Check your npm reference files, if anyone of them is not installed properly, then you'll get the raw data which is present in app,js file, as output

import class from external madule does not work in NodeJS

I'm pretty new to Node.js and try to learn it for my work.
I want to import a class name "IgApiClient" from a file with name "client.ts"
the file exists in "core" folder which it exists in "src" folder.
the file I try to run is under "example" folder which it and "src" are in one folder.
Here is screenshots of what I said:
first
second
I think I should use the import statement for this as below:
import { IgApiClient } from '../src';
which is for nodejs version before 13 and for ver 13 and above I should use this as below:
const { IgApiClient } = require('../src');
I'm using the second one for my purpose.
But when I run the code it get me this error:
the error
I also tried to move the "client" file to "node_modules" folder and replace "../src" with "../src/core/client", but none of them worked.
what's wrong with it?
How can I solve it?
If anyone has a better idea for this I appreciate it.
Use babel to use ES6 feature like classes in NodeJs. You check this as an example babel-node.

Resources