Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module #11243

Closed
vtereshyn opened this issue Nov 3, 2022 · 5 comments · Fixed by #13273
Closed

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module #11243

vtereshyn opened this issue Nov 3, 2022 · 5 comments · Fixed by #13273
Assignees
Labels
area/tooling kind/bug Some behavior is incorrect or out of spec language/javascript resolution/by-design This issue won't be fixed because the functionality is working as designed resolution/fixed This issue was fixed
Milestone

Comments

@vtereshyn
Copy link

vtereshyn commented Nov 3, 2022

What happened?

I am migrating my project from CJS to ESM. I put type: "module" to project package.json and now trying to run pulumi preview to test everything.

Here is my Pulumi.yaml:

name: test
description: test
main: src/index.ts
runtime: 
  name: nodejs
  options: 
    # See https://github.com/TypeStrong/ts-node/issues/1007
    nodeargs: "--experimental-specifier-resolution=node --loader ts-node/esm --no-warnings"

Folder structure:

src
  index.ts
  config.ts
Pulumi.yaml
Pulumi.stack.yaml
tsconfig.json
package.json

Full error that I am getting:

 Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /src/index.ts
    require() of ES modules is not supported.
    require() of /src/index.ts from /node_modules/.pnpm/registry.npmjs.org+@pulumi+pulumi@3.45.0/node_modules/@pulumi/pulumi/cmd/run/run.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
    Instead change the requiring code to use import(), or remove "type": "module" from /package.json.

If I move everything from src folder to the root level and remove main from Pulumi.yaml everything works fine.

pulumi version: v3.45.0

Steps to reproduce

Please, see above

Expected Behavior

Pulumi + Typescript creates resources and works well with ESM

Actual Behavior

Pulumi + Typescript don't create resources don't work with ESM

Output of pulumi about

CLI          
Version      3.45.0
Go Version   go1.19.2
Go Compiler  gc

Plugins
NAME    VERSION
nodejs  unknown

Host     
OS       debian
Version  11.5
Arch     x86_64

This project is written in nodejs: executable='/usr/local/share/nvm/versions/node/v18.12.0/bin/node' version='v18.12.0'

Additional context

config.ts file:

const config = new Config();
export const testVar = config.require('test');

index.ts file:

import * as config from './config.js';

export const test = {
  t: config.testVar
}

No response

Contributing

Vote on this issue by adding a 👍 reaction.
To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already).

@vtereshyn vtereshyn added kind/bug Some behavior is incorrect or out of spec needs-triage Needs attention from the triage team labels Nov 3, 2022
@iwahbe iwahbe added language/javascript and removed needs-triage Needs attention from the triage team labels Nov 3, 2022
@dixler
Copy link
Contributor

dixler commented Nov 4, 2022

Hi. I was able to reproduce this. The solution would be to copy both tsconfig.json and package.json to your src directory.

Pulumi uses the value provided in the main field in Pulumi.yaml to also determine the working directory for running a pulumi program. Because of this, it didn't see a package.json or tsconfig.json.

I've opened pulumi/pulumi-hugo#2189 to track clarifying the behavior in the docs as it could be better documented.

@dixler dixler added area/tooling resolution/by-design This issue won't be fixed because the functionality is working as designed labels Nov 4, 2022
@dixler dixler closed this as completed Nov 4, 2022
@vtereshyn
Copy link
Author

@dixler thanks for the responding, but this is not a solution :) so I don't understand why you just closed the issue

When I've used CJS everything worked as expected, so you probably have an issue with your implementation. To support ESM, I need to move files in all infrastructure packages which is not quite a good scenario for my team and me. We use this as a preferred structure for all monorepo packages, and we don't want to use another one for Pulumi only.

Please, correct me if I am wrong, but can we find a workaround or fix that?

@dixler
Copy link
Contributor

dixler commented Nov 7, 2022

When I've used CJS everything worked as expected, so you probably have an issue with your implementation. To support ESM, I need to move files in all infrastructure packages which is not quite a good scenario for my team and me. We use this as a preferred structure for all monorepo packages, and we don't want to use another one for Pulumi only.

My bad, from my limited work with the go and python language hosts, the main indicates the program to run and the working directory. I assumed that nodejs had a similar design, but each language has its own language host implementation. If nodejs behaves in the way you described, it may be an easy fix. I can take a look and get back to you today.

@dixler dixler reopened this Nov 7, 2022
@dixler
Copy link
Contributor

dixler commented Nov 7, 2022

#11045 should hopefully close this.

@dixler dixler assigned RobbieMcKinstry and unassigned dixler Mar 27, 2023
lukaskoeller added a commit to lukaskoeller/freedev that referenced this issue Apr 12, 2023
@lukaskoeller
Copy link

I experience this error although "main": "./index.js".

The package exports an adapter function that is used in svelte.config.js as an adapter to deploy to AWS using the Pulumi Automation API. Thus, when npm run build is executed, the adapter function is called which includes Pulumi Automation API code.

The package with the adapter is a Javascript Project using "module": "es2020". In tsconfig.json, allowJs and checkJs is set to true. It fails with the following output in the bash terminal:

err?: Error: Command failed with exit code 255: pulumi up --yes --skip-preview --exec-kind auto.local --stack dev --non-interactive
Updating (dev)

…

pulumi:pulumi:Stack (loremipsum):
    error: Running program '/Users/loremipsum/stacks/server/' failed with an unhandled exception:
    Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/loremipsum/stacks/server/index.js from /Users/loremipsum/node_modules/@pulumi/pulumi/cmd/run/run.js not supported.

…

code: 'ERR_REQUIRE_ESM'

I use es modules throughout the package, there is no require() I use. But the run.js from @pulumi/pulumi is using require().

Pulumi.yaml

name: loremipsum
runtime:
  name: nodejs
  options:
    typescript: false
description: AWS infrastructure for a sveltekit app 

package.json

{
  "name": "@lorem/sveltekit-adapter",
  "version": "0.0.0",
  "main": "./index.js",
  "type": "module",
  "dependencies": {
    "@pulumi/command": "^0.7.2",
    "@pulumi/pulumi": "^3.62.0",
    "tsconfig": "^0.0.0"
  }
} 

@RobbieMcKinstry RobbieMcKinstry removed their assignment Apr 19, 2023
@justinvp justinvp self-assigned this Jun 24, 2023
@justinvp justinvp added this to the 0.90 milestone Jun 24, 2023
@bors bors bot closed this as completed in 1a92341 Jun 29, 2023
@pulumi-bot pulumi-bot added the resolution/fixed This issue was fixed label Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/tooling kind/bug Some behavior is incorrect or out of spec language/javascript resolution/by-design This issue won't be fixed because the functionality is working as designed resolution/fixed This issue was fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants