Cannot import npm modules after converting ES5 require to ES6 import

Andrew Serra

I am trying to convert my npm package imports from ES5 (require) to ES6 (import...from) but I keep getting unexpected identifier errors. What should I be looking out for?

I have read through forums but honestly not sure what to try. I checked the documentation in MDN but none of those worked. Every single package has been tried individually but none of them work.

// changed version
import fs from 'fs';

// old imports
// const fs = require('fs')
Jack Bashford

Because they're NPM modules - IIRC you have to use require for those.

I think you can change the command you use slightly and it should work if your Node version is high enough. You'll also need to change the file type from .js to .mjs (module JS):

node --experimental-modules moduleApp.mjs

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

cannot import or require node modules (using npm)

ES6 Modules: Undefined onclick function after import

How to allow both import ES5 and ES6 to my npm package?

ES6 - Convert from 'require' to 'import'

New es6 syntax for importing commonjs / amd modules i.e. `import foo = require('foo')`

ES6 import and npm packages

WebPacker - Load NPM modules via ES6 Import vs webpack.ProvidePlugin()

How can i import npm modules in es6 whiout webpack?

Using require() and import for private npm modules failing

How to import "old" ES5 code in ES6

import ES5 library in ES6

Is there a way to use ES6 modules to import modules with the ArcGIS API

es6 modules export/import: Uncaught TypeError: Cannot read property 'default' of undefined

import momentjs with es6 modules and webpack Cannot assign to read only property

Cannot import router from file with ES6 import/export?

ES6 import equivalent of require() without exports

Add a module in in ES6 using import instead of require

Is it possible to require or import resource from cdn in javascript or es6

Correct way to translate ES6 "import" to nodejs "require"

How does 'require' operates differently from 'import' es6?

CommonJS require vs. ES6 import discrepancy

Convert from require to import es6 module

Converting jsx with ES6 import syntax using reactify

How to import Vanilla JS ES6 modules in TypeScript?

ES6 modules in the browser: Uncaught SyntaxError: Unexpected token import

How to test ES6 modules (these with import) with mocha?

ES6 (EcmaScript 2015) modules: import index.js

Jest es6 modules: unexpected module import

How to import part of object in ES6 modules