Comments (9)
Hrmm, actually, no, v5 works with a commonjs project as well. axios
has dual publishing, so it can be loaded either way.
this works fine:
npm init
npm i twilio
# copy sample above into index.js
node index.js
I tried a couple of different combinations with TS, but couldn't reproduce there either. Sorry for the diversion.
from twilio-node.
Hi @Manbearpixel! I just tried doing the fresh install and it works for me. Things to remember:
- Make sure you have
"type": "module"
present in your package.json - I am not sure why you are using
.done()
in the last line. I removed it and it worked perfectly for me
Let me know if you still face issue. Thanks!
from twilio-node.
Make sure you have "type": "module" present in your package.json
That is a gigantic undertaking if you don't have it there already.
When libraries used in node decide to convert to ESM only, the overwhelming majority of people just use the previous version.
npm i twilio@^4
from twilio-node.
When libraries used in node decide to convert to ESM only, the overwhelming majority of people just use the previous version.
npm i twilio@^4
Does that mean twilio V4 was working without the package.json requirement but it is not the case with V5?
from twilio-node.
Hi @ehaynes99 @tiwarishubham635
I am still encountering this issue even in Twilio@4. I have not created this project as a "module" only initiative. Mostly because a lot of dependencies don't support ESM, for example: Knex
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 'project-x/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
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 'project-x/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at file:///project-x/knexfile.js:1:1
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
at async importModuleDynamicallyWrapper (node:internal/vm/module:438:15)
at async openKnexfile (/project-x/node_modules/knex/bin/cli.js:36:16)
at async initKnex (/project-x/node_modules/knex/bin/cli.js:61:7)
at async Command.<anonymous> (/project-x/node_modules/knex/bin/cli.js:223:26)
I was able to get it to work using twilio@3
however. I guess Twilio@4 also uses an updated version of axios which does not have dual loaders?
from twilio-node.
Hey @Manbearpixel! As mentioned by @ehaynes99, we are using axios with 1.6.8. As per the package.json, it supports dual publishing. I would suggest if your project is not module only, you can try using require also
from twilio-node.
I have not created this project as a "module" only initiative.
The error suggests otherwise:
This file is being treated as an ES module because it has a '.js' file extension and 'project-x/package.json' contains "type": "module".
from twilio-node.
I think if you are using with "type": "module"
, then you can easily use import and the following code snippet:
import twilio from 'twilio'
const accountSid = 'xxx'
const authToken = 'xxx' //ENV var
const client = twilio(accountSid, authToken)
client.messages
.create({
body: 'Hello from Twilio',
from: '+1xxx',
to: '+1xxx'
})
.then(message => {
console.log(message.sid)
})
.catch(error => {
console.log(error);
});
However, if "type": "module"
is a change that you don't want to make, go for the require method:
const accountSid = 'xxx'
const authToken = 'xxx' //ENV var
const client = require('twilio')(accountSid, authToken);
client.messages
.create({
body: 'Hello from Twilio',
from: '+1xxx',
to: '+1xxx'
})
.then(message => {
console.log(message.sid)
})
.catch(error => {
console.log(error);
});
I hope this answers your queries.
from twilio-node.
Closing this issue as no response was received.
from twilio-node.
Related Issues (20)
- [Feature Request]: twilio message create timeout HOT 5
- Twilio Studio HOT 1
- please support latest node v20 HOT 3
- Certificate error while creating conversation message in ie1 HOT 3
- Regression: some call filtering params got removed in v5 HOT 2
- Right-hand side of 'instanceof' is not an object for token.toJwt() (next.js) HOT 2
- Missing required parameter error when updating factor in `verify.v2` HOT 4
- Misleading / Incorrect documentation for webhook validation HOT 5
- retries for network errors (e.g. ECONNRESET)
- [Bug]: Module not found. Error polyfills, HOT 1
- Conferences('xxx').recordings.create does not exist
- [Feature Request]: Need to add background sound with twilio voice call like background office sound or something.
- Twilio crashed nodejs on errors 'Uncaught Exception' HOT 6
- [Feature Request]: fix audit HOT 2
- Question about task_queue_bulk_real_time_statistics HOT 2
- Twillio doesn't work with remix js
- Problem with Twilio streams using Websockets
- update you readme ffs. HOT 4
- [Vulnerability] patch axios vulnerability CVE-2023-26159 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from twilio-node.