-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Working with node and only esm packages #7872
Comments
Hi @alonronin , can I have more information? Or a repo to replicate this? I'm seeing the issue with ESM and want to figure out the steps to get to the error. |
@nartc sure, here is an example repo: $ git clone && yarn && yarn nx run execa:serve |
Yesterday bumped into this error when migrated my NX node project from CommonJS to ESM. Root cause investigation:
Workaround:
// @filename ./apps/myapp/webpack.config.cjs
const nodeExternals = require('webpack-node-externals');
module.exports = (config, context) => {
return {
...config,
externalsPresets: {
node: true
},
output: {
...config.output,
module: true,
libraryTarget: 'module',
chunkFormat: 'module',
library: {
type: 'module'
},
environment: {
module: true
}
},
experiments: {
outputModule: true,
},
externals: nodeExternals({
importType: 'module'
})
}
}
...
"myapp": {
...
"architect": {
...
"build": {
"builder": "@nrwl/node:build",
"options": {
...
"webpackConfig": "apps/myapp/webpack.config.cjs",
},
},
},
}
/*
const fs = require('fs');
const os = require('os');
const cp = require('child_process');
*/
import fs from 'fs';
import os from 'os';
import cp from 'child_process';
...
"compilerOptions": {
"module": "ES2020",
} After these steps are done your app will be assembled by Webpack with externals imported using ESM way. And you'll see in import * as __WEBPACK_EXTERNAL_MODULE__google_cloud_bigquery_18c5c905__ from "@google-cloud/bigquery";
//...
/***/ "@google-cloud/bigquery":
/*!*****************************************!*\
!*** external "@google-cloud/bigquery" ***!
\*****************************************/
/***/ ((module) => {
var x = y => { var x = {}; __webpack_require__.d(x, y); return x; }
var y = x => () => x
module.exports = __WEBPACK_EXTERNAL_MODULE__google_cloud_bigquery_18c5c905__;
/***/ }), |
@nartc I propose a feature request: add nodeExternals({
modulesDir,
importType: 'module'
}) nx/packages/node/src/utils/node.config.ts Line 27 in 0b3cab0
And also this flag should tune the default webpack config used during build as described in the comment above |
@dobromyslov thanks! would be great if this is on nx roadmap to fix node's generators. |
This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. |
I think support is still needed? I haven't gave them a try recently. |
I have experimented a little with There are some caveats though:
It would certainly be possible to adjust |
This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. |
Still real pain |
This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. |
Fighting the stalebot! |
Will #10414 fix this? |
This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. |
Up |
I was running into a bunch of issues trying to run plain typescript that targets esm and also uses other libraries in my workspace. I got a custom module loader working that hooks in the commonjs path resolver (so directory imports work and you don't need the contentious '.js' suffix!) and also grabs the library paths from the base tsconfig so it knows where to find your libraries. Easy running of ts->esm scripts e.g. TS_NODE_PROJECT=packages/test/tsconfig.lib.json NX_BASE_TSCONFIG=./tsconfig.base.json node --loader nx-ts-esm-loader packages/test/index.ts https://www.npmjs.com/package/nx-ts-esm-loader - hope it helps! |
up |
1 similar comment
up |
This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context. |
Current Behavior
this cause an error
Expected Behavior
The package in the example is execa, however it happens with any esm only package as node-fetch.
Environment
The text was updated successfully, but these errors were encountered: