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

ReferenceError: require is not defined in ES module scope, you can use import instead #1104

Closed
jpcarpanezi opened this issue Aug 23, 2023 · 1 comment

Comments

@jpcarpanezi
Copy link

i'm trying to build my index.js in a single contained file. That's working very well with commonjs, but when i try to build with module type, when i run the code it doesn't work.

I run the command:

ncc build index.js -m

This is my test code:

import { createLogger, format as _format, transports as _transports } from 'winston';

const logger = createLogger({
	level: 'info',
	format: _format.json(),
	defaultMeta: { service: 'user-service' },
	transports: [
		//
		// - Write all logs with importance level of `error` or less to `error.log`
		// - Write all logs with importance level of `info` or less to `combined.log`
		//
		new _transports.File({ filename: 'error.log', level: 'error' }),
		new _transports.File({ filename: 'combined.log' }),
	],
});

//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
if (process.env.NODE_ENV !== 'production') {
	logger.add(new _transports.Console({
		format: _format.simple(),
	}));
}

This is my package.json:

{
	"name": "typescript-test-2",
	"version": "1.0.0",
	"type": "module",
	"description": "",
	"main": "index.js",
	"scripts": {
		"test": "echo \"Error: no test specified\" && exit 1"
	},
	"keywords": [],
	"author": "",
	"license": "ISC",
	"dependencies": {
		"@vercel/ncc": "^0.36.1",
		"winston": "^3.10.0"
	}
}

Then, when i run node dist/index.js it throws this error:

file:///C:/Users/user/source/repos/app/scripts/dist/index.js:14506
module.exports = require("util");
^

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'file:///C:/Users/user/source/repos/app/scripts/dist/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at 3837 (file:///C:/Users/user/source/repos/app/scripts/dist/index.js:14506:1)
    at __nccwpck_require__ (file:///C:/Users/user/source/repos/app/scripts/dist/index.js:14546:42)
    at 5825 (file:///C:/Users/user/source/repos/app/scripts/dist/index.js:41:12)
    at __nccwpck_require__ (file:///C:/Users/user/source/repos/app/scripts/dist/index.js:14546:42)
    at 1251 (file:///C:/Users/user/source/repos/app/scripts/dist/index.js:770:14)
    at __nccwpck_require__ (file:///C:/Users/user/source/repos/app/scripts/dist/index.js:14546:42)
    at 3605 (file:///C:/Users/user/source/repos/app/scripts/dist/index.js:5264:16)
    at __nccwpck_require__ (file:///C:/Users/user/source/repos/app/scripts/dist/index.js:14546:42)
    at 7829 (file:///C:/Users/user/source/repos/app/scripts/dist/index.js:5695:23)
    at __nccwpck_require__ (file:///C:/Users/user/source/repos/app/scripts/dist/index.js:14546:42)

Does anyone has this problem before?

@styfle
Copy link
Member

styfle commented Aug 23, 2023

Looks like a duplicate of #791

@styfle styfle closed this as not planned Won't fix, can't repro, duplicate, stale Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants